CoCalc Shared FileslogSpiralInteract.sagews
Authors: Pat Browne, Harald Schilly, William A. Stein
Views : 8
Description: GraphicsIssueslogSpiral
class logSpiral: def __init__(self): self.size = 0 self.points = [] self.pointsR = [] self.g = Graphics() def normal(self,xyPoints,xyI): var('x') aPoint = xyPoints[0] bPoint = xyPoints[1] #deltay = 0 if bPoint[1] == aPoint[1]: return 0 #deltax = 0 if bPoint[0] == aPoint[0]: return 1e6 m = -(bPoint[0] - aPoint[0])/ (bPoint[1] - aPoint[1]) b = xyI[1] - m*xyI[0] return N(m)*x + b def eqnLine(self,aPoint,bPoint): var('x') yb = bPoint[1] ya = aPoint[1] xb = bPoint[0] xa = aPoint[0] m = (yb - ya) / (xb -xa) return m*(x - xa) + ya def doSpiral(self,n,c): #print "doSpiral received n, c",n,c self.points= [[[0.0,0.0],[ N(7*cos(pi*k/6.0)), N(6*sin(pi*k/6.0))]] for k in range(n)] self.pointsR = [[[0.0,0.0],[ N(7*cos(pi*k/6.0)), N(6*sin(pi*k/6.0))]] for k in range(n,0,-1)] spiral = self.pointsR if c == True else self.points #print spiral px = spiral[0][1][0]/5.0 py = spiral[0][1][1]/5.0 x,y = var('x','y') self.g = Graphics() lprev = None ptIx = None #self.g = polar_plot([ r for r in range(1,8)],(x,0,2*pi)) #debug issue 1 fixed - keep one color for all circles by not using polar plot for r in range(1,8): self.g += circle((0,0),r) for i in range(n): lxy = spiral[i] #print "lxy",lxy self.g += line(lxy) if lprev <> None: slopeExp = self.eqnLine(lxy[0],lxy[1]) eqn = y - slopeExp #print "eqn {}".format(eqn) if eqn == y: #print "lxy and lprev for horizontal{} {}".format(lxy,lprev[1]) norm = y - self.eqnLine(lprev[1],lxy[1]) #print "adjusting norm {}".format(norm) else: deltaY = lprev[1][1] - lprev[0][1] if deltaY == 0: ptIx = px yy = self.eqnLine(lxy[0],lxy[1]) ptIy = yy.subs(x=px) else: norm = y - self.normal([[0.0,0.0],[px,py]],[px,py]) #print "eqn",eqn #print "norm",norm pointI = solve([eqn,norm],x,y,solution_dict=True) ptIx = N(pointI[0][x]) ptIy = N(pointI[0][y]) #print "debug point of intersection",N(ptIx),N(ptIy) gn = line([[px,py],[ptIx,ptIy]],color='red') self.g += gn lprev = lxy if ptIx <> None: px = ptIx py = ptIy self.g.show() #return self.g def magnitude(self): #sage doesn't like classes using keywords size or length for methods!!! return self.size def remove(self,n): self.g = None logspiral = logSpiral() @interact def logSpiralInteract(n = slider(1,100,1,12, label="Radials"),c = checkbox(False,"Reverse")): logspiral.doSpiral(n,c)