Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import*
- from turtle import*
- from time import*
- def algebraic_graph(functions_list,N=25000, h=[-20,20], v=[-20.,20], h_interval=None, v_interval=None, hwidth_of_cell=1, vwidth_of_cell=1, hscale=1, vscale=1, axes=('x','y')):
- #graph of multiple algebraic functions
- #h and v sets up domain and range for functions
- #h_interval and v_interval sets up territory of gridliness
- #IMPORTANT: if range (a,b) of function is small, make sure (a+0.5, b-0.5) doesn't cut off important parts of a function graph
- #to change the width of cell change hwidth_of_cell and vwidth_of_cell
- #to change the scaling change hscale and vscale
- def graph(curves, c1=-575.,c2=575.,d1=-333.,d2=340.,scalingXY=(15,10,'x','y'),rounding=0, background='white', split_h=(True,15,"red"), split_v=(True,10,"red"), lower=None, upper=None, go=goto, message=True):
- #curves=list of curves to graph - form of each: [(color, width),*set of points]
- #scalingXY[0] - (units to scale x; units to scale y; name of Ox axis; name of Oy axis)
- #rounding - precision of scaling
- #background - color of background
- #split_h[0] - (is horizontal gridliness used; split_h[1] - units per board to grid; color of grid)
- #lower - position of left lower corner bounding graph
- #upper - position of right upper corner bounding graph
- #message - are we printing time graph function takes
- '''update: if we have a curve with a stream of data (not (x1,y1), (x2,y2), (x3,y3)... but x1, x2, x3) we assume x1,x2,x3,...=1,2,3,... and y1,y2,y3,... is that stream'''
- colormode(255)
- if message:
- print '2d preview...',
- t=time()
- reset()
- tracer(0)
- C,D=c2-c1,d2-d1
- for j in range(len(curves)):
- if str(type(curves[j][1]))<>"<type 'tuple'>":
- for i in range(len(curves[j])-1): curves[j][i+1]=(i,curves[j][i+1])
- 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]
- #if y1==y2: y1,y2=y1-0.5,y2+0.5
- 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 coordinate to begin 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]+1)]: #each vertical strip
- up()
- go(c1+k+C*n,d1)
- down()
- '''draws a special line and labels axis on it'''
- if n==special_line:
- pensize(3)
- for N in [float(i)/scalingXY[1] for i in range(scalingXY[1])]:
- go(c1+k+C*n,d1+k+D*N)
- write(str(int(round(y1+k*dify/D+N*dify,rounding))), align='right', font=("System", 12, "normal"))
- dot(5)
- go(c1+k+C*n,d2-D*(1./scalingXY[1])/3.) #let's write y
- up()
- left(90)
- shape('classic')
- stamp()
- go(c1+k+C*n+4,d2-D*(1./scalingXY[1])/3.-4)
- write(str(scalingXY[3]), align='left', font=("Arial", 14, "normal"))
- pensize(1)
- else: go(c1+k+C*n,d2)
- if split_v[0]:
- pencolor(split_v[2])
- k=D/dify*(-y1-dify/split_v[1]*improved_int(-y1*split_v[1]/dify)) #a coordinate to begin 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)]: #each vertical strip
- up()
- go(c1,d1+k+D*n)
- down()
- if n==special_line:
- pensize(3)
- for N in [float(i)/scalingXY[0] for i in range(scalingXY[0])]:
- go(c1+k+C*N,d1+k+D*n)
- write(str(int(round(x1+k*difx/C+N*difx,rounding))), align='right', font=("System", 12, "normal"))
- dot(5)
- go(c2-C*(1./scalingXY[0])/3.,d1+k+D*n) #let's write x
- up()
- right(90)
- shape('classic')
- stamp()
- write(str(scalingXY[2]), align='left', font=("Arial", 14, "normal"))
- pensize(1)
- else: go(c2,d1+k+D*n)
- #responsible for drawing a curves
- update()
- for m in curves:
- tracer(0)
- pencolor(m[0][0])
- pensize(m[0][1])
- up()
- go(c1+C*(m[1][0]-x1)/difx,d1+D*(m[1][1]-y1)/dify)
- down()
- for n in m[2:]: go(c1+C*(n[0]-x1)/difx,d1+D*(n[1]-y1)/dify)
- update()
- pencolor("black")
- pensize(1)
- up()
- if message: print time()-t
- hideturtle()
- update()
- exitonclick()
- pot=[]
- c=[("black",4),("brown",4),("purple",4),("darkgreen", 4), ("grey",4), ("red",4)]
- if h_interval==None: h_interval=(h[0]+0.5,h[1]-0.5)
- if v_interval==None: v_interval=(v[0]+0.5,v[1]-0.5)
- for j in range(len(functions_list)):
- dx=(h_interval[1]-h_interval[0])/float(N) #vienos dalies ilgis
- line=[c[j]]
- for i in range(N+1):
- try:
- value=functions_list[j](h_interval[0]+i*dx)
- if v_interval[0]<=value<=v_interval[1]:
- line.append((h_interval[0]+i*dx, functions_list[j](h_interval[0]+i*dx))) #dalos tasku kordinates
- else:
- if len(line)>2: pot.append(line)
- line=[c[j]]
- except (ZeroDivisionError, ValueError,TypeError):
- if len(line)>2: pot.append(line)
- line=[c[j]]
- if len(line)>2: pot.append(line)
- cell_h=int(h[1]-h[0])
- cell_v=int(v[1]-v[0])
- K=reduce(lambda x,y: x+y, [[pot[i]] for i in range(len(pot))])
- print len(K)
- try:
- graph(reduce(lambda x,y: x+y, [[pot[i]] for i in range(len(pot))]),c1=-300.,c2=300.,d1=-300.,d2=300.,\
- scalingXY=(cell_h/hscale,cell_v/vscale,axes[0],axes[1]), split_h=(True,cell_h/hwidth_of_cell,"blue"), split_v=(True,cell_v/vwidth_of_cell,"blue"),rounding=3,lower=(int(h[0]),int(v[0])),upper=(int(h[1]),int(v[1])))
- except ZeroDivisionError: print "We've got ZeroDivisionError, the most probably scale unit exceeds domain"
- algebraic_graph([lambda x: sin(x)], h=[-500,-450],v=[-2,2],hwidth_of_cell=10,hscale=10)
- algebraic_graph([lambda x: sin(10*x)+sin(10.1*x)+sin(10.2*x)], h=[-500,500],v=[-4,4],hwidth_of_cell=50,hscale=100)
- algebraic_graph([lambda x: (9-(x-2)*(x-2))**0.5], h=[-10,10],v=[-10,10],axes=('x','f(x)'))
- algebraic_graph([lambda x: (x+1)/(1-x)+(x-1)/x-2],h=[-10,10],v=[-1000,1000],vwidth_of_cell=100,vscale=100)
- algebraic_graph([lambda x: 3**x, lambda x: 2**x, lambda x: 1.2**x, lambda x: 1**x, lambda x: 0.9**x, lambda x: 0.7**x],h=[-10,10],v=[-10,10])
- algebraic_graph([lambda x: x*x, lambda x: (x-2)*(x-2), lambda x: 2(x-2)*(x-2),lambda x: 2(x-2)*(x-2)+3, lambda x: (3*x-2)*(3*x-2), lambda x: (3*x-2)*(3*x-2)], h=[-15,15],v=[-15,15])
Add Comment
Please, Sign In to add comment