geom2d.point

Basic 2D point/vector.

class geom2d.point.P(x: TPoint | float, y: float | None = None)

Two dimensional immutable Cartesion point (vector).

Represented as a simple tuple (x, y) so that it is compatible with many other libraries.

angle() float

The angle of this vector relative to the x axis in radians.

Returns:

A float value between -pi and pi.

angle2(p1: Sequence[float], p2: Sequence[float]) float

The angle formed by p1->self->p2.

The angle is negative if p1 is to the left of p2.

Parameters:
  • p1 – First point as 2-tuple (x, y).

  • p2 – Second point as 2-tuple( x, y).

Returns:

The angle in radians between -pi and pi. Returns 0 if points are coincident.

bisector(p1: Sequence[float], p2: Sequence[float], mag: float = 1.0) P

The bisector between the angle formed by p1->self->p2.

Parameters:
  • p1 – First point as 2-tuple (x, y).

  • p2 – Second point as 2-tuple (x, y).

  • mag – Optional magnitude. Default is 1.0 (unit vector).

Returns:

A vector with origin at self with magnitude mag.

ccw_angle2(p1: Sequence[float], p2: Sequence[float]) float

The counterclockwise angle formed by p1->self->p2.

Parameters:
  • p1 – First point as 2-tuple (x, y).

  • p2 – Second point as 2-tuple( x, y).

Returns:

An angle in radians between 0 and 2*math.pi.

copysign(v: tuple[float, float]) P

Return a new point with x,y having the same sign as v.

Where p.x value is magnitude self.x with sign of v[0], and p.y value is magnitude self.y with sign of v[1].

cross(other: Sequence[float]) float

Compute the cross product with another vector.

Also called the perp-dot product for 2D vectors. Also called determinant for 2D matrix.

See:

http://mathworld.wolfram.com/PerpDotProduct.html http://johnblackburne.blogspot.com/2012/02/perp-dot-product.html http://www.gamedev.net/topic/441590-2d-cross-product/

From Woodward: The cross product generates a new vector perpendicular to the two that are being multiplied, with a length equal to the (ordinary) product of their lengths.

Parameters:

other – The vector with which to compute the cross product.

Returns:

A scalar cross product.

distance(p: Sequence[float]) float

Euclidean distance from this point to another point.

Parameters:

p – The other point as a 2-tuple (x, y).

Returns:

The Euclidean distance.

distance2(p: Sequence[float]) float

Euclidean distance squared to other point.

This can be used to compare distances without the expense of a sqrt.

distance_to_line(p1: Sequence[float], p2: Sequence[float]) float

Distance to line.

Euclidean distance from this point to it’s normal projection on a line that intersects the given points.

See:

http://mathworld.wolfram.com/Point-LineDistance2-Dimensional.html http://local.wasp.uwa.edu.au/~pbourke/geometry/pointline/

Parameters:
  • p1 – First point on line.

  • p2 – Second point on line.

Returns:

Normal distance to line.

dot(other: Sequence[float]) float

Compute the dot product with another vector.

Equivalent to |p1| * |p2| * cos(theta) where theta is the angle between the two vectors.

See:

http://en.wikipedia.org/wiki/Dot_product

Parameters:

other – The vector with which to compute the dot product. A 2-tuple (x, y).

Returns:

A scalar dot product.

static from_polar(mag: float, angle: float) P

Create a Cartesian point from polar coordinates.

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

Parameters:
  • mag – Magnitude (radius)

  • angle – Angle in radians

Returns:

A point.

inside_triangle(a: Sequence[float], b: Sequence[float], c: Sequence[float]) bool

Test if this point lies inside the triangle A->B->C.

Where ABC is clockwise or counter-clockwise.

See:

http://www.sunshine2k.de/stuff/Java/PointInTriangle/PointInTriangle.html

Parameters:
  • a – First point of triangle as 2-tuple (x, y)

  • b – Second point of triangle as 2-tuple (x, y)

  • c – Third point of triangle as 2-tuple (x, y)

Returns:

True if this point lies within the triangle ABC.

is_ccw(other: Sequence[float]) bool

Return True if the other vector is to the left of this vector.

That would be counter-clockwise with respect to the origin as long as the sector angle is less than PI (180deg).

is_zero() bool

Check if this point is within EPSILON distance to zero/origin.

length() float

The length or scalar magnitude of the vector.

Returns:

Distance from (0, 0).

length2() float

The square of the length of the vector.

mag() float

The length or scalar magnitude of the vector.

Returns:

Distance from (0, 0).

static max_point() P

Create a point with maximum X and Y values.

static min_point() P

Create a point with minimum negative X and Y values.

mirror() P

This vector flipped 180d.

normal(left: bool = True) P

Return a vector perpendicular to this one.

Parameters:

left – Normal is left of vector if True, otherwise right. Default is True.

normal_projection(p: Sequence[float]) float

Unit distance to normal projection of point.

The unit distance mu from the origin that corresponds to the projection of the specified point on to the line described by this vector.

Parameters:

p – A point as 2-tuple (x, y).

normalized() P

The vector scaled to unit length.

If the vector length is zero, a null (0, 0) vector is returned.

Returns:

A copy of this vector scaled to unit length.

perpendicular(left: bool = True) P

Return a vector perpendicular to this one.

Parameters:

left – Normal is left of vector if True, otherwise right. Default is True.

static random(min_x: float = -1.7976931348623157e+308, max_x: float = 1.7976931348623157e+308, min_y: float | None = None, max_y: float | None = None, normal: bool = False) P

Create a random point within a range.

The default range is -big_xy() to big_xy().

Parameters:
  • min_x – Minimum X coordinate.

  • max_x – Maximum X coordinate.

  • min_y – Minimum Y coordinate. Default is value of min_x.

  • max_y – Maximum Y coordinate. Default is value of max_x.

  • normal – Use a normal/Gaussian instead of uniform distribution. Mu is midpoint of min/max span, sigma is half span divided by 3.

rotate(angle: float, origin: Sequence[float] | None = None) P

Return a copy of this point rotated about the origin by angle.

to_polar() tuple[float, float]

Convert this point to polar coordinates.

ReturnsL

A tuple containing the radius/magnitude and angle respectively (r, a).

to_svg(scale: float = 1) str

SVG string representation.

transform(matrix: TMatrix) P

Apply transform matrix to this vector.

Returns:

A copy of this point with the transform matrix applied to it.

unit() P

The vector scaled to unit length.

If the vector length is zero, a null (0, 0) vector is returned.

Returns:

A copy of this vector scaled to unit length.

winding(p2: Sequence[float], p3: Sequence[float]) float

Winding direction.

Determine the direction defined by the three points p1->p2->p3. p1 being this point.

Parameters:
  • p2 – Second point as 2-tuple (x, y).

  • p3 – Third point as 2-tuple (x, y).

Returns:

Positive if self->p2->p3 is clockwise (right), negative if counterclockwise (left), zero if points are colinear.

property x: float

The X axis coordinate.

property y: float

The Y axis coordinate.

geom2d.point.almost_equal(p1: Sequence[float], p2: Sequence[float], tolerance: float | None = None) bool

Compare points for geometric equality.

Parameters:
  • p1 – First point. A 2-tuple (x, y).

  • p2 – Second point. A 2-tuple (x, y).

  • tolerance – Max distance between the two points. Default is EPSILON.

Returns:

True if distance between the points < tolerance.

geom2d.point.max_xy() float

Max absolute value for X or Y.

This is a fairly large number (well over 100 digits) that still works with the hash function and fast float comparisons.

This is a function not a constant because const.EPSILON is potentially mutable.