# Version 1 # watch out for the file path setting in the Povray class in the graphics.py # module, which is pre-coded to point to an existing directory from math import cos,sin,pi,acos,asin,atan,tan import graphics from graphics import Povray # constant rad2deg = 180/pi # convert radians to degrees deg2rad = pi/180 # convert degrees to radians class Vector(graphics.Vector): """ Inherits from previous version in graphics.py but adds new methods based on trig functions """ def angle(self, other): """ angle between this and another vector in degrees (to 4 decimal digits) """ dab = self.dot(other) ab = self.length() * other.length() return round ( acos(dab/ab) * rad2deg, 4 ) def rotx(self, degs): """ rotate self around the x axis by deg degrees, return new vector """ t = degs * deg2rad newy = self.y * cos(t) - self.z * sin(t) newz = self.y * sin(t) + self.z * cos(t) return Vector(self.x, newy, newz) def roty(self, degs): """ rotate self around the y axis by deg degrees, return new vector """ t = degs * deg2rad newx = self.x * cos(t) - self.z * sin(t) newz = self.x * sin(t) + self.z * cos(t) return Vector(newx, self.y, newz) def rotz(self, degs): """ rotate self around the z axis by deg degrees, return new vector """ t = degs * deg2rad newx = self.x * cos(t) - self.y * sin(t) newy = self.x * sin(t) + self.y * cos(t) return Vector(newx, newy, self.z) def test(): """ Draw a pentagon by rotating a vector around the circle and connecting the tips """ p = Povray("pentagon.pov") p.sphradius = 0.14 # bigger spheres p.sphcolor = 'Blue' # make them blue v0 = Vector(4,0,0) # point out to the right v1 = v0.rotz(72) # sweep 72 degrees to new vector p.edge(v0,v1) # ... and connect the two tips p.point(v0) v0, v1 = v1, v1.rotz(72) # rotate another 72 degrees p.edge(v0,v1) # connect previous to new vector p.point(v0) v0, v1 = v1, v1.rotz(72) # and so on p.edge(v0,v1) p.point(v0) v0, v1 = v1, v1.rotz(72) p.edge(v0,v1) p.point(v0) v0, v1 = v1, v1.rotz(72) # do it 5 times (5 * 72 = 360) p.edge(v0,v1) p.point(v0) p.close() def ngon(n): p = Povray("ngon.pov") p.sphradius = 0.14 # bigger spheres p.sphcolor = 'Blue' # make them blue sweep = 360.0/n v0 = Vector(4,0,0) # point out to the right v1 = v0.rotz(sweep) # sweep to new vector for i in range(n): p.edge(v0,v1) # ... and connect the two tips p.point(v0) v0, v1 = v1, v1.rotz(sweep) # and so on