def bisectors(self): """ The angle bisectors of the triangle in a dictionary where the key is the vertex and the value is the bisector at that point. Example: ======== >>> from sympy.geometry import Point, Triangle, Segment >>> p1,p2,p3 = Point(0,0), Point(1,0), Point(0,1) >>> t = Triangle(p1, p2, p3) >>> from sympy import sqrt >>> t.bisectors[p2] == Segment(Point(0, sqrt(2)-1), Point(1, 0)) True """ s = self.sides v = self.vertices c = self.incenter l1 = Segment(v[0], GeometryEntity.do_intersection(Line(v[0], c), s[1])[0]) l2 = Segment(v[1], GeometryEntity.do_intersection(Line(v[1], c), s[2])[0]) l3 = Segment(v[2], GeometryEntity.do_intersection(Line(v[2], c), s[0])[0]) return {v[0]: l1, v[1]: l2, v[2]: l3}
def bisectors(self): """The angle bisectors of the triangle. An angle bisector of a triangle is a straight line through a vertex which cuts the corresponding angle in half. Returns ------- bisectors : dict Each key is a vertex (Point) and each value is the corresponding bisector (Segment). See Also -------- Point Segment Examples -------- >>> from sympy.geometry import Point, Triangle, Segment >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> from sympy import sqrt >>> t.bisectors[p2] == Segment(Point(0, sqrt(2) - 1), Point(1, 0)) True """ s = self.sides v = self.vertices c = self.incenter l1 = Segment(v[0], GeometryEntity.do_intersection(Line(v[0], c), s[1])[0]) l2 = Segment(v[1], GeometryEntity.do_intersection(Line(v[1], c), s[2])[0]) l3 = Segment(v[2], GeometryEntity.do_intersection(Line(v[2], c), s[0])[0]) return {v[0]: l1, v[1]: l2, v[2]: l3}
def intersection(*entities): """The intersection of a collection of GeometryEntity instances. Parameters ---------- entities : sequence of GeometryEntity Returns ------- intersection : list of GeometryEntity Raises ------ NotImplementedError When unable to calculate intersection. Notes ----- The intersection of any geometrical entity with itself should return a list with one item: the entity in question. An intersection requires two or more entities. If only a single entity is given then the function will return an empty list. It is possible for `intersection` to miss intersections that one knows exists because the required quantities were not fully simplified internally. Reals should be converted to Rationals, e.g. Rational(str(real_num)) or else failures due to floating point issues may result. Examples -------- >>> from sympy.geometry import Point, Line, Circle, intersection >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(-1, 5) >>> l1, l2 = Line(p1, p2), Line(p3, p2) >>> c = Circle(p2, 1) >>> intersection(l1, p2) [Point(1, 1)] >>> intersection(l1, l2) [Point(1, 1)] >>> intersection(c, p2) [] >>> intersection(c, Point(1, 0)) [Point(1, 0)] >>> intersection(c, l2) [Point(1 - 5**(1/2)/5, 1 + 2*5**(1/2)/5), Point(1 + 5**(1/2)/5, 1 - 2*5**(1/2)/5)] """ from entity import GeometryEntity entities = GeometryEntity.extract_entities(entities, False) if len(entities) <= 1: return [] res = GeometryEntity.do_intersection(entities[0], entities[1]) for entity in entities[2:]: newres = [] for x in res: newres.extend(GeometryEntity.do_intersection(x, entity)) res = newres return res
def intersection(self, o): res = [] for side in self.sides: inter = GeometryEntity.do_intersection(side, o) if inter is not None: res.extend(inter) return res
def intersection(self, o): """The intersection of two polygons. The intersection may be empty and can contain individual Points and complete Line Segments. Parameters ---------- other: Polygon Returns ------- intersection : list The list of Segments and Points Examples -------- >>> from sympy import Point, Polygon >>> p1, p2, p3, p4 = map(Point, [(0, 0), (1, 0), (5, 1), (0, 1)]) >>> poly1 = Polygon(p1, p2, p3, p4) >>> p5, p6, p7, p8 = map(Point, [(3, 2), (1, -1), (0, 2), (-2, 1)]) >>> poly2 = Polygon(p5, p6, p7, p8) >>> poly1.intersection(poly2) [Point(2/3, 0), Point(9/5, 1/5), Point(7/3, 1), Point(1/3, 1)] """ res = [] for side in self.sides: inter = GeometryEntity.do_intersection(side, o) if inter is not None: res.extend(inter) return res
def is_concurrent(*lines): """ Returns True if the set of linear entities are concurrent, False otherwise. Two or more linear entities are concurrent if they all intersect at a single point. Description of Method Used: =========================== Simply take the first two lines and find their intersection. If there is no intersection, then the first two lines were parallel and had no intersection so concurrency is impossible amongst the whole set. Otherwise, check to see if the intersection point of the first two lines is a member on the rest of the lines. If so, the lines are concurrent. """ _lines = lines lines = GeometryEntity.extract_entities(lines) # Concurrency requires intersection at a single point; One linear # entity cannot be concurrent. if len(lines) <= 1: return False try: # Get the intersection (if parallel) p = GeometryEntity.do_intersection(lines[0], lines[1]) if len(p) == 0: return False # Make sure the intersection is on every linear entity for line in lines[2:]: if p[0] not in line: return False return True except AttributeError: return False
def perpendicular_segment(self, p): """Create a perpendicular line segment from `p` to this line. Parameters ---------- p : Point Returns ------- segment : Segment Notes ----- Returns `p` itself if `p` is on this linear entity. Examples -------- >>> from sympy import Point, Line >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(0, 2) >>> l1 = Line(p1, p2) >>> s1 = l1.perpendicular_segment(p3) >>> l1.is_perpendicular(s1) True >>> p3 in s1 True """ if p in self: return p pl = self.perpendicular_line(p) p2 = GeometryEntity.do_intersection(self, pl)[0] return Segment(p, p2)
def is_concurrent(*lines): """Is a sequence of linear entities concurrent? Two or more linear entities are concurrent if they all intersect at a single point. Parameters ---------- lines : a sequence of linear entities. Returns ------- True if the set of linear entities are concurrent, False otherwise. Notes ----- Simply take the first two lines and find their intersection. If there is no intersection, then the first two lines were parallel and had no intersection so concurrency is impossible amongst the whole set. Otherwise, check to see if the intersection point of the first two lines is a member on the rest of the lines. If so, the lines are concurrent. Examples -------- >>> from sympy import Point, Line >>> p1, p2 = Point(0, 0), Point(3, 5) >>> p3, p4 = Point(-2, -2), Point(0, 2) >>> l1, l2, l3 = Line(p1, p2), Line(p1, p3), Line(p1, p4) >>> l1.is_concurrent(l2, l3) True >>> l4 = Line(p2, p3) >>> l4.is_concurrent(l2, l3) False """ _lines = lines lines = GeometryEntity.extract_entities(lines) # Concurrency requires intersection at a single point; One linear # entity cannot be concurrent. if len(lines) <= 1: return False try: # Get the intersection (if parallel) p = GeometryEntity.do_intersection(lines[0], lines[1]) if len(p) == 0: return False # Make sure the intersection is on every linear entity for line in lines[2:]: if p[0] not in line: return False return True except AttributeError: return False
def intersection(*entities): """ Finds the intersection between a list GeometryEntity instances. Returns a list of all the intersections, Will raise a NotImplementedError exception if unable to calculate the intersection. Examples: ========= >>> from sympy.geometry import * >>> p1,p2,p3 = Point(0,0), Point(1,1), Point(-1, 5) >>> l1, l2 = Line(p1, p2), Line(p3, p2) >>> c = Circle(p2, 1) >>> intersection(l1, p2) [Point(1, 1)] >>> intersection(l1, l2) [Point(1, 1)] >>> intersection(c, p2) [] >>> intersection(c, Point(1, 0)) [Point(1, 0)] >>> intersection(c, l2) [Point(1 - 1/5*5**(1/2), 1 + 2*5**(1/2)/5), Point(1 + 1/5*5**(1/2), 1 - 2*5**(1/2)/5)] Notes: ====== - The intersection of any geometrical entity with itself should return a list with one item: the entity in question. - An intersection requires two or more entities. If only a single entity is given then one will receive an empty intersection list. - It is possible for intersection() to miss intersections that one knows exists because the required quantities were not fully simplified internally. """ from entity import GeometryEntity entities = GeometryEntity.extract_entities(entities, False) if len(entities) <= 1: return [] res = GeometryEntity.do_intersection(entities[0], entities[1]) for entity in entities[2:]: newres = [] for x in res: newres.extend( GeometryEntity.do_intersection(x, entity) ) res = newres return res
def intersection(*entities): """ Finds the intersection between a list GeometryEntity instances. Returns a list of all the intersections, Will raise a NotImplementedError exception if unable to calculate the intersection. Examples: ========= >>> from sympy.geometry import * >>> p1,p2,p3 = Point(0,0), Point(1,1), Point(-1, 5) >>> l1, l2 = Line(p1, p2), Line(p3, p2) >>> c = Circle(p2, 1) >>> intersection(l1, p2) [Point(1, 1)] >>> intersection(l1, l2) [Point(1, 1)] >>> intersection(c, p2) [] >>> intersection(c, Point(1, 0)) [Point(1, 0)] >>> intersection(c, l2) [Point(1 - 5**(1/2)/5, 1 + 2*5**(1/2)/5), Point(1 + 5**(1/2)/5, 1 - 2*5**(1/2)/5)] Notes: ====== - The intersection of any geometrical entity with itself should return a list with one item: the entity in question. - An intersection requires two or more entities. If only a single entity is given then one will receive an empty intersection list. - It is possible for intersection() to miss intersections that one knows exists because the required quantities were not fully simplified internally. """ from entity import GeometryEntity entities = GeometryEntity.extract_entities(entities, False) if len(entities) <= 1: return [] res = GeometryEntity.do_intersection(entities[0], entities[1]) for entity in entities[2:]: newres = [] for x in res: newres.extend( GeometryEntity.do_intersection(x, entity) ) res = newres return res
def bisectors(self): """ The angle bisectors of the triangle in a dictionary where the key is the vertex and the value is the bisector at that point. Example: ======== >>> p1,p2,p3 = Point(0,0), Point(1,0), Point(0,1) >>> t = Triangle(p1, p2, p3) >>> t.bisectors[p2] Segment(Point(0, (-1) + 2**(1/2)), Point(1, 0)) """ s = self.sides v = self.vertices c = self.incenter l1 = Segment(v[0], GeometryEntity.do_intersection(Line(v[0], c), s[1])[0]) l2 = Segment(v[1], GeometryEntity.do_intersection(Line(v[1], c), s[2])[0]) l3 = Segment(v[2], GeometryEntity.do_intersection(Line(v[2], c), s[0])[0]) return {v[0]: l1, v[1]: l2, v[2]: l3}
def perpendicular_segment(self, p): """ Returns a new Segment which connects p to a point on this linear entity and is also perpendicular to this line. Returns p itself if p is on this linear entity. """ if p in self: return p pl = self.perpendicular_line(p) p2 = GeometryEntity.do_intersection(self, pl)[0] return Segment(p, p2)
def projection(self, o): """ Project a point, line, ray, or segment onto this linear entity. If projection cannot be performed then a GeometryError is raised. Notes: ====== - A projection involves taking the two points that define the linear entity and projecting those points onto a Line and then reforming the linear entity using these projections. - A point P is projected onto a line L by finding the point on L that is closest to P. This is done by creating a perpendicular line through P and L and finding its intersection with L. """ tline = Line(self.p1, self.p2) def project(p): """Project a point onto the line representing self.""" if p in tline: return p l1 = tline.perpendicular_line(p) return tline.intersection(l1)[0] projected = None if isinstance(o, Point): return project(o) elif isinstance(o, LinearEntity): n_p1 = project(o.p1) n_p2 = project(o.p2) if n_p1 == n_p2: projected = n_p1 else: projected = o.__class__(n_p1, n_p2) # Didn't know how to project so raise an error if projected is None: n1 = self.__class__.__name__ n2 = o.__class__.__name__ raise GeometryError("Do not know how to project %s onto %s" % (n2, n1)) return GeometryEntity.do_intersection(self, projected)[0]
def circumcenter(self): """The circumcenter of the triangle The circumcenter is the center of the circumcircle. Returns ------- circumcenter : Point See Also -------- Point Examples -------- >>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.circumcenter Point(1/2, 1/2) """ a,b,c = [x.perpendicular_bisector() for x in self.sides] return GeometryEntity.do_intersection(a, b)[0]
def circumcenter(self): """The circumcenter of the triangle The circumcenter is the center of the circumcircle. Returns ------- circumcenter : Point See Also -------- Point Examples -------- >>> from sympy.geometry import Point, Triangle >>> p1, p2, p3 = Point(0, 0), Point(1, 0), Point(0, 1) >>> t = Triangle(p1, p2, p3) >>> t.circumcenter Point(1/2, 1/2) """ a, b, c = [x.perpendicular_bisector() for x in self.sides] return GeometryEntity.do_intersection(a, b)[0]
def projection(self, o): """Project a point, line, ray, or segment onto this linear entity. Parameters ---------- other : Point or LinearEntity (Line, Ray, Segment) Returns ------- projection : Point or LinearEntity (Line, Ray, Segment) The return type matches the type of the parameter `other`. Raises ------ GeometryError When method is unable to perform projection. See Also -------- Point Notes ----- A projection involves taking the two points that define the linear entity and projecting those points onto a Line and then reforming the linear entity using these projections. A point P is projected onto a line L by finding the point on L that is closest to P. This is done by creating a perpendicular line through P and L and finding its intersection with L. Examples -------- >>> from sympy import Point, Line, Segment, Rational >>> p1, p2, p3 = Point(0, 0), Point(1, 1), Point(Rational(1, 2), 0) >>> l1 = Line(p1, p2) >>> l1.projection(p3) Point(1/4, 1/4) >>> p4, p5 = Point(10, 0), Point(12, 1) >>> s1 = Segment(p4, p5) >>> l1.projection(s1) Segment(Point(5, 5), Point(13/2, 13/2)) """ tline = Line(self.p1, self.p2) def project(p): """Project a point onto the line representing self.""" if p in tline: return p l1 = tline.perpendicular_line(p) return tline.intersection(l1)[0] projected = None if isinstance(o, Point): return project(o) elif isinstance(o, LinearEntity): n_p1 = project(o.p1) n_p2 = project(o.p2) if n_p1 == n_p2: projected = n_p1 else: projected = o.__class__(n_p1, n_p2) # Didn't know how to project so raise an error if projected is None: n1 = self.__class__.__name__ n2 = o.__class__.__name__ raise GeometryError("Do not know how to project %s onto %s" % (n2, n1)) return GeometryEntity.do_intersection(self, projected)[0]
def circumcenter(self): """The circumcenter of the triangle.""" a,b,c = [x.perpendicular_bisector() for x in self.sides] return GeometryEntity.do_intersection(a, b)[0]