""" grafica della tartaruga con il modulo graphics """ from vettor3 import V3 from vettori import V from graphics import * import math def ruota(vettore, vett_perp, angolo): """ruota un vettore 3d di angolo assegnando un vettore perpendicolare""" return math.cos(angolo)*vettore+math.sin(angolo)*vett_perp def calcola(x1,y1,x2,y2): """ funzione che ritorna dx e dy per asPos """ if x1==x2: dx=0 dy=abs(y2-y1) if y1==y2: dx=abs(x2-x1) dy=0 if (x1<>x2) and (y1<>y2): x1=float(x1) y1=float(y1) x2=float(x2) y2=float(y2) m=(y2-y1)/(x2-x1) l=math.sqrt((y2-y1)**2+(x2-x1)**2) alfa=math.atan(m) dx, dy = abs(l*math.cos(alfa)), abs(l*math.sin(alfa)) if x1>x2: dx=-dx if y1>y2: dy=-dy return dx, dy def rotazione(x,y,alfa): """ rotazione oraria di angolo alfa e centro l'origine, restituisce x e y trasformati""" return x*math.cos(math.pi*alfa/180)+y*math.sin(math.pi*alfa/180), -x*math.sin(math.pi*alfa/180)+y*math.cos(math.pi*alfa/180) class Graf(GraphWin): def __init__(self,title="Graphics Window",width=200, height=200, autoflush=False): """ la classe Graf discende dalla classe GraphWin del modulo graphics """ GraphWin.__init__(self, title,width, height, autoflush) self.coordx=0 self.coordy=0 self.direz=0 self.penna_su=False self.spessore_penna=1 self.tarta_visibile=True self.palla_visibile=False self.H=V3(1,0,0) self.S=V3(0,1,0) self.A=V3(0,0,1) self.XR=V3(1,0,0) self.YR=V3(0,1,0) self.ZR=V3(0,0,1) self.P=V3(0,0,0) self.E=V3(0,0,0) self.P2d=V(0,0) def imbarda(self,angolo): """S cost rotazione dell'aereo intorno al proprio asse dal basso in alto; destra e sinistra in un piano""" temp=ruota(self.H,-1*self.A,angolo) self.A=ruota(self.A,self.H,angolo) self.H=temp def imbardaR(self,angolo): """YR cost rotazione dell'aereo intorno al proprio asse dal basso in alto; destra e sinistra in un piano""" temp=ruota(self.XR,-1*self.ZR,angolo) self.ZR=ruota(self.ZR,self.XR,angolo) self.XR=temp def beccheggia(self,angolo): """self.A cost oscillazioni della nave da poppa a prua""" temp=ruota(self.H,self.S,angolo) self.S=ruota(self.S,-1*self.H,angolo) self.H=temp def beccheggiaR(self,angolo): """self.ZR cost oscillazioni della nave da poppa a prua""" temp=ruota(self.XR,self.YR,angolo) self.YR=ruota(self.YR,-1*self.XR,angolo) self.XR=temp def rolla(self,angolo): """self.H cost oscillazioni della nave mantenendo fisso l'asse poppa prua""" temp=ruota(self.S,self.A,angolo) self.A=ruota(self.A,-1*self.S,angolo) self.S=temp def rollaR(self,angolo): """self.self.XR cost oscillazioni della nave mantenendo fisso l'asse poppa prua""" temp=ruota(self.YR,self.ZR,angolo) self.ZR=ruota(self.ZR,-1*self.YR,angolo) self.YR=temp def progetto(self,vet3): comp_x=vet3.prod_sca(self.XR) comp_y=vet3.prod_sca(self.YR) visual=V(comp_x, comp_y) return visual def avanti3d(self,dist): self.P = self.P+(dist*self.H) R=self.P.diff(self.E) N=self.progetto(R) if not(self.penna_su): li=Line(Point(self.P2d.x,self.P2d.y), Point(N.x,N.y)) li.setWidth(self.spessore_penna) li.draw(self) li.setFill(self.colore_penna) self.P2d=N def disegnapunto3d(self,a,b,c, colore): Pos=V3(a,b,c) Pos=Pos+(-1*self.E) self.plot(Pos.prod_sca(self.XR),Pos.prod_sca(self.YR), colore) def aspos3d(self,a,b,c): self.P=V3(a,b,c) Pos=self.P+(-1*self.E) N=self.progetto(Pos) if not(self.penna_su): li=Line(Point(self.P2d.x,self.P2d.y), Point(N.x,N.y)) li.setWidth(self.spessore_penna) li.draw(self) li.setFill(self.colore_penna) self.P2d=N # fine 3d def coordinate(self, xi,yi,xs,ys,assi=False): """ stabilisce le coordinate cartesiane per la finestra grafica xi yi coord. vertice in basso a sinistra xs ys coord. vertice in alto a destra ovvero le x nell'intervallo xinferiore, xsuperiore le y nell'intervallo yinferiore ysuperiore se si pone assi True, disegna gli assi e le tacche """ self.setCoords(xi, yi, xs, ys) self.xi=xi self.xs=xs self.yi=yi self.ys=ys self.colore_penna='black' self.palla=Circle(Point(0,0),2) self.tarta=Polygon(Point(-(xs-xi)/100.0,-(ys-yi)/40.0), Point(0,0), Point((xs-xi)/100.0,-(ys-yi)/40.0)) if self.palla_visibile: self.palla.draw(self) if self.tarta_visibile: self.tarta.draw(self) self.tarta.setFill("black") if assi==True: ax=Line(Point(xi,0),Point(xs,0)) ax.draw(self) ay=Line(Point(0,yi),Point(0,ys)) ay.draw(self) if round((xs-xi)/10.0) < 1: passox = round((xs-xi)/10.0,2) if round((xs-xi)/10.0) >= 1: passox = round((xs-xi)/10.0) n=1 b=(ys-yi)/50.0 while (n*passox)<= xs: tx=Line(Point(n*passox,0),Point(n*passox,b)) tx.draw(self) te=Text(Point(n*passox,-2*b),str(n*passox)) te.draw(self) n=n+1 n=1 while (n*(-passox))>= xi: tx=Line(Point(n*(-passox),0),Point(n*(-passox),b)) tx.draw(self) te=Text(Point(n*(-passox),-2*b),str(n*(-passox))) te.draw(self) n=n+1 # asse y if round((ys-yi)/10.0) < 1: passoy = round((ys-yi)/10.0,2) if round((ys-yi)/10.0) >= 1: passoy = round((ys-yi)/10.0) n=1 b=(xs-xi)/50.0 while (n*passoy)<= ys: ty=Line(Point(0,n*passoy),Point(b,n*passoy)) ty.draw(self) te=Text(Point(-b*2,n*passoy),str(n*passoy)) te.draw(self) n=n+1 n=1 while (n*(-passoy))>= yi: ty=Line(Point(0,n*(-passoy)),Point(b,n*(-passoy))) ty.draw(self) te=Text(Point(-b*2,n*(-passoy)),str(n*(-passoy))) te.draw(self) n=n+1 def colorePenna(self,colore): self.colore_penna=colore def spessorePenna(self,num): self.spessore_penna=num def asPos(self,x,y): """ assegna alla tarta la posizione x, y """ if not(self.penna_su): LL=Line(Point(self.coordx,self.coordy),Point(x,y)) LL.setWidth(self.spessore_penna) LL.draw(self) LL.setFill(self.colore_penna) dx, dy=calcola(self.coordx,self.coordy,x,y) if self.palla_visibile: self.palla.move(dx,dy) if self.tarta_visibile: self.tarta.move(dx,dy) self.coordx=x self.coordy=y def grafico(self,f,xi,xs,passo=0.5): """grafico di f fra xinf e xsup""" x=xi self.tarta.undraw() self.tarta_visibile=False self.penna_su=True self.asPos(x,f(x)) self.penna_su=False while x<=xs: self.asPos(x,f(x)) x=x+passo self.penna_su=True self.asPos(0,0) self.penna_su=False def pennasu(self): self.penna_su=True def pennagiu(self): self.penna_su=False def nascondi_palla(self): self.palla.undraw() self.palla_visibile=False def mostra_palla(self,raggio): self.palla=Circle(Point(self.coordx,self.coordy),raggio) self.palla.draw(self) self.palla.setFill("black") self.palla_visibile=True def nascondi_tarta(self): self.tarta.undraw() self.tarta_visibile=False def mostra_tarta(self): xa, ya=rotazione(-(self.xs-self.xi)/100.0,-(self.ys-self.yi)/40.0, self.direz) xc, yc=rotazione((self.xs-self.xi)/100.0, -(self.ys-self.yi)/40.0, self.direz) xa, ya=xa+self.coordx, ya+self.coordy xb, yb=0+self.coordx, 0+self.coordy xc, yc=xc+self.coordx, yc+self.coordy self.tarta=Polygon(Point(xa,ya), Point(xb,yb), Point(xc,yc)) self.tarta.draw(self) self.tarta.setFill("black") self.tarta_visibile=True def avanti(self,dist): dx=dist*math.sin(math.pi*self.direz/180) dy=dist*math.cos(math.pi*self.direz/180) if self.palla_visibile: self.palla.move(dx,dy) if self.tarta_visibile: self.tarta.move(dx,dy) if not(self.penna_su): LL=Line(Point(self.coordx,self.coordy),Point(self.coordx+dx,self.coordy+dy)) LL.setWidth(self.spessore_penna) LL.draw(self) LL.setFill(self.colore_penna) self.coordx=self.coordx+dx self.coordy=self.coordy+dy def destra(self,angolo): self.direz=(self.direz+angolo) % 360 if self.tarta_visibile: L=self.tarta.getPoints() xa=L[0].getX() ya=L[0].getY() xc=L[2].getX() yc=L[2].getY() xa, ya=xa-self.coordx, ya-self.coordy xc, yc=xc-self.coordx, yc-self.coordy xa, ya=rotazione(xa, ya, angolo) xc, yc=rotazione(xc, yc, angolo) xa, ya=xa+self.coordx, ya+self.coordy xb, yb=0+self.coordx, 0+self.coordy xc, yc=xc+self.coordx, yc+self.coordy self.tarta.undraw() self.tarta=Polygon(Point(xa,ya), Point(xb,yb), Point(xc,yc)) self.tarta.draw(self) self.tarta.setFill("black") def verso(self,x,y): """ restituisce l'angolo di cui deve girare la tarta per dirigersi verso il punto x, y """ dx=float(x-self.coordx) dy=float(y-self.coordy) if (dx<>0) or (dy<>0): if dy==0: if dx<0: return 270 else: return 90 else: if dy>0: return (math.atan(dx/dy)*180/math.pi) % 360 else: return 180+math.atan(dx/dy)*180/math.pi def asDir(self,angolo): """ assegna una Direzione alla tarta; asDir(verso(x,y)) """ self.direz=angolo if self.tarta_visibile: self.tarta.undraw() xa, ya=rotazione(-(self.xs-self.xi)/100.0,-(self.ys-self.yi)/40.0, self.direz) xc, yc=rotazione((self.xs-self.xi)/100.0, -(self.ys-self.yi)/40.0, self.direz) xa, ya=xa+self.coordx, ya+self.coordy xb, yb=0+self.coordx, 0+self.coordy xc, yc=xc+self.coordx, yc+self.coordy self.tarta=Polygon(Point(xa,ya), Point(xb,yb), Point(xc,yc)) self.tarta.draw(self) self.tarta.setFill("black")