The following classes are available for dealing with simple 2D geometry.
The interface to each shape is similar; in particular, the connect
and distance methods are defined identically for each.
For example, to find the closest point on a line to a circle:
>>> circ = Circle(Point2(3., 2.), 2.) >>> line = Line2(Point2(0., 0.), Point2(-1., 1.)) >>> line.connect(circ).p1 Point2(0.50, -0.50)
To find the corresponding closest point on the circle to the line:
>>> line.connect(circ).p2 Point2(1.59, 0.59)
A point on a 2D plane. Construct in the obvious way:
>>> p = Point2(1.0, 2.0) >>> p Point2(1.00, 2.00)
Point2 subclasses Vector2, so all of Vector2 operators and methods apply. In particular, subtracting two points gives a vector:
>>> Point2(2.0, 3.0) - Point2(1.0, 0.0) Vector2(1.00, 3.00)
The following methods are also defined:
connect(other):Returns a LineSegment2 which is the minimum length line segment that can connect the two shapes. other may be a Point2, Line2, Ray2, LineSegment2 or Circle.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.
A Line2 is a line on a 2D plane extending to infinity in both directions; a Ray2 has a finite end-point and extends to infinity in a single direction; a LineSegment2 joins two points.
All three classes support the same constructors, operators and methods, but may behave differently when calculating intersections etc.
You may construct a line, ray or line segment using any of:
For example:
>>> Line2(Point2(1.0, 1.0), Point2(2.0, 3.0)) Line2(<1.00, 1.00> + u<1.00, 2.00>) >>> Line2(Point2(1.0, 1.0), Vector2(1.0, 2.0)) Line2(<1.00, 1.00> + u<1.00, 2.00>) >>> Ray2(Point2(1.0, 1.0), Vector2(1.0, 2.0), 1.0) Ray2(<1.00, 1.00> + u<0.45, 0.89>)
Internally, lines, rays and line segments store a Point2 p and a Vector2 v. You can also access (but not set) the two endpoints p1 and p2. These may or may not be meaningful for all types of lines.
The following methods are supported by all three classes:
intersect(other):If other is a Line2, Ray2 or LineSegment2, returns a Point2 of intersection, or None if the lines are parallel.
If other is a Circle, returns a LineSegment2 giving the part of the line that intersects the circle, or None if there is no intersection. Note that a line segment is returned even if the intersection is tangential (i.e., only at one point); the two endpoints will be identical in this case.
connect(other):Returns a LineSegment2 which is the minimum length line segment that can connect the two shapes. For two parallel lines, this line segment may be in an arbitrary position. other may be a Point2, Line2, Ray2, LineSegment2 or Circle.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.
LineSegment2 also has a length property which is read-only.
Circles are constructed with a center Point2 and a radius:
>>> c = Circle(Point2(1.0, 1.0), 0.5) >>> c Circle(<1.00, 1.00>, radius=0.50)
Internally there are two attributes: c, giving the center point and r, giving the radius.
The following methods are supported:
intersect(other):If other is a Line2, Ray2 or LineSegment2, returns a LineSegment2 giving the part of the line that intersects the circle, or None if there is no intersection.
connect(other):Returns a LineSegment2 which is the minimum length line segment that can connect the two shapes. other may be a Point2, Line2, Ray2, LineSegment2 or Circle.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.