Advertisement
miklis

grafiku piesimas

Jan 8th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. from math import*
  2. from turtle import*
  3. from time import*
  4. def graph(curves, c1=-275.,c2=283.,d1=-275.,d2=283.,scalingXY=(10,10,'x','y'),rounding=3, background='lightgreen', split_h=(True,10,"red"), split_v=(True,10,'red',0), lower=None, upper=None, go=goto, message=True):
  5. #curves=list of curves to graph - form: [(color, width,),*set of points]
  6. #scalingXY - for painting axis; rounding - for coordinates rounding; splits - for griding
  7. colormode(255)
  8. if message:
  9. print '2d preview...',
  10. t=time()
  11. reset()
  12. tracer(0)
  13. C,D=c2-c1,d2-d1
  14. if lower==None: x1,y1=float(min([min([el[0] for el in n[1:]]) for n in curves])),float(min([min([el[1] for el in n[1:]]) for n in curves]))
  15. else: x1,y1=lower[0],lower[1]
  16. if upper==None: x2,y2=float(max([max([el[0] for el in n[1:]]) for n in curves])),float(max([max([el[1] for el in n[1:]]) for n in curves]))
  17. else: x2,y2=upper[0],upper[1]
  18. difx,dify=x2-x1,y2-y1
  19. bgcolor(background)
  20. #responsible for drawing a grid
  21. def improved_int(a):
  22. if a>0: return(int(a))
  23. else: return int(a)-1
  24. if split_h[0]:
  25. pencolor(split_h[2])
  26. k=C/difx*(-x1-difx/split_h[1]*improved_int(-x1*split_h[1]/difx)) #a number to move gridliness
  27. special_line=improved_int(-x1*split_h[1]/difx)/float(split_h[1])
  28. for n in [float(i)/split_h[1] for i in range(split_h[1])]:
  29. up()
  30. go(c1+k+C*n,d1)
  31. down()
  32. if n==special_line:
  33. pensize(3)
  34. go(c1+k+C*n,d2)
  35. pensize(1)
  36. else: go(c1+k+C*n,d2)
  37. if split_v[0]:
  38. pencolor(split_v[2])
  39. k=C/dify*(-y1-dify/split_v[1]*improved_int(-y1*split_v[1]/dify)) #a number to move gridliness
  40. special_line=improved_int(-y1*split_v[1]/dify)/float(split_v[1])
  41. for n in [float(i)/split_v[1] for i in range(split_v[1]+1)]:
  42. up()
  43. go(c1,d1+k+C*n)
  44. down()
  45. if n==special_line:
  46. pensize(3)
  47. go(c2,d1+k+C*n)
  48. pensize(1)
  49. else: go(c2,d1+k+C*n)
  50. #responsible for drawing a curves
  51. update()
  52. for m in curves:
  53. tracer(0)
  54. pencolor(m[0][0])
  55. pensize(m[0][1])
  56. up()
  57. for n in m[1:]:
  58. go(c1+C*(n[0]-x1)/difx,d1+D*(n[1]-y1)/dify)
  59. down()
  60. update()
  61. pencolor("black")
  62. pensize(1)
  63. #responsible for marking axis
  64. up()
  65. go(c1,d1)
  66. down()
  67. k=C/difx*(-x1-difx/scalingXY[0]*improved_int(-x1*scalingXY[0]/difx))
  68. for n in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
  69. go(c1+k+C*n,d1)
  70. write(str(round(x1+k*difx/C+n*difx,rounding)))
  71. dot(5)
  72. go(c1+k+C*(n+.5/scalingXY[0]),d1)
  73. write(str(scalingXY[2]))
  74. pensize(1)
  75. up()
  76. go(c1,d1)
  77. down()
  78. k=C/dify*(-y1-dify/scalingXY[1]*improved_int(-y1*scalingXY[1]/dify))
  79. for n in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
  80. go(c1,d1+k+C*n)
  81. write(str(round(y1+k*dify/C+n*dify,rounding)))
  82. dot(5)
  83. go(c1,d1+k+C*(n+.5/scalingXY[1]))
  84. write(str(scalingXY[3]))
  85. if message: print time()-t
  86. exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement