[Edu-sig] Design patterns
Kirby Urner
urnerk at qwest.net
Sun Aug 21 21:27:11 CEST 2005
> PyGeo has a Triangle object, inherited from the Plane object. It exists
> mostly for drawing purposes, as the portion of the plane enclosed by the
> infinite lines connecting any 3 points. Since all Triangles are
> projectively equivalent, in the context of PyGeo there is little to be
> gained by saying much of anything about any particular triangle - and
> little
> is.
>
>
> Art
Good point about all triangles being equivalent given projection. In
nailing down the angles, we've inadvertently defined a fourth vertex: the
point of view. Given we're talking four vertices, we should maybe rename
our class Tetrahedron ;-D.
A transformation that has no impact on angles, is scaling. A feature I'd
like to add to the Triangle sub/class is scalability -- using operator
overloading why not?
Here a design question is: should mytri * 3 change mytri, or should it
return a new object. My preference is for the latter, since we can always
rebind mytri to the output of __mul__. Should I also define __div__ while
I'm at it?
class Triangle2(BaseTriangle):
def _reset(self):
self.perimeter = self.a + self.b + self.c
s = 0.5 * self.perimeter
self.area = math.sqrt(s * (s - self.a)
*(s - self.b) * (s - self.c))
self.A = math.acos((-self.a**2 + self.b**2 + self.c**2)
/ (2.0 * self.b * self.c))
self.B = math.acos((self.a**2 - self.b**2 + self.c**2)
/ (2.0 * self.a * self.c))
self.C = math.acos((self.a**2 + self.b**2 - self.c**2)
/ (2.0 * self.a * self.b))
def __mul__(self, scalar):
a = self.a * scalar
b = self.b * scalar
c = self.c * scalar
return Triangle2(a,b,c)
__rmul__ = __mul__
>>> reload(trig)
<module 'trig' from 'D:\Python24\lib\site-packages\trig.py'>
>>> from trig import Triangle2 as Tri
>>> mytri = Tri(3,4,5)
>>> mytri = mytri * 3
>>> mytri.area
54.0
>>> mytri.a
9
>>> math.degrees(mytri.C)
90.0
Other enhancements:
I could add xyz coordinates as read-only, but have translation, rotation and
scale methods (this last already defined) make them change.
Having xyz coordinates will allow me to feed triangle objects to draw
objects, e.g. for output to VPython, POV-Ray, VRML or what-have-we.
Kirby
More information about the Edu-sig
mailing list