ait.core.geom module

AIT 2D/3D Geometry

This module contains basic 2D and 3D geometry classes (Point, Line, Polygon, Rectangle) with arithmetic operator, containment, and sequence/iterator methods. These methods allow for natural and convenient Python expressions such as:

# Translate point by five units in both x and y.
point + 5

# Polygon hit tests
if point in polygon:
  ...

# Iteration or vertices
for vertex in polygon:
  ...

This module was originally written as a support library for AEGIS ground processing code and its precursors (e.g. OASIS). It dates back to at least 2009 and probably even earlier.

class ait.core.geom.Line(p, q)

Bases: object

Line segment objects contain two points.

__init__(p, q)

Line(Point, Point) -> Line

Creates a new Line segment with the given endpoints.

intersect(line) → Point | None

Returns the intersection point of this line segment with another. If this line segment and the other line segment are conincident, the first point on this line segment is returned. If the line segments do not intersect, None is returned.

See http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

Examples:

>>> A = Line( Point(0.0, 0.0), Point(5.0, 5.0) )
>>> B = Line( Point(5.0, 0.0), Point(0.0, 5.0) )
>>> C = Line( Point(1.0, 3.0), Point(9.0, 3.0) )
>>> D = Line( Point(0.5, 3.0), Point(6.0, 4.0) )
>>> E = Line( Point(1.0, 1.0), Point(3.0, 8.0) )
>>> F = Line( Point(0.5, 2.0), Point(4.0, 7.0) )
>>> G = Line( Point(1.0, 2.0), Point(3.0, 6.0) )
>>> H = Line( Point(2.0, 4.0), Point(4.0, 8.0) )
>>> I = Line( Point(3.5, 9.0), Point(3.5, 0.5) )
>>> J = Line( Point(3.0, 1.0), Point(9.0, 1.0) )
>>> K = Line( Point(2.0, 3.0), Point(7.0, 9.0) )
>>> L = Line( Point(1.0, 2.0), Point(5.0, 7.0) )
>>> A.intersect(B)
Point(2.5, 2.5)
>>> C.intersect(D) == None
True
>>> E.intersect(F)
Point(1.8275862069, 3.89655172414)
>>> G.intersect(H)
Point(1.0, 2.0)
>>> I.intersect(J)
Point(3.5, 1.0)
>>> K.intersect(L) == None
True
slope() → float
p
q
class ait.core.geom.Plane(point, normal)

Bases: object

Plane objects are defined by a point and direction vector normal to the plane.

__init__(point, normal)

Plane(point, normal) -> Plane

Creates a new plane given a point and direction vector normal to the plane.

front(point) → True | False

Returns True if point is in “”front”” of the Plane, False otherwise.

intersect(line) → Point | None

Returns the point at which the line segment and Plane intersect or None if they do not intersect.

n
p
class ait.core.geom.Point(x=0, y=0, z=None)

Bases: object

Point is a simple 2D Cartesian point object with public ‘x’ and ‘y’ coordinate fields. The operators +, -, +=, -=, *, *=, /, /=, == and !=.

__init__(x=0, y=0, z=None)

Point(x=0, y=0, z=None) -> Point Point([x, y, z]) -> Point Point([x, y]) -> Point

copy()

Returns a copy of this Point.

dot(self, other) → number

Returns the dot product of this Point with another.

x
y
z
class ait.core.geom.Polygon(*vertices)

Bases: object

Polygon objects contain a list of points.

__init__(*vertices)

Polygon(vertices) -> Polygon

Creates a new Polygon with no vertices.

area() → number

Returns the area of this Polygon.

bounds() → Rect

Returns the bounding Rectangle for this Polygon.

center() -> (x, y)

Returns the center (of mass) point of this Polygon.

See http://en.wikipedia.org/wiki/Polygon

Examples:

>>> p = Polygon()
>>> p.vertices = [ Point(3, 8), Point(6, 4), Point(0, 3) ]
>>> p.center()
Point(2.89285714286, 4.82142857143)
contains(p)

Returns True if point is contained inside this Polygon, False otherwise.

This method uses the Ray Casting algorithm.

Examples:

>>> p = Polygon()
>>> p.vertices = [Point(1, 1), Point(1, -1), Point(-1, -1), Point(-1, 1)]
>>> p.contains( Point(0, 0) )
True
>>> p.contains( Point(2, 3) )
False
segments()

Return the Line segments that comprise this Polygon.

vertices
class ait.core.geom.Rect(ul, lr)

Bases: object

__init__(ul, lr)

Rect(Point, Point) -> Rect

Creates a new rectangle.

area() → number

Returns the area of this Rectangle.

bounds() → Rect

Returns the Rectangle itself.

center() → Point

Returns the center Point of this Rectangle.

contains(point) → True | False

Returns True if point is contained inside this Rectangle, False otherwise.

Examples:

>>> r = Rect( Point(-1, -1), Point(1, 1) )
>>> r.contains( Point(0, 0) )
True
>>> r.contains( Point(2, 3) )
False
height() → number

Returns the height of this Rectangle.

segments() → [ Line, Line, Line, Line ]

Return a list of Line segments that comprise this Rectangle.

width() → number

Returns the width of this Rectangle.

lr
ul
ait.core.geom.runTests()