Skip to content

Commit

Permalink
Merge pull request #1332 from smichr/geom
Browse files Browse the repository at this point in the history
geometry: perpendicular segment and orthocenter corrections
  • Loading branch information
smichr committed Jun 7, 2012
2 parents 12c4ead + fbbc724 commit 3920a39
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
10 changes: 8 additions & 2 deletions sympy/geometry/line.py
Expand Up @@ -399,7 +399,7 @@ def perpendicular_line(self, p):
"""
d1, d2 = (self.p1 - self.p2).args
if d2 == 0: # If an horizontal line
if d2 == 0: # If a horizontal line
if p.y == self.p1.y: # if p is on this linear entity
return Line(p, p + Point(0, 1))
else:
Expand All @@ -412,6 +412,10 @@ def perpendicular_line(self, p):
def perpendicular_segment(self, p):
"""Create a perpendicular line segment from `p` to this line.
The enpoints of the segment are ``p`` and the closest point in
the line containing self. (If self is not a line, the point might
not be in self.)
Parameters
==========
Expand Down Expand Up @@ -443,12 +447,14 @@ def perpendicular_segment(self, p):
True
>>> p3 in s1
True
>>> l1.perpendicular_segment(Point(4, 0))
Segment(Point(2, 2), Point(4, 0))
"""
if p in self:
return p
pl = self.perpendicular_line(p)
p2 = self.intersection(pl)[0]
p2 = Line(self).intersection(pl)[0]
return Segment(p, p2)

@property
Expand Down
7 changes: 4 additions & 3 deletions sympy/geometry/polygon.py
Expand Up @@ -1824,8 +1824,9 @@ def is_right(self):
def altitudes(self):
"""The altitudes of the triangle.
An altitude of a triangle is a straight line through a vertex and
perpendicular to the opposite side.
An altitude of a triangle is a segment through a vertex,
perpendicular to the opposite side, with length being the
height of the vertex measured from the line containing the side.
Returns
=======
Expand Down Expand Up @@ -1884,7 +1885,7 @@ def orthocenter(self):
"""
a = self.altitudes
v = self.vertices
return a[v[0]].intersection(a[v[1]])[0]
return Line(a[v[0]]).intersection(Line(a[v[1]]))[0]

@property
def circumcenter(self):
Expand Down
8 changes: 8 additions & 0 deletions sympy/geometry/tests/test_geometry.py
Expand Up @@ -776,6 +776,14 @@ def test_polygon():
assert altitudes[p2] == s1[0]
assert altitudes[p3] == s1[2]
assert t1.orthocenter == p1
t = S('''Triangle(
Point(100080156402737/5000000000000, 79782624633431/500000000000),
Point(39223884078253/2000000000000, 156345163124289/1000000000000),
Point(31241359188437/1250000000000, 338338270939941/1000000000000000))''')
assert t.orthocenter == S('''Point(-780660869050599840216997'''
'''79471538701955848721853/80368430960602242240789074233100000000000000,'''
'''20151573611150265741278060334545897615974257/16073686192120448448157'''
'''8148466200000000000)''')

# Ensure
assert len(intersection(*bisectors.values())) == 1
Expand Down

0 comments on commit 3920a39

Please sign in to comment.