#! /usr/bin/env python import math class Point: def __init__(self, x, y): self.x = x self.y = y def __repr__(self): return str((self.x, self.y)) class Segment: def __init__(self, p1, p2): self.start = p1 self.end = p2 self.length = getDist(p1, p2) def __repr__(self): return str((self.start, self.end, self.length)) def isOn(self, p): if getDist(p, self.start) + getDist(p, self.end) == self.length: return True else: return False def getDist(p1, p2): return math.sqrt((p1.x-p2.x)**2 + (p1.y-p2.y)**2) def main(): p1 = Point(-1, -1) p2 = Point(1, 1) p3 = Point(0, 0) p4 = Point(0, 1) line = Segment(p1, p2) print 'Is ', p3, ' on line ', line, ':', line.isOn(p3) print 'Is ', p4, ' on line ', line, ':', line.isOn(p4) if __name__ == '__main__': main()
26 Apr 2010
Python Tips - 判断点是否在直线上
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment