Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import*
- from turtle import*
- from time import*
- 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):
- #curves=list of curves to graph - form: [(color, width,),*set of points]
- #scalingXY - for painting axis; rounding - for coordinates rounding; splits - for griding
- colormode(255)
- if message:
- print '2d preview...',
- t=time()
- reset()
- tracer(0)
- C,D=c2-c1,d2-d1
- 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]))
- else: x1,y1=lower[0],lower[1]
- 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]))
- else: x2,y2=upper[0],upper[1]
- difx,dify=x2-x1,y2-y1
- bgcolor(background)
- #responsible for drawing a grid
- def improved_int(a):
- if a>0: return(int(a))
- else: return int(a)-1
- if split_h[0]:
- pencolor(split_h[2])
- k=C/difx*(-x1-difx/split_h[1]*improved_int(-x1*split_h[1]/difx)) #a number to move gridliness
- special_line=improved_int(-x1*split_h[1]/difx)/float(split_h[1])
- for n in [float(i)/split_h[1] for i in range(split_h[1])]:
- up()
- go(c1+k+C*n,d1)
- down()
- if n==special_line:
- pensize(3)
- go(c1+k+C*n,d2)
- pensize(1)
- else: go(c1+k+C*n,d2)
- if split_v[0]:
- pencolor(split_v[2])
- k=C/dify*(-y1-dify/split_v[1]*improved_int(-y1*split_v[1]/dify)) #a number to move gridliness
- special_line=improved_int(-y1*split_v[1]/dify)/float(split_v[1])
- for n in [float(i)/split_v[1] for i in range(split_v[1]+1)]:
- up()
- go(c1,d1+k+C*n)
- down()
- if n==special_line:
- pensize(3)
- go(c2,d1+k+C*n)
- pensize(1)
- else: go(c2,d1+k+C*n)
- #responsible for drawing a curves
- update()
- for m in curves:
- tracer(0)
- pencolor(m[0][0])
- pensize(m[0][1])
- up()
- for n in m[1:]:
- go(c1+C*(n[0]-x1)/difx,d1+D*(n[1]-y1)/dify)
- down()
- update()
- pencolor("black")
- pensize(1)
- #responsible for marking axis
- up()
- go(c1,d1)
- down()
- k=C/difx*(-x1-difx/scalingXY[0]*improved_int(-x1*scalingXY[0]/difx))
- for n in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
- go(c1+k+C*n,d1)
- write(str(round(x1+k*difx/C+n*difx,rounding)))
- dot(5)
- go(c1+k+C*(n+.5/scalingXY[0]),d1)
- write(str(scalingXY[2]))
- pensize(1)
- up()
- go(c1,d1)
- down()
- k=C/dify*(-y1-dify/scalingXY[1]*improved_int(-y1*scalingXY[1]/dify))
- for n in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
- go(c1,d1+k+C*n)
- write(str(round(y1+k*dify/C+n*dify,rounding)))
- dot(5)
- go(c1,d1+k+C*(n+.5/scalingXY[1]))
- write(str(scalingXY[3]))
- if message: print time()-t
- exitonclick()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement