shapes.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 '''
3 Some simple geometry shape helpers.
4 '''
5 
6 class Point(object):
7  '''
8  A N-D Cartesian point.
9 
10  It tries to acts as a collection and a number.
11  '''
12 
13  def __init__(self, *coords):
14  self._coords = list(coords)
15 
16  def __str__(self):
17  s = ",".join([str(a) for a in self._coords])
18  return "Point(%s)" % s
19 
20  def __repr__(self):
21  return str(self)
22 
23  def __len__(self):
24  return len(self._coords)
25 
26  def __getitem__(self, key):
27  return self._coords[key]
28 
29  def __setitem__(self, key, val):
30  self._coords[key] = val
31 
32  def __iter__(self):
33  return self._coords.__iter__()
34 
35  def __abs__(self):
36  return Point(*[abs(a) for a in self])
37 
38  def __sub__(self, other):
39  try:
40  return Point(*[(a-b) for a,b in zip(self, other)])
41  except TypeError:
42  return Point(*[(a-other) for a in self])
43 
44  def __add__(self, other):
45  try:
46  return Point(*[(a+b) for a,b in zip(self, other)])
47  except TypeError:
48  return Point(*[(a+other) for a in self])
49 
50  def __mul__(self, other):
51  try:
52  return Point(*[(a*b) for a,b in zip(self, other)])
53  except TypeError:
54  return Point(*[(a*other) for a in self])
55 
56  def __div__(self, other):
57  try:
58  return Point(*[(a/b) for a,b in zip(self, other)])
59  except TypeError:
60  return Point(*[(a/other) for a in self])
61 
62  def dot(self, other):
63  return sum([a*b for a,b in zip(self, other)])
64 
65  @property
66  def magnitude(self):
67  return math.sqrt(self.dot(self))
68 
69  @property
70  def unit(self):
71  mag = self.magnitude
72  return self/mag
73 
74 class Point2D(Point):
75 
76  @property
77  def x(self):
78  return self[0]
79  @x.setter
80  def x(self, val):
81  self[0] = val
82 
83  @property
84  def y(self):
85  return self[1]
86  @y.setter
87  def y(self, val):
88  self[1] = val
89 
91 
92  @property
93  def z(self):
94  return self[2]
95  @z.setter
96  def z(self, val):
97  self[2] = val
98 
99 
100 
101 
102 class Ray(object):
103  '''
104  A pair of N-D vectors, each represented by an N-D Point
105  '''
106  def __init__(self, tail, head):
107  self.tail = tail
108  self.head = head
109 
110  def __str__(self):
111  return "%s -> %s" % (self.tail, self.head)
112 
113  def __repr__(self):
114  return str(self)
115 
116  @property
117  def vector(self):
118  return self.head - self.tail
119 
120  @property
121  def unit(self):
122  return self.vector.unit
123 
124 
125 class Rectangle2D(object):
126  '''
127  An 2D rectangle
128  '''
129  def __init__(self, width, height, center = Point2D(0.0, 0.0)):
130  self.width = width
131  self.height = height
132  self.center = center
133 
134  @property
135  def ll(self):
136  return Point(self.center.x - 0.5*self.width,
137  self.center.y - 0.5*self.height);
138 
139  def relative(self, point2d):
140  return point2d - self.center
141 
142  def inside(self, point2d):
143  r = self.relative(point2d)
144  return abs(r.x) <= 0.5*self.width and abs(r.y) <= 0.5*self.height
145 
146  def toedge(self, point, direction):
147  '''
148  Return a vector that takes point along direction to the nearest edge.
149  '''
150  p1 = self.relative(point)
151  d1 = direction.unit
152 
153  #print "toedge: p1:%s d1:%s" % (p1, d1)
154 
155  xdir = d1.dot((1.0, 0.0)) # cos(theta_x)
156  xsign = xdir/abs(xdir)
157  ydir = d1.dot((0.0, 1.0)) # cos(theta_y)
158  ysign = ydir/abs(ydir)
159 
160  corn = Point(0.5*self.width, 0.5*self.height)
161 
162  dx = xsign*corn.x - p1.x
163  dy = ysign*corn.y - p1.y
164 
165  tx = dx/d1.x
166  ty = dy/d1.y
167 
168 
169  if tx < ty: # closer to vertical side
170  jump = tx
171  else:
172  jump = ty
173 
174  return d1 * jump
175 
176 
def __sub__(self, other)
Definition: shapes.py:38
def relative(self, point2d)
Definition: shapes.py:139
def dot(self, other)
Definition: shapes.py:62
def __init__(self, coords)
Definition: shapes.py:13
def __init__(self, width, height, center=Point2D(0.0, 0.0))
Definition: shapes.py:129
def __div__(self, other)
Definition: shapes.py:56
def __setitem__(self, key, val)
Definition: shapes.py:29
def __add__(self, other)
Definition: shapes.py:44
T abs(T value)
def __getitem__(self, key)
Definition: shapes.py:26
def __mul__(self, other)
Definition: shapes.py:50
def toedge(self, point, direction)
Definition: shapes.py:146
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
static QCString str
def __init__(self, tail, head)
Definition: shapes.py:106