-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
point.py
585 lines (432 loc) · 14.4 KB
/
point.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
"""Geometrical Points.
Contains
========
Point
"""
from ..core import Add, Float, Integer, Tuple
from ..core.compatibility import iterable
from ..core.evaluate import global_evaluate
from ..core.sympify import sympify
from ..functions import im, sqrt
from ..matrices import Matrix
from ..simplify import nsimplify, simplify
from ..utilities import ordered
from .entity import GeometryEntity
class Point(GeometryEntity):
"""A point in a n-dimensional Euclidean space.
Parameters
==========
coords : sequence of n-coordinate values.
Raises
======
TypeError
When trying to add or subtract points with different dimensions.
When `intersection` is called with object other than a Point.
See Also
========
diofant.geometry.line.Segment : Connects two Points
Examples
========
>>> Point([1, 2])
Point(1, 2)
>>> Point(0, x)
Point(0, x)
Floats are automatically converted to Rational unless the
evaluate flag is False:
>>> Point(0.5, 0.25)
Point(1/2, 1/4)
>>> print(Point(0.5, 0.25, evaluate=False))
Point(0.5, 0.25)
"""
def __new__(cls, *args, **kwargs):
evaluate = kwargs.get('evaluate', global_evaluate[0])
if iterable(args[0]):
args = args[0]
# unpack the arguments into a friendly Tuple
# if we were already a Point, we're doing an excess
# iteration, but we'll worry about efficiency later
coords = Tuple(*args)
if any(a.is_number and im(a) for a in coords):
raise ValueError('Imaginary coordinates not permitted.')
# Turn any Floats into rationals and simplify
# any expressions before we instantiate
if evaluate:
coords = coords.xreplace({f: simplify(nsimplify(f, rational=True))
for f in coords.atoms(Float)})
if len(coords) != 2:
raise ValueError('Only two dimensional points currently supported')
return GeometryEntity.__new__(cls, *coords)
def __contains__(self, item):
return item == self
def is_concyclic(*points):
"""Is a sequence of points concyclic?
Test whether or not a sequence of points are concyclic (i.e., they lie
on a circle).
Parameters
==========
points : sequence of Points
Returns
=======
is_concyclic : boolean
True if points are concyclic, False otherwise.
See Also
========
diofant.geometry.ellipse.Circle
Notes
=====
No points are not considered to be concyclic. One or two points
are definitely concyclic and three points are conyclic iff they
are not collinear.
For more than three points, create a circle from the first three
points. If the circle cannot be created (i.e., they are collinear)
then all of the points cannot be concyclic. If the circle is created
successfully then simply check the remaining points for containment
in the circle.
Examples
========
>>> p1, p2 = Point(-1, 0), Point(1, 0)
>>> p3, p4 = Point(0, 1), Point(-1, 2)
>>> Point.is_concyclic(p1, p2, p3)
True
>>> Point.is_concyclic(p1, p2, p3, p4)
False
"""
if not all(isinstance(p, Point) for p in points):
raise TypeError('Must pass only Point objects')
if len(points) == 0:
return False
if len(points) <= 2:
return True
ppoints = list(ordered(Point(p) for p in points))
from .ellipse import Circle
try:
c = Circle(ppoints[0], ppoints[1], ppoints[2])
except ValueError:
return False
for point in ppoints[3:]:
if point not in c:
return False
return True
def is_collinear(*args):
"""Is a sequence of points collinear?
Test whether or not a set of points are collinear. Returns True if
the set of points are collinear, or False otherwise.
Parameters
==========
points : sequence of Point
Returns
=======
is_collinear : boolean
Notes
=====
Slope is preserved everywhere on a line, so the slope between
any two points on the line should be the same. Take the first
two points, p1 and p2, and create a translated point v1
with p1 as the origin. Now for every other point we create
a translated point, vi with p1 also as the origin. Note that
these translations preserve slope since everything is
consistently translated to a new origin of p1. Since slope
is preserved then we have the following equality:
* v1_slope = vi_slope
* v1.y/v1.x = vi.y/vi.x (due to translation)
* v1.y*vi.x = vi.y*v1.x
* v1.y*vi.x - vi.y*v1.x = 0 (*)
Hence, if we have a vi such that the equality in (*) is False
then the points are not collinear. We do this test for every
point in the list, and if all pass then they are collinear.
See Also
========
diofant.geometry.line.Line
Examples
========
>>> p1, p2 = Point(0, 0), Point(1, 1)
>>> p3, p4, p5 = Point(2, 2), Point(x, x), Point(1, 2)
>>> Point.is_collinear(p1, p2, p3, p4)
True
>>> Point.is_collinear(p1, p2, p3, p5)
False
"""
# Coincident points are irrelevant; use only unique points.
uniq_args = list(set(args))
if not all(isinstance(p, Point) for p in uniq_args):
raise TypeError('Must pass only Point objects')
if len(uniq_args) == 0:
return False
if len(uniq_args) <= 2:
return True
# translate our points
points = [p - uniq_args[0] for p in uniq_args[1:]]
for p in points[1:]:
if not Point.is_scalar_multiple(points[0], p):
return False
return True
def is_scalar_multiple(self, other):
"""Returns whether `self` and `other` are scalar multiples
of each other.
"""
# if the vectors self and other are linearly dependent, then they must
# be scalar multiples of each other
m = Matrix([self.args, other.args])
return m.rank() < 2
@property
def length(self):
"""
Treating a Point as a Line, this returns 0 for the length of a Point.
Examples
========
>>> p = Point(0, 1)
>>> p.length
0
"""
return Integer(0)
@property
def origin(self):
"""A point of all zeros of the same ambient dimension
as the current point
"""
return Point([0]*len(self))
@property
def is_zero(self):
"""True if every coordinate is zero, otherwise False."""
return all(x == 0 for x in self.args)
@property
def ambient_dimension(self):
"""The dimension of the ambient space the point is in.
I.e., if the point is in R^n, the ambient dimension
will be n
"""
return len(self)
def distance(self, p):
"""The Euclidean distance from self to point p.
Parameters
==========
p : Point
Returns
=======
distance : number or symbolic expression.
See Also
========
diofant.geometry.line.Segment.length
Examples
========
>>> p1, p2 = Point(1, 1), Point(4, 5)
>>> p1.distance(p2)
5
>>> p3 = Point(x, y)
>>> p3.distance(Point(0, 0))
sqrt(x**2 + y**2)
"""
p = Point(p)
return sqrt(sum((a - b)**2
for a, b in zip(self.args,
p.args if isinstance(p, Point) else p)))
def midpoint(self, p):
"""The midpoint between self and point p.
Parameters
==========
p : Point
Returns
=======
midpoint : Point
See Also
========
diofant.geometry.line.Segment.midpoint
Examples
========
>>> p1, p2 = Point(1, 1), Point(13, 5)
>>> p1.midpoint(p2)
Point(7, 3)
"""
return Point([simplify((a + b)/2) for a, b in zip(self.args, p.args)])
def evalf(self, dps=15, **options):
"""Evaluate the coordinates of the point.
This method will, where possible, create and return a new Point
where the coordinates are evaluated as floating point numbers to
the decimal precision dps.
Returns
=======
point : Point
Examples
========
>>> p1 = Point(Rational(1, 2), Rational(3, 2))
>>> p1
Point(1/2, 3/2)
>>> print(p1.evalf())
Point(0.5, 1.5)
"""
coords = [x.evalf(dps, **options) for x in self.args]
return Point(*coords, evaluate=False)
n = evalf
def intersection(self, o):
"""The intersection between this point and another point.
Parameters
==========
other : Point
Returns
=======
intersection : list of Points
Notes
=====
The return value will either be an empty list if there is no
intersection, otherwise it will contain this point.
Examples
========
>>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(0, 0)
>>> p1.intersection(p2)
[]
>>> p1.intersection(p3)
[Point(0, 0)]
"""
if isinstance(o, Point):
if self == o:
return [self]
return []
return o.intersection(self)
def dot(self, p2):
"""Return dot product of self with another Point."""
p2 = Point(p2)
return Add(*[a*b for a, b in zip(self, p2)])
def __len__(self):
return len(self.args)
def __iter__(self):
return self.args.__iter__()
def __add__(self, other):
"""Add other to self by incrementing self's coordinates by those of other.
See Also
========
diofant.geometry.entity.GeometryEntity.translate
"""
if iterable(other) and len(other) == len(self):
return Point([simplify(a + b) for a, b in zip(self, other)])
else:
raise ValueError(
'Points must have the same number of dimensions')
def __sub__(self, other):
"""Subtract two points, or subtract a factor from this point's
coordinates.
"""
return self + (-other)
def __mul__(self, factor):
"""Multiply point's coordinates by a factor."""
factor = sympify(factor)
return Point([simplify(x*factor) for x in self.args])
def __truediv__(self, divisor):
"""Divide point's coordinates by a factor."""
divisor = sympify(divisor)
return Point([simplify(x/divisor) for x in self.args])
def __neg__(self):
"""Negate the point."""
return Point([-x for x in self.args])
def __abs__(self):
"""Returns the distance between this point and the origin."""
origin = Point([0]*len(self))
return Point.distance(origin, self)
@property
def x(self):
"""
Returns the X coordinate of the Point.
Examples
========
>>> p = Point(0, 1)
>>> p.x
0
"""
return self.args[0]
@property
def y(self):
"""
Returns the Y coordinate of the Point.
Examples
========
>>> p = Point(0, 1)
>>> p.y
1
"""
return self.args[1]
@property
def bounds(self):
"""Return a tuple (xmin, ymin, xmax, ymax) representing the bounding
rectangle for the geometric figure.
"""
return self.x, self.y, self.x, self.y
def rotate(self, angle, pt=None):
"""Rotate ``angle`` radians counterclockwise about Point ``pt``.
See Also
========
rotate, scale
Examples
========
>>> t = Point(1, 0)
>>> t.rotate(pi/2)
Point(0, 1)
>>> t.rotate(pi/2, (2, 0))
Point(2, -1)
"""
from ..functions import cos, sin
c = cos(angle)
s = sin(angle)
rv = self
if pt is not None:
pt = Point(pt)
rv -= pt
x, y = rv.args
rv = Point(c*x - s*y, s*x + c*y)
if pt is not None:
rv += pt
return rv
def scale(self, x=1, y=1, pt=None):
"""Scale the coordinates of the Point by multiplying by
``x`` and ``y`` after subtracting ``pt`` -- default is (0, 0) --
and then adding ``pt`` back again (i.e. ``pt`` is the point of
reference for the scaling).
See Also
========
rotate, translate
Examples
========
>>> t = Point(1, 1)
>>> t.scale(2)
Point(2, 1)
>>> t.scale(2, 2)
Point(2, 2)
"""
if pt:
pt = Point(pt)
return self.translate(*(-pt).args).scale(x, y).translate(*pt.args)
return Point(self.x*x, self.y*y)
def translate(self, x=0, y=0):
"""Shift the Point by adding x and y to the coordinates of the Point.
See Also
========
rotate, scale
Examples
========
>>> t = Point(0, 1)
>>> t.translate(2)
Point(2, 1)
>>> t.translate(2, 2)
Point(2, 3)
>>> t + Point(2, 2)
Point(2, 3)
"""
return Point(self.x + x, self.y + y)
def transform(self, matrix):
"""Return the point after applying the transformation described
by the 3x3 Matrix, ``matrix``.
See Also
========
diofant.geometry.entity.GeometryEntity.rotate
diofant.geometry.entity.GeometryEntity.scale
diofant.geometry.entity.GeometryEntity.translate
"""
try:
col, _ = matrix.shape
valid_matrix = matrix.is_square and col == 3
except AttributeError:
# We hit this block if matrix argument is not actually a Matrix.
valid_matrix = False
if not valid_matrix:
raise ValueError('The argument to the transform function must be '
+ 'a 3x3 matrix')
x, y = self.args
return Point(*(Matrix(1, 3, [x, y, 1])*matrix).tolist()[0][:2])