The following classes are available for dealing with simple 3D geometry. The interfaces are very similar to the 2D classes (but note that you cannot mix and match 2D and 3D operations).
For example, to find the closest point on a line to a sphere:
>>> sphere = Sphere(Point3(1., 2., 3.,), 2.) >>> line = Line3(Point3(0., 0., 0.), Point3(-1., -1., 0.)) >>> line.connect(sphere).p1 Point3(1.50, 1.50, 0.00)
To find the corresponding closest point on the sphere to the line:
>>> line.connect(sphere).p2 Point3(1.32, 1.68, 1.05)
XXX I have not checked if these are correct.
A point on a 3D plane. Construct in the obvious way:
>>> p = Point3(1.0, 2.0, 3.0) >>> p Point3(1.00, 2.00, 3.00)
Point3 subclasses Vector3, so all of Vector3 operators and methods apply. In particular, subtracting two points gives a vector:
>>> Point3(1.0, 2.0, 3.0) - Point3(1.0, 0.0, -2.0) Vector3(0.00, 2.00, 5.00)
The following methods are also defined:
intersect(other):
If other is a Sphere, returns True iff the point lies within
the sphere.
connect(other):Returns a LineSegment3 which is the minimum length line segment that can connect the two shapes. other may be a Point3, Line3, Ray3, LineSegment3, Sphere or Plane.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.
A Line3 is a line on a 3D plane extending to infinity in both directions; a Ray3 has a finite end-point and extends to infinity in a single direction; a LineSegment3 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:
>>> Line3(Point3(1.0, 1.0, 1.0), Point3(1.0, 2.0, 3.0)) Line3(<1.00, 1.00, 1.00> + u<0.00, 1.00, 2.00>) >>> Line3(Point3(0.0, 1.0, 1.0), Vector3(1.0, 1.0, 2.0)) Line3(<0.00, 1.00, 1.00> + u<1.00, 1.00, 2.00>) >>> Ray3(Point3(1.0, 1.0, 1.0), Vector3(1.0, 1.0, 2.0), 1.0) Ray3(<1.00, 1.00, 1.00> + u<0.41, 0.41, 0.82>)
Internally, lines, rays and line segments store a Point3 p and a Vector3 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 Sphere, returns a LineSegment3 which is the
intersection of the sphere and line, or None if there is no
intersection.
If other is a Plane, returns a Point3 of intersection, or
None.
connect(other):Returns a LineSegment3 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 Point3, Line3, Ray3, LineSegment3, Sphere or Plane.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.
LineSegment3 also has a length property which is read-only.
Spheres are constructed with a center Point3 and a radius:
>>> s = Sphere(Point3(1.0, 1.0, 1.0), 0.5) >>> s Sphere(<1.00, 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 Point3, returns True iff the point lies
within the sphere.
If other is a Line3, Ray3 or LineSegment3, returns
a LineSegment3 giving the intersection, or None if the
line does not intersect the sphere.
connect(other):Returns a LineSegment3 which is the minimum length line segment that can connect the two shapes. other may be a Point3, Line3, Ray3, LineSegment3, Sphere or Plane.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.
Planes can be constructed with any of:
Internally, planes are stored with the normal n and constant k such that n.p = k for any point on the plane p.
The following methods are supported:
intersect(other):
If other is a Line3, Ray3 or LineSegment3, returns a
Point3 of intersection, or None if there is no intersection.
If other is a Plane, returns the Line3 of intersection.
connect(other):Returns a LineSegment3 which is the minimum length line segment that can connect the two shapes. other may be a Point3, Line3, Ray3, LineSegment3, Sphere or Plane.
distance(other):
Returns the absolute minimum distance to other. Internally this
simply returns the length of the result of connect.