1 from __future__
import division
7 __slots__
= 'x', 'y', 'z'
9 def __init__ (self
, x
= 0, y
= 0, z
= 0):
10 self
.x
, self
.y
, self
.z
= x
, y
, z
13 return '(%f, %f, %f)' % (self
.x
, self
.y
, self
.z
)
17 def __add__ (self
, other
):
18 return vector (self
.x
+ other
.x
,
22 def __sub__ (self
, other
):
23 return vector (self
.x
- other
.x
,
27 def __mul__ (self
, other
):
28 return vector (self
.x
* other
,
32 def __truediv__ (self
, other
):
33 return vector (self
.x
/ other
,
39 def __rmul__ (self
, other
):
40 return vector (self
.x
* other
,
44 def __iadd__ (self
, other
):
50 def __isub__ (self
, other
):
56 def __imul__ (self
, other
):
62 def __itruediv__ (self
, other
):
68 __idiv__
= __itruediv__
71 return vector (-self
.x
, -self
.y
, -self
.z
)
81 return sqrt (self
.normsq ())
84 return self
.dot (self
)
86 def normed (self
, norm
):
87 return self
* (norm
/ abs (self
))
89 def dot (self
, other
):
90 return (self
.x
* other
.x
+
94 def cross (self
, other
):
95 return vector (self
.y
* other
.z
- self
.z
* other
.y
,
96 self
.z
* other
.x
- self
.x
* other
.z
,
97 self
.x
* other
.y
- self
.y
* other
.x
)
99 def proj (self
, other
):
100 return other
* self
.dot (other
) / other
.dot (other
)
102 def xrot (self
, angle
):
103 from math
import cos
, sin
104 cosx
, sinx
= cos (angle
), sin (angle
)
105 return vector (self
.x
,
106 cosx
* self
.y
- sinx
* self
.z
,
107 sinx
* self
.y
+ cosx
* self
.z
)
109 def yrot (self
, angle
):
110 from math
import cos
, sin
111 cosy
, siny
= cos (angle
), sin (angle
)
112 return vector (siny
* self
.z
+ cosy
* self
.x
,
114 cosy
* self
.z
- siny
* self
.x
)
116 def zrot (self
, angle
):
117 from math
import cos
, sin
118 cosz
, sinz
= cos (angle
), sin (angle
)
119 return vector (cosz
* self
.x
- sinz
* self
.y
,
120 sinz
* self
.x
+ cosz
* self
.y
,
123 def transform (self
, oval
):
124 # pos = (self - oval.pos).zrot (-oval.angle)
125 pos
= self
- oval
.pos
130 def invert (self
, oval
):
131 x
= self
.x
* oval
.size
.x
132 y
= self
.y
* oval
.size
.y
133 pos
= vector (x
, y
, self
.z
)
134 return oval
.pos
+ pos
135 # return oval.pos + pos.zrot (oval.angle)
141 __slots__
= 'p1', 'p2'
143 def __init__ (self
, p1
= None, p2
= None):
144 self
.p1
= p1
or vector ()
145 self
.p2
= p2
or vector ()
147 def __call__ (self
, t
):
148 return self
.p1
+ t
* self
.dir ()
151 return self
.p2
- self
.p1
155 return vector (-dir.y
, dir.x
)
158 return abs (self
.dir ())
161 cross
= self
.p1
.cross (self
.p2
)
162 return cross
.z
/ self
.length ()
165 cross
= self
.p1
.cross (self
.p2
)
166 return cross
.z
* cross
.z
/ self
.dir ().normsq ()
169 '''Get parameters yielding points on the unit circle.
171 In some sense, finds the intersection(s) of this line
172 with the unit circle. It really only gives the
173 solutions to the equation |p1 + t (p2 - p1)| = 1.'''
175 from math
import sqrt
179 b
= 2 * self
.p1
.dot (dir)
180 c
= self
.p1
.normsq () - 1
181 dis
= b
* b
- 4 * a
* c
186 return (-b
/ (2 * a
),)
188 return ((-b
- sqrt (dis
)) / (2 * a
),
189 (-b
+ sqrt (dis
)) / (2 * a
))
191 def transform (self
, oval
):
192 return line (self
.p1
.transform (oval
),
193 self
.p2
.transform (oval
))
195 def invert (self
, oval
):
196 return line (self
.p1
.invert (oval
),
197 self
.p2
.invert (oval
))
203 __slots__
= 'pos', 'size'
205 def __init__ (self
, pos
= None, size
= None):
206 self
.pos
= pos
or vector ()
207 self
.size
= size
or vector ()
210 return self
.pos
.x
- self
.size
.x
213 return self
.pos
.x
+ self
.size
.x
216 return self
.pos
.y
+ self
.size
.y
219 return self
.pos
.y
- self
.size
.y
222 return 2 * self
.size
.x
225 return 2 * self
.size
.y
227 def set_left (self
, l
):
228 self
.pos
.x
= l
+ self
.size
.x
230 def set_right (self
, r
):
231 self
.pos
.x
= r
- self
.size
.x
233 def set_top (self
, t
):
234 self
.pos
.y
= t
- self
.size
.y
236 def set_bottom (self
, b
):
237 self
.pos
.y
= b
+ self
.size
.y
239 def set_width (self
, w
):
242 def set_height (self
, h
):
247 class ellipse (object):
249 __slots__
= 'pos', 'size', 'angle'
251 def __init__ (self
, pos
= None, size
= None, angle
= 0):
252 self
.pos
= pos
or vector ()
253 self
.size
= size
or vector ()