PageRenderTime 51ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/rel-1-3-25/SWIG/Examples/GIFPlot/Python/full/runme.py

#
Python | 64 lines | 49 code | 10 blank | 5 comment | 4 complexity | df9a4776645c3d522ff5652fcabafa13 MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. # Plot a 3D function
  2. # This example uses the low-level C interface.
  3. from _gifplot import *
  4. from math import *
  5. # Here is the function to plot
  6. def func(x,y):
  7. return 5*cos(2*sqrt(x*x+y*y))*exp(-0.3*sqrt(x*x+y*y))
  8. # Here are some plotting parameters
  9. xmin = -5.0
  10. xmax = 5.0
  11. ymin = -5.0
  12. ymax = 5.0
  13. zmin = -5.0
  14. zmax = 5.0
  15. # Grid resolution
  16. nxpoints = 60
  17. nypoints = 60
  18. cmap = new_ColorMap("cmap")
  19. frame = new_FrameBuffer(500,500)
  20. FrameBuffer_clear(frame,BLACK)
  21. p3 = new_Plot3D(frame,xmin,ymin,zmin,xmax,ymax,zmax)
  22. Plot3D_lookat(p3,2*max([xmax-xmin,ymax-ymin,zmax-zmin]))
  23. Plot3D_autoperspective(p3,40)
  24. Plot3D_rotu(p3,60)
  25. Plot3D_rotr(p3,30)
  26. Plot3D_rotd(p3,10)
  27. def drawsolid():
  28. Plot3D_clear(p3,BLACK)
  29. Plot3D_start(p3)
  30. dx = 1.0*(xmax-xmin)/nxpoints
  31. dy = 1.0*(ymax-ymin)/nypoints
  32. cscale = 240.0/(zmax-zmin)
  33. x = xmin
  34. for i in xrange(0,nxpoints):
  35. y = ymin
  36. for j in xrange(0,nypoints):
  37. z1 = func(x,y)
  38. z2 = func(x+dx,y)
  39. z3 = func(x+dx,y+dy)
  40. z4 = func(x,y+dy)
  41. c1 = cscale*(z1-zmin)
  42. c2 = cscale*(z2-zmin)
  43. c3 = cscale*(z3-zmin)
  44. c4 = cscale*(z4-zmin)
  45. c = int((c1+c2+c3+c4)/4)
  46. if (c < 0) : c = 0
  47. if c > 239 : c = 239
  48. Plot3D_solidquad(p3,x,y,z1,x+dx,y,z2,x+dx,y+dy,z3,x,y+dy,z4,c+16)
  49. y = y + dy
  50. x = x + dx
  51. print "Making a nice 3D plot..."
  52. drawsolid()
  53. FrameBuffer_writeGIF(frame,cmap,"image.gif")
  54. print "Wrote image.gif"