def rotate(self, angle, pt=None): """Rotate ``angle`` radians counterclockwise about Point ``pt``. See Also ======== rotate, scale Examples ======== >>> from diofant import Point2D, pi >>> t = Point2D(1, 0) >>> t.rotate(pi/2) Point2D(0, 1) >>> t.rotate(pi/2, (2, 0)) Point2D(2, -1) """ from diofant import cos, sin, Point 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 ======== >>> from diofant import Point2D >>> t = Point2D(1, 1) >>> t.scale(2) Point2D(2, 1) >>> t.scale(2, 2) Point2D(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 are_collinear(*points): """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 ======= are_collinear : boolean See Also ======== diofant.geometry.line3d.Line3D Examples ======== >>> from diofant import Point3D, Matrix >>> from diofant.abc import x >>> p1, p2 = Point3D(0, 0, 0), Point3D(1, 1, 1) >>> p3, p4, p5 = Point3D(2, 2, 2), Point3D(x, x, x), Point3D(1, 2, 6) >>> Point3D.are_collinear(p1, p2, p3, p4) True >>> Point3D.are_collinear(p1, p2, p3, p5) False """ return Point.is_collinear(*points)
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 ======== >>> from diofant.geometry import Point >>> p1, p2 = Point(1, 1), Point(13, 5) >>> p1.midpoint(p2) Point2D(7, 3) """ return Point([simplify((a + b)*S.Half) for a, b in zip(self.args, p.args)])
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 ======== >>> from diofant.geometry import Point >>> p1, p2 = Point(1, 1), Point(4, 5) >>> p1.distance(p2) 5 >>> from diofant.abc import x, y >>> 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)))
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 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, row = 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])
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 ======== >>> from diofant import Point2D >>> t = Point2D(0, 1) >>> t.translate(2) Point2D(2, 1) >>> t.translate(2, 2) Point2D(2, 3) >>> t + Point2D(2, 2) Point2D(2, 3) """ return Point(self.x + x, self.y + y)
def evalf(self, prec=None, **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 precision indicated (default=15). Returns ======= point : Point Examples ======== >>> from diofant import Point, Rational >>> p1 = Point(Rational(1, 2), Rational(3, 2)) >>> p1 Point2D(1/2, 3/2) >>> print(p1.evalf()) Point2D(0.5, 1.5) """ coords = [x.evalf(prec, **options) for x in self.args] return Point(*coords, evaluate=False)
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 ======== >>> from diofant.geometry import Point >>> 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 len(points) == 0: return False if len(points) <= 2: return True points = [Point(p) for p in points] if len(points) == 3: return (not Point.is_collinear(*points)) try: from .ellipse import Circle c = Circle(points[0], points[1], points[2]) for point in points[3:]: if point not in c: return False return True except GeometryError: # Circle could not be created, because of collinearity of the # three points passed in, hence they are not concyclic. return False
def __abs__(self): """Returns the distance between this point and the origin.""" origin = Point([0]*len(self)) return Point.distance(origin, self)
def __neg__(self): """Negate the point.""" return Point([-x for x in self.args])
def __div__(self, divisor): """Divide point's coordinates by a factor.""" divisor = sympify(divisor) return Point([simplify(x/divisor) for x in self.args])
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 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 origin(self): """A point of all zeros of the same ambient dimension as the current point""" return Point([0]*len(self))