# - - - L i n e . t w o P o i n t @staticmethod def twoPoint(p1, p2): '''Find the equation of a line between two points. '''
The first order of business is to eliminate the case
where the points coincide; see Section 7.12, “
Pt.__cmp__(): Compare two points”.
#-- 1 -- # [ if p1 and p2 coincide -> # raise ValueError # else -> # x1 := abscissa of p1 # y1 := ordinate of p1 # x2 := abscissa of p2 # y2 := ordinate of p2 ] if p1 == p2: raise ValueError("Points are not distinct.") else: x1, y1 = p1.xy() x2, y2 = p2.xy()
Here is the formula for a line through two points:
Notice that for vertical lines
x2 = x1, which would require dividing by zero.
In this case, a vertical line through
x1 has the
x1, which in
standard form would be:
If the line is not vertical, we calculate the slope
#-- 2 -- # [ if x1 == x2 -> # return a vertical line through x1 # else -> # m := (y2-y1)/(x2-x1) ] if x1 == x2: return Line(1.0, 0.0, -x1) else: m = (y2-y1)/(x2-x1)
m in the
two-point formula, we rearrange the terms into the
ax + by + c = 0.
#-- 4 -- # [ return a new Line instance having a=(-m), b=1, and # c=(m*x1-y1) ] return Line(-m, 1.0, (m*x1-y1))