Utility functions and classes located in module ezdxf.math.

Functions

ezdxf.math.is_close_points(p1: Vertex, p2: Vertex, abs_tol=1e-10) → bool

Returns True if p1 is very close to p2.

Parameters
  • p1 – first vertex as Vector compatible object

  • p2 – second vertex as Vector compatible object

  • abs_tol – absolute tolerance

Raises

TypeError – for incompatible vertices

ezdxf.math.closest_point(base: Vertex, points: Iterable[Vertex]) → Vector

Returns closest point to base.

Parameters
  • base – base point as Vector compatible object

  • points – iterable of points as Vector compatible object

ezdxf.math.bspline_control_frame(fit_points: Iterable[Vertex], degree: int = 3, method: str = 'distance', power: float = 0.5)

Generates the control points for the B-spline control frame by Curve Global Interpolation. Given are the fit points and the degree of the B-spline. The function provides 3 methods for generating the parameter vector t:

Method

Description

'uniform'

creates a uniform t vector, from 0 to 1 evenly spaced; see uniform method

'distance'

creates a t vector with values proportional to the fit point distances, see chord length method

'centripetal'

creates a t vector with values proportional to the fit point distances ^ power; see centripetal method

Parameters
  • fit_points – fit points of B-spline, as list of Vector compatible objects

  • degree – degree of B-spline

  • method – calculation method for parameter vector t

  • power – power for centripetal method

Returns

BSpline

ezdxf.math.bspline_control_frame_approx(fit_points: Iterable[Vertex], count: int, degree: int = 3, method: str = 'distance', power: float = 0.5)

Approximate B-spline by a reduced count of control points, given are the fit points and the degree of the B-spline.

Parameters
  • fit_points – all fit points of B-spline as Vector compatible objects

  • count – count of designated control points

  • degree – degree of B-spline

  • method – calculation method for parameter vector t, see bspline_control_frame()

  • power – power for centripetal method

Returns

BSpline

ezdxf.math.uniform_knot_vector(n: int, order: int) → List[float]

Returns an uniform knot vector for a B-spline of order and n control points.

order = degree + 1

Parameters
  • n – count of control points

  • order – spline order

ezdxf.math.open_uniform_knot_vector(n: int, order: int) → List[float]

Returns an open uniform knot vector for a B-spline of order and n control points.

order = degree + 1

Parameters
  • n – count of control points

  • order – spline order

ezdxf.math.required_knot_values(count: int, order: int) → int

Returns the count of required knot values for a B-spline of order and count control points.

degree = degree of B-spline, in math papers often called: p

Parameters
  • count – count of control points, in math papers often called: n + 1

  • order – order of B-Spline, in math papers often called: k

Relationships:

  • k (order) = p (degree) + 1

  • 2 ≤ k (order) ≤ n + 1 (count)

ezdxf.math.xround(value: float, rounding: float = 0.0) → float

Extended rounding function, argument rounding defines the rounding limit:

0

remove fraction

0.1

round next to x.1, x.2, … x.0

0.25

round next to x.25, x.50, x.75 or x.00

0.5

round next to x.5 or x.0

1.0

round to a multiple of 1: remove fraction

2.0

round to a multiple of 2: xxx2, xxx4, xxx6 …

5.0

round to a multiple of 5: xxx5 or xxx0

10.0

round to a multiple of 10: xx10, xx20, …

Parameters
  • value – float value to round

  • rounding – rounding limit

2D Functions

ezdxf.math.distance_point_line_2d(point: Vec2, start: Vec2, end: Vec2) → float

Returns distance from point to line defined by start- and end point.

Parameters
  • point – 2D point to test as Vec2 or tuple of float

  • start – line definition point as Vec2 or tuple of float

  • end – line definition point as Vec2 or tuple of float

New in version 0.11.

ezdxf.math.point_to_line_relation(point: Vec2, start: Vec2, end: Vec2, abs_tol=1e-10) → int

Returns -1 if point is left line, +1 if point is right of line and 0 if point is on the line. The line is defined by two vertices given as arguments start and end.

Parameters
  • point – 2D point to test as Vec2

  • start – line definition point as Vec2

  • end – line definition point as Vec2

  • abs_tol – tolerance for minimum distance to line

New in version 0.11.

ezdxf.math.is_point_on_line_2d(point: Vec2, start: Vec2, end: Vec2, ray=True, abs_tol=1e-10) → bool

Returns True if point is on line.

Parameters
  • point – 2D point to test as Vec2

  • start – line definition point as Vec2

  • end – line definition point as Vec2

  • ray – if True point has to be on the infinite ray, if False point has to be on the line segment

  • abs_tol – tolerance for on line test

New in version 0.11.

ezdxf.math.is_point_left_of_line(point: Vec2, start: Vec2, end: Vec2, colinear=False) → bool

Returns True if point is “left of line” defined by start- and end point, a colinear point is also “left of line” if argument colinear is True.

Parameters
  • point – 2D point to test as Vec2

  • start – line definition point as Vec2

  • end – line definition point as Vec2

  • colinear – a colinear point is also “left of line” if True

Changed in version 0.11: renamed from is_left_of_line()

ezdxf.math.is_point_in_polygon_2d(point: Vec2, polygon: Iterable[Vec2], abs_tol=1e-10) → int

Test if point is inside polygon.

Parameters
  • point – 2D point to test as Vec2

  • polygon – iterable of 2D points as Vec2

  • abs_tol – tolerance for distance check

Returns

+1 for inside, 0 for on boundary line, -1 for outside

New in version 0.11.

ezdxf.math.convex_hull_2d(points: Iterable[Vertex]) → List[Vertex]

Returns 2D convex hull for points.

Parameters

points – iterable of points as Vector compatible objects, z-axis is ignored

ezdxf.math.intersection_line_line_2d(line1: Sequence[Vec2], line2: Sequence[Vec2], virtual=True, abs_tol=1e-10) → Optional[Vec2]

Compute the intersection of two lines in the xy-plane.

Parameters
  • line1 – start- and end point of first line to test e.g. ((x1, y1), (x2, y2)).

  • line2 – start- and end point of second line to test e.g. ((x3, y3), (x4, y4)).

  • virtualTrue returns any intersection point, False returns only real intersection points.

  • abs_tol – tolerance for intersection test.

Returns

None if there is no intersection point (parallel lines) or intersection point as Vec2

New in version 0.11.

ezdxf.math.rytz_axis_construction(d1: Vector, d2: Vector) → Tuple[Vector, Vector, float]

The Rytz’s axis construction is a basic method of descriptive Geometry to find the axes, the semi-major axis and semi-minor axis, starting from two conjugated half-diameters.

Source: Wikipedia

Given conjugated diameter d1 is the vector from center C to point P and the given conjugated diameter d2 is the vector from center C to point Q. Center of ellipse is always (0, 0, 0). This algorithm works for 2D/3D vectors.

Parameters
  • d1 – conjugated semi-major axis as Vector

  • d2 – conjugated semi-minor axis as Vector

Returns

Tuple of (major axis, minor axis, ratio)

ezdxf.math.offset_vertices_2d(vertices: Iterable[Vertex], offset: float, closed: bool = False) → Iterable[Vec2]

Yields vertices of the offset line to the shape defined by vertices. The source shape consist of straight segments and is located in the xy-plane, the z-axis of input vertices is ignored. Takes closed shapes into account if argument closed is True, which yields intersection of first and last offset segment as first vertex for a closed shape. For closed shapes the first and last vertex can be equal, else an implicit closing segment from last to first vertex is added. A shape with equal first and last vertex is not handled automatically as closed shape.

Warning

Adjacent collinear segments in opposite directions, same as a turn by 180 degree (U-turn), leads to unexpected results.

New in version 0.11.

Parameters
  • vertices – source shape defined by vertices

  • offset – line offset perpendicular to direction of shape segments defined by vertices order, offset > 0 is ‘left’ of line segment, offset < 0 is ‘right’ of line segment

  • closedTrue to handle as closed shape

source = [(0, 0), (3, 0), (3, 3), (0, 3)]
result = list(offset_vertices_2d(source, offset=0.5, closed=True))
_images/offset_vertices_2d_1.png

Example for a closed collinear shape, which creates 2 additional vertices and the first one has an unexpected location:

source = [(0, 0), (0, 1), (0, 2), (0, 3)]
result = list(offset_vertices_2d(source, offset=0.5, closed=True))
_images/offset_vertices_2d_2.png

3D Functions

ezdxf.math.normal_vector_3p(a: Vector, b: Vector, c: Vector) → Vector

Returns normal vector for 3 points, which is the normalized cross product for: a->b x a->c.

New in version 0.11.

ezdxf.math.is_planar_face(face: Sequence[Vector], abs_tol=1e-9) → bool

Returns True if sequence of vectors is a planar face.

Parameters
  • face – sequence of Vector objects

  • abs_tol – tolerance for normals check

New in version 0.11.

ezdxf.math.subdivide_face(face: Sequence[Union[Vector, Vec2]], quads=True) → Iterable[List[Vector]]

Yields new subdivided faces. Creates new faces from subdivided edges and the face midpoint by linear interpolation.

Parameters
  • face – a sequence of vertices, Vec2 and Vector objects supported.

  • quads – create quad faces if True else create triangles

New in version 0.11.

ezdxf.math.subdivide_ngons(faces: Iterable[Sequence[Union[Vector, Vec2]]]) → Iterable[List[Vector]]

Yields only triangles or quad faces, subdivides ngons into triangles.

Parameters

faces – iterable of faces as sequence of Vec2 and Vector objects

New in version 0.12.

ezdxf.math.intersection_ray_ray_3d(ray1: Tuple[Vector, Vector], ray2: Tuple[Vector, Vector], abs_tol=1e-10) → Sequence[Vector]

Calculate intersection of two rays, returns a 0-tuple for parallel rays, a 1-tuple for intersecting rays and a 2-tuple for not intersecting and not parallel rays with points of closest approach on each ray.

Parameters
  • ray1 – first ray as tuple of two points on the ray as Vector objects

  • ray2 – second ray as tuple of two points on the ray as Vector objects

  • abs_tol – absolute tolerance for comparisons

New in version 0.11.

Transformation Classes

OCS Class

class ezdxf.math.OCS(extrusion: Vertex = Vector(0.0, 0.0, 1.0))

Establish an OCS for a given extrusion vector.

Parameters

extrusion – extrusion vector.

ux

x-axis unit vector

uy

y-axis unit vector

uz

z-axis unit vector

from_wcs(point: Vertex) → Vertex

Returns OCS vector for WCS point.

points_from_wcs(points: Iterable[Vertex]) → Iterable[Vertex]

Returns iterable of OCS vectors from WCS points.

to_wcs(point: Vertex) → Vertex

Returns WCS vector for OCS point.

points_to_wcs(points: Iterable[Vertex]) → Iterable[Vertex]

Returns iterable of WCS vectors for OCS points.

render_axis(layout: BaseLayout, length: float = 1, colors: Tuple[int, int, int] = (1, 3, 5))

Render axis as 3D lines into a layout.

UCS Class

class ezdxf.math.UCS(origin: Vertex = (0, 0, 0), ux: Vertex = None, uy: Vertex = None, uz: Vertex = None)

Establish an user coordinate system (UCS). The UCS is defined by the origin and two unit vectors for the x-, y- or z-axis, all axis in WCS. The missing axis is the cross product of the given axis.

If x- and y-axis are None: ux = (1, 0, 0), uy = (0, 1, 0), uz = (0, 0, 1).

Unit vectors don’t have to be normalized, normalization is done at initialization, this is also the reason why scaling gets lost by copying or rotating.

Parameters
  • origin – defines the UCS origin in world coordinates

  • ux – defines the UCS x-axis as vector in WCS

  • uy – defines the UCS y-axis as vector in WCS

  • uz – defines the UCS z-axis as vector in WCS

ux

x-axis unit vector

uy

y-axis unit vector

uz

z-axis unit vector

is_cartesian

Returns True if cartesian coordinate system.

copy() → UCS

Returns a copy of this UCS.

New in version 0.11.

to_wcs(point: Vertex) → Vector

Returns WCS point for UCS point.

points_to_wcs(points: Iterable[Vertex]) → Iterable[Vector]

Returns iterable of WCS vectors for UCS points.

direction_to_wcs(vector: Vertex) → Vector

Returns WCS direction for UCS vector without origin adjustment.

from_wcs(point: Vertex) → Vector

Returns UCS point for WCS point.

points_from_wcs(points: Iterable[Vertex]) → Iterable[Vector]

Returns iterable of UCS vectors from WCS points.

direction_from_wcs(vector: Vertex) → Vector

Returns UCS vector for WCS vector without origin adjustment.

to_ocs(point: Vertex) → Vector

Returns OCS vector for UCS point.

The OCS is defined by the z-axis of the UCS.

points_to_ocs(points: Iterable[Vertex]) → Iterable[Vector]

Returns iterable of OCS vectors for UCS points.

The OCS is defined by the z-axis of the UCS.

Parameters

points – iterable of UCS vertices

ocs_points_to_ocs(points: Iterable[Vertex], extrusion=Vector(0.0, 0.0, 1.0)) → Iterable[Vector]

Returns iterable of OCS vectors for UCS points located by an OCS established by the extrusion vector.

Parameters
  • points – iterable of UCS vertices located in an OCS

  • extrusion – vector defines the source OCS

to_ocs_angle_deg(angle: float) → float

Transforms angle from current UCS to the parent coordinate system (most likely the WCS) including the transformation to the OCS established by the extrusion vector UCS.uz.

Parameters

angle – in UCS in degrees

angles_to_ocs_deg(angles: Iterable[float]) → List[float]

Transforms angles from current UCS to the parent coordinate system (most likely the WCS) including the transformation to the OCS established by the extrusion vector UCS.uz.

Parameters

angles – iterable of UCS (OCS) angles to transform, angles in degrees

Returns

List of OCS angles in degrees.

ocs_angles_to_ocs_deg(angles: Iterable[float], extrusion=Vector(0.0, 0.0, 1.0)) → List[float]

Transforms angles from current UCS located in an established OCS defined by the given extrusion vector to the parent coordinate system (most likely the WCS) including the transformation to the target OCS established by the transformed extrusion vector.

Parameters
  • angles – iterable of UCS (OCS) angles to transform, angles in degrees

  • extrusion – extrusion vector of the source OCS

Returns

List of OCS angles in degrees.

rotate(axis: Vertex, angle:float) → UCS

Returns a new rotated UCS, with the same origin as the source UCS. The rotation vector is located in the origin and has WCS coordinates e.g. (0, 0, 1) is the WCS z-axis as rotation vector.

Scaling get lost because creates new UCS and unit vectors always get normalized on initialization.

New in version 0.11.

Parameters
  • axis – arbitrary rotation axis as vector in WCS

  • angle – rotation angle in radians

rotate_local_x(angle:float) → UCS

Returns a new rotated UCS, rotation axis is the local x-axis.

Scaling get lost because creates new UCS and unit vectors always get normalized on initialization.

New in version 0.11.

Parameters

angle – rotation angle in radians

rotate_local_y(angle:float) → UCS

Returns a new rotated UCS, rotation axis is the local y-axis.

Scaling get lost because creates new UCS and unit vectors always get normalized on initialization.

New in version 0.11.

Parameters

angle – rotation angle in radians

rotate_local_z(angle:float) → UCS

Returns a new rotated UCS, rotation axis is the local z-axis.

Scaling get lost because creates new UCS and unit vectors always get normalized on initialization.

New in version 0.11.

Parameters

angle – rotation angle in radians

shift(delta: Vertex) → UCS

Shifts current UCS by delta vector and returns self.

New in version 0.11.

Parameters

delta – shifting vector

moveto(location: Vertex) → UCS

Place current UCS at new origin location and returns self.

New in version 0.11.

Parameters

location – new origin in WCS

static from_x_axis_and_point_in_xy(origin: Vertex, axis: Vertex, point: Vertex) → UCS

Returns an new UCS defined by the origin, the x-axis vector and an arbitrary point in the xy-plane.

Parameters
  • origin – UCS origin as (x, y, z) tuple in WCS

  • axis – x-axis vector as (x, y, z) tuple in WCS

  • point – arbitrary point unlike the origin in the xy-plane as (x, y, z) tuple in WCS

static from_x_axis_and_point_in_xz(origin: Vertex, axis: Vertex, point: Vertex) → UCS

Returns an new UCS defined by the origin, the x-axis vector and an arbitrary point in the xz-plane.

Parameters
  • origin – UCS origin as (x, y, z) tuple in WCS

  • axis – x-axis vector as (x, y, z) tuple in WCS

  • point – arbitrary point unlike the origin in the xz-plane as (x, y, z) tuple in WCS

static from_y_axis_and_point_in_xy(origin: Vertex, axis: Vertex, point: Vertex) → UCS

Returns an new UCS defined by the origin, the y-axis vector and an arbitrary point in the xy-plane.

Parameters
  • origin – UCS origin as (x, y, z) tuple in WCS

  • axis – y-axis vector as (x, y, z) tuple in WCS

  • point – arbitrary point unlike the origin in the xy-plane as (x, y, z) tuple in WCS

static from_y_axis_and_point_in_yz(origin: Vertex, axis: Vertex, point: Vertex) → UCS

Returns an new UCS defined by the origin, the y-axis vector and an arbitrary point in the yz-plane.

Parameters
  • origin – UCS origin as (x, y, z) tuple in WCS

  • axis – y-axis vector as (x, y, z) tuple in WCS

  • point – arbitrary point unlike the origin in the yz-plane as (x, y, z) tuple in WCS

static from_z_axis_and_point_in_xz(origin: Vertex, axis: Vertex, point: Vertex) → UCS

Returns an new UCS defined by the origin, the z-axis vector and an arbitrary point in the xz-plane.

Parameters
  • origin – UCS origin as (x, y, z) tuple in WCS

  • axis – z-axis vector as (x, y, z) tuple in WCS

  • point – arbitrary point unlike the origin in the xz-plane as (x, y, z) tuple in WCS

static from_z_axis_and_point_in_yz(origin: Vertex, axis: Vertex, point: Vertex) → UCS

Returns an new UCS defined by the origin, the z-axis vector and an arbitrary point in the yz-plane.

Parameters
  • origin – UCS origin as (x, y, z) tuple in WCS

  • axis – z-axis vector as (x, y, z) tuple in WCS

  • point – arbitrary point unlike the origin in the yz-plane as (x, y, z) tuple in WCS

render_axis(layout: BaseLayout, length: float = 1, colors: Tuple[int, int, int] = (1, 3, 5))

Render axis as 3D lines into a layout.

BRCS Class

class ezdxf.math.BRCS(insert: Vector, ux: Vector, uy: Vector, uz: Vector)

Establish a block reference coordinate system. Create BRCS from block references (Insert) by the brcs() method.

Parameters
  • insert – Block reference insert point

  • ux – x-axis as unit vector in WCS

  • uy – y-axis as unit vector in WCS

  • uz – z-axis as unit vector in WCS

to_wcs(point: Vertex) → Vector

Returns WCS point for block reference point.

points_to_wcs(points: Iterable[Vertex]) → Iterable[Vector]

Returns iterable of WCS vectors for block reference points.

direction_to_wcs(vector: Vertex) → Vector

Returns WCS direction for block reference vector without origin adjustment.

Matrix44

class ezdxf.math.Matrix44(*args)

This is a pure Python implementation for 4x4 transformation matrices, to avoid dependency to big numerical packages like numpy, before binary wheels, installation of these packages wasn’t always easy on Windows.

Matrix44 initialization:

  • Matrix44() returns the identity matrix.

  • Matrix44(values) values is an iterable with the 16 components of the matrix.

  • Matrix44(row1, row2, row3, row4) four rows, each row with four values.

__repr__() → str

Returns the representation string of the matrix: Matrix44((col0, col1, col2, col3), (...), (...), (...))

set(*args) → None

Reset matrix values.

  • set() creates the identity matrix.

  • set(values) values is an iterable with the 16 components of the matrix.

  • set(row1, row2, row3, row4) four rows, each row with four values.

get_row(row: int) → Tuple[float, float, float, float]

Get row as list of of four float values.

Parameters

row – row index [0 .. 3]

set_row(row: int, values: Sequence[float]) → None

Sets the values in a row.

Parameters
  • row – row index [0 .. 3]

  • values – iterable of four row values

get_col(col: int) → Tuple[float, float, float, float]

Returns a column as a tuple of four floats.

Parameters

col – column index [0 .. 3]

set_col(col: int, values: Sequence[float])

Sets the values in a column.

Parameters
  • col – column index [0 .. 3]

  • values – iterable of four column values

copy() → Matrix44

Copy of Matrix

__copy__() → Matrix44

Copy of Matrix

classmethod scale(sx: float, sy: float = None, sz: float = None) → Matrix44

Returns a scaling transformation matrix. If sy is None, sy = sx, and if sz is None sz = sx.

classmethod translate(dx: float, dy: float, dz: float) → Matrix44

Returns a translation matrix for translation vector (dx, dy, dz).

classmethod x_rotate(angle: float) → Matrix44

Returns a rotation matrix about the x-axis.

Parameters

angle – rotation angle in radians

classmethod y_rotate(angle: float) → Matrix44

Returns a rotation matrix about the y-axis.

Parameters

angle – rotation angle in radians

classmethod z_rotate(angle: float) → Matrix44

Returns a rotation matrix about the z-axis.

Parameters

angle – rotation angle in radians

classmethod axis_rotate(axis: Vertex, angle: float) → Matrix44

Returns a rotation matrix about an arbitrary axis.

Parameters
  • axis – rotation axis as (x, y, z) tuple or Vector object

  • angle – rotation angle in radians

classmethod xyz_rotate(angle_x: float, angle_y: float, angle_z: float) → Matrix44

Returns a rotation matrix for rotation about each axis.

Parameters
  • angle_x – rotation angle about x-axis in radians

  • angle_y – rotation angle about y-axis in radians

  • angle_z – rotation angle about z-axis in radians

classmethod perspective_projection(left: float, right: float, top: float, bottom: float, near: float, far: float) → Matrix44

Returns a matrix for a 2D projection.

Parameters
  • left – Coordinate of left of screen

  • right – Coordinate of right of screen

  • top – Coordinate of the top of the screen

  • bottom – Coordinate of the bottom of the screen

  • near – Coordinate of the near clipping plane

  • far – Coordinate of the far clipping plane

classmethod perspective_projection_fov(fov: float, aspect: float, near: float, far: float) → Matrix44

Returns a matrix for a 2D projection.

Parameters
  • fov – The field of view (in radians)

  • aspect – The aspect ratio of the screen (width / height)

  • near – Coordinate of the near clipping plane

  • far – Coordinate of the far clipping plane

static chain(*matrices: Iterable[Matrix44]) → Matrix44

Compose a transformation matrix from one or more matrices.

static ucs(ux: Vertex, uy: Vertex, uz: Vertex) → Matrix44

Returns a matrix for coordinate transformation from WCS to UCS. Origin of both systems is (0, 0, 0). For transformation from UCS to WCS, transpose the returned matrix.

All vectors as (x, y, z) tuples or Vector objects.

Parameters
  • ux – x-axis for UCS as unit vector

  • uy – y-axis for UCS as unit vector

  • uz – z-axis for UCS as unit vector

__hash__() → int

Returns hash value of matrix.

__getitem__(index: Tuple[int, int])

Get (row, column) element.

__setitem__(index: Tuple[int, int], value: float)

Set (row, column) element.

__iter__() → Iterable[float]

Iterates over all matrix values.

rows() → Iterable[Tuple[float, float, float, float]]

Iterate over rows as 4-tuples.

columns() → Iterable[Tuple[float, float, float, float]]

Iterate over columns as 4-tuples.

__mul__(other: Matrix44) → Matrix44

Returns a new matrix as result of the matrix multiplication with another matrix.

__imul__(other: Matrix44) → Matrix44

Inplace multiplication with another matrix.

fast_mul(other: Matrix44) → Matrix44

Multiplies this matrix with other matrix.

Assumes that both matrices have a right column of (0, 0, 0, 1). This is True for matrices composed of rotations, translations and scales. fast_mul is approximately 25% quicker than the *= operator.

transform(vector: Vertex) → ezdxf.math.vector.Vector

Transforms a 3D vector and returns the result as a tuple.

transform_vectors(vectors: Iterable[Vertex]) → List[ezdxf.math.vector.Vector]

Returns a list of transformed vectors.

transpose() → None

Swaps the rows for columns inplace.

get_transpose() → Matrix44

Returns a new transposed matrix.

determinant() → float

Returns determinant.

inverse() → None

Calculates the inverse of the matrix.

Raises

ZeroDivisionError – if matrix has no inverse.

Construction Tools

Vector

class ezdxf.math.Vector(*args)

This is an immutable universal 3D vector object. This class is optimized for universality not for speed. Immutable means you can’t change (x, y, z) components after initialization:

v1 = Vector(1, 2, 3)
v2 = v1
v2.z = 7  # this is not possible, raises AttributeError
v2 = Vector(v2.x, v2.y, 7)  # this creates a new Vector() object
assert v1.z == 3  # and v1 remains unchanged

Vector initialization:

  • Vector(), returns Vector(0, 0, 0)

  • Vector((x, y)), returns Vector(x, y, 0)

  • Vector((x, y, z)), returns Vector(x, y, z)

  • Vector(x, y), returns Vector(x, y, 0)

  • Vector(x, y, z), returns Vector(x, y, z)

Addition, subtraction, scalar multiplication and scalar division left and right handed are supported:

v = Vector(1, 2, 3)
v + (1, 2, 3) == Vector(2, 4, 6)
(1, 2, 3) + v == Vector(2, 4, 6)
v - (1, 2, 3) == Vector(0, 0, 0)
(1, 2, 3) - v == Vector(0, 0, 0)
v * 3 == Vector(3, 6, 9)
3 * v == Vector(3, 6, 9)
Vector(3, 6, 9) / 3 == Vector(1, 2, 3)
-Vector(1, 2, 3) == (-1, -2, -3)

Comparison between vectors and vectors or tuples is supported:

Vector(1, 2, 3) < Vector (2, 2, 2)
(1, 2, 3) < tuple(Vector(2, 2, 2))  # conversion necessary
Vector(1, 2, 3) == (1, 2, 3)

bool(Vector(1, 2, 3)) is True
bool(Vector(0, 0, 0)) is False
x

x-axis value

y

y-axis value

z

z-axis value

xy

Vector as (x, y, 0), projected on the xy-plane.

xyz

Vector as (x, y, z) tuple.

vec2

Real 2D vector as Vec2 object.

magnitude

Length of vector.

magnitude_xy

Length of vector in the xy-plane.

magnitude_square

Square length of vector.

is_null

True for Vector(0, 0, 0).

angle

Angle between vector and x-axis in the xy-plane in radians.

angle_deg

Returns angle of vector and x-axis in the xy-plane in degrees.

spatial_angle

Spatial angle between vector and x-axis in radians.

spatial_angle_deg

Spatial angle between vector and x-axis in degrees.

__str__() → str

Return '(x, y, z)' as string.

__repr__() → str

Return 'Vector(x, y, z)' as string.

__len__() → int

Returns always 3.

__hash__() → int

Returns hash value of vector, enables the usage of vector as key in set and dict.

copy() → Vector

Returns a copy of vector as Vector object.

__copy__() → Vector

Returns a copy of vector as Vector object.

__deepcopy__(memodict: dict) → Vector

copy.deepcopy() support.

__getitem__(index: int) → float

Support for indexing:

  • v[0] is v.x

  • v[1] is v.y

  • v[2] is v.z

__iter__() → Iterable[float]

Returns iterable of x-, y- and z-axis.

__abs__() → float

Returns length (magnitude) of vector.

replace(x: float = None, y: float = None, z: float = None) → Vector

Returns a copy of vector with replaced x-, y- and/or z-axis.

classmethod generate(items: Iterable[Sequence[float]]) → Iterable[Vector]

Returns an iterable of Vector objects.

classmethod list(items: Iterable[Sequence[float]]) → List[Vector]

Returns a list of Vector objects.

classmethod from_angle(angle: float, length: float = 1.) → Vector

Returns a Vector object from angle in radians in the xy-plane, z-axis = 0.

classmethod from_deg_angle(angle: float, length: float = 1.) → Vector

Returns a Vector object from angle in degrees in the xy-plane, z-axis = 0.

orthogonal(ccw: bool = True) → Vector

Returns orthogonal 2D vector, z-axis is unchanged.

Parameters

ccw – counter clockwise if True else clockwise

lerp(other: Any, factor=.5) → Vector

Returns linear interpolation between self and other.

Parameters
  • other – end point as Vector compatible object

  • factor – interpolation factor (0 = self, 1 = other, 0.5 = mid point)

project(other: Any) → Vector

Returns projected vector of other onto self.

normalize(length: float = 1.) → Vector

Returns normalized vector, optional scaled by length.

reversed() → Vector

Returns negated vector (-self).

isclose(other: Any, abs_tol: float = 1e-12) → bool

Returns True if self is close to other. Uses math.isclose() to compare all axis.

__neg__() → Vector

Returns negated vector (-self).

__bool__() → bool

Returns True if vector is not (0, 0, 0).

__eq__(other: Any) → bool

Equal operator.

Parameters

otherVector compatible object

__lt__(other: Any) → bool

Lower than operator.

Parameters

otherVector compatible object

__add__(other: Any) → Vector

Add operator: self + other

Parameters

otherVector compatible object

__radd__(other: Any) → Vector

RAdd operator: other + self

Parameters

otherVector compatible object

__sub__(other: Any) → Vector

Sub operator: self - other

Parameters

otherVector compatible object

__rsub__(other: Any) → Vector

RSub operator: other - self

Parameters

otherVector compatible object

__mul__(other: float) → Vector

Mul operator: self * other

Parameters

other – scale factor

__rmul__(other: float) → Vector

RMul operator: other * self

Parameters

other – scale factor

__truediv__(other: float) → Vector

Div operator: self / other

Parameters

other – scale factor

__div__(other: float) → Vector

Div operator: self / other

Parameters

other – scale factor

__rtruediv__(other: float) → Vector

RDiv operator: other / self

Parameters

other – scale factor

__rdiv__(other: float) → Vector

RDiv operator: other / self

Parameters

other – scale factor

dot(other: Any) → float

Dot operator: self . other

Parameters

otherVector compatible object

cross(other: Any) → Vector

Dot operator: self x other

Parameters

otherVector compatible object

distance(other: Any) → float

Returns distance between self and other vector.

angle_between(other: Any) → float

Returns angle between self and other in radians. +angle is counter clockwise orientation.

Parameters

otherVector compatible object

rotate(angle: float) → Vector

Returns vector rotated about angle around the z-axis.

Parameters

angle – angle in radians

rotate_deg(angle: float) → Vector

Returns vector rotated about angle around the z-axis.

Parameters

angle – angle in degrees

ezdxf.math.X_AXIS

Vector(1, 0, 0)

ezdxf.math.Y_AXIS

Vector(0, 1, 0)

ezdxf.math.Z_AXIS

Vector(0, 0, 1)

ezdxf.math.NULLVEC

Vector(0, 0, 0)

Vec2

class ezdxf.math.Vec2(v)

Vec2 represents a special 2D vector (x, y). The Vec2 class is optimized for speed and not immutable, iadd(), isub(), imul() and idiv() modifies the vector itself, the Vector class returns a new object.

Vec2 initialization accepts float-tuples (x, y[, z]), two floats or any object providing x and y attributes like Vec2 and Vector objects.

Parameters
  • v – vector object with x and y attributes/properties or a sequence of float [x, y, ...] or x-axis as float if argument y is not None

  • y – second float for Vec2(x, y)

Vec2 implements a subset of Vector.

Plane

class ezdxf.math.Plane(normal: Vector, distance: float)

Represents a plane in 3D space as normal vector and the perpendicular distance from origin.

New in version 0.11.

normal

Normal vector of the plane.

distance_from_origin

The (perpendicular) distance of the plane from origin (0, 0, 0).

vector

Returns the location vector.

classmethod from_3p(a: Vector, b: Vector, c: Vector) → Plane

Returns a new plane from 3 points in space.

classmethod from_vector(vector) → Plane

Returns a new plane from a location vector.

copy() → Plane

Returns a copy of the plane.

signed_distance_to(v: Vector) → float

Returns signed distance of vertex v to plane, if distance is > 0, v is in ‘front’ of plane, in direction of the normal vector, if distance is < 0, v is at the ‘back’ of the plane, in the opposite direction of the normal vector.

distance_to(v: Vector) → float

Returns absolute (unsigned) distance of vertex v to plane.

is_coplanar_vertex(v: Vector, abs_tol=1e-9) → bool

Returns True if vertex v is coplanar, distance from plane to vertex v is 0.

is_coplanar_plane(p: Plane, abs_tol=1e-9) → bool

Returns True if plane p is coplanar, normal vectors in same or opposite direction.

BoundingBox

class ezdxf.math.BoundingBox(vertices: Iterable[Vertex])

3D bounding box.

Parameters

vertices – iterable of (x, y, z) tuples or Vector objects

extmin

“lower left” corner of bounding box

extmax

“upper right” corner of bounding box

property center

Returns center of bounding box.

extend(vertices: Iterable[Vertex]) → None

Extend bounds by vertices.

Parameters

vertices – iterable of (x, y, z) tuples or Vector objects

inside(vertex: Vertex) → bool

Returns True if vertex is inside bounding box.

property size

Returns size of bounding box.

BoundingBox2d

class ezdxf.math.BoundingBox2d(vertices: Iterable[Vertex])

Optimized 2D bounding box.

Parameters

vertices – iterable of (x, y[, z]) tuples or Vector objects

extmin

“lower left” corner of bounding box

extmax

“upper right” corner of bounding box

property center

Returns center of bounding box.

extend(vertices: Iterable[Vertex]) → None

Extend bounds by vertices.

Parameters

vertices – iterable of (x, y[, z]) tuples or Vector objects

inside(vertex: Vertex) → bool

Returns True if vertex is inside bounding box.

property size

Returns size of bounding box.

ConstructionRay

class ezdxf.math.ConstructionRay(p1: Vertex, p2: Vertex = None, angle: float = None)

Infinite 2D construction ray as immutable object.

Parameters
  • p1 – definition point 1

  • p2 – ray direction as 2nd point or None

  • angle – ray direction as angle in radians or None

location

Location vector as Vec2.

direction

Direction vector as Vec2.

slope

Slope of ray or None if vertical.

angle

Angle between x-axis and ray in radians.

angle_deg

Angle between x-axis and ray in degrees.

is_vertical

True if ray is vertical (parallel to y-axis).

is_horizontal

True if ray is horizontal (parallel to x-axis).

__str__()

Return str(self).

is_parallel(self, other: ConstructionRay) → bool

Returns True if rays are parallel.

intersect(other: ConstructionRay) → Vec2

Returns the intersection point as (x, y) tuple of self and other.

Raises

ParallelRaysError – if rays are parallel

orthogonal(location: 'Vertex') → ConstructionRay

Returns orthogonal ray at location.

bisectrix(other: ConstructionRay) → ConstructionRay:

Bisectrix between self and other.

yof(x: float) → float

Returns y-value of ray for x location.

Raises

ArithmeticError – for vertical rays

xof(y: float) → float

Returns x-value of ray for y location.

Raises

ArithmeticError – for horizontal rays

ConstructionLine

class ezdxf.math.ConstructionLine(start: Vertex, end: Vertex)

2D ConstructionLine is similar to ConstructionRay, but has a start- and endpoint. The direction of line goes from start- to endpoint, “left of line” is always in relation to this line direction.

Parameters
  • start – start point of line as Vec2 compatible object

  • end – end point of line as Vec2 compatible object

start

start point as Vec2

end

end point as Vec2

bounding_box

bounding box of line as BoundingBox2d object.

ray

collinear ConstructionRay.

is_vertical

True if line is vertical.

__str__()

Return str(self).

move(dx: float, dy: float) → None

Move line about dx in x-axis and about dy in y-axis.

Parameters
  • dx – translation in x-axis

  • dy – translation in y-axis

length() → float

Returns length of line.

midpoint() → Vec2

Returns mid point of line.

inside_bounding_box(point: Vertex) → bool

Returns True if point is inside of line bounding box.

intersect(other: ConstructionLine, abs_tol:float=1e-10) → Optional[Vec2]

Returns the intersection point of to lines or None if they have no intersection point.

Parameters
has_intersection(other: ConstructionLine, abs_tol:float=1e-10) → bool

Returns True if has intersection with other line.

is_point_left_of_line(point: Vertex, colinear=False) → bool

Returns True if point is left of construction line in relation to the line direction from start to end.

If colinear is True, a colinear point is also left of the line.

ConstructionCircle

class ezdxf.math.ConstructionCircle(center: Vertex, radius: float = 1.0)

Circle construction tool.

Parameters
  • center – center point as Vec2 compatible object

  • radius – circle radius > 0

center

center point as Vec2

radius

radius as float

bounding_box

2D bounding box of circle as BoundingBox2d object.

static from_3p(p1: Vertex, p2: Vertex, p3: Vertex) → ConstructionCircle

Creates a circle from three points, all points have to be compatible to Vec2 class.

__str__() → str

Returns string representation of circle ConstructionCircle(center, radius).

move(dx: float, dy: float) → None

Move circle about dx in x-axis and about dy in y-axis.

Parameters
  • dx – translation in x-axis

  • dy – translation in y-axis

point_at(angle: float) → Vec2

Returns point on circle at angle as Vec2 object.

Parameters

angle – angle in radians

inside(point: Vertex) → bool

Returns True if point is inside circle.

tangent(angle: float) → ConstructionRay

Returns tangent to circle at angle as ConstructionRay object.

Parameters

angle – angle in radians

intersect_ray(ray: ConstructionRay, abs_tol: float = 1e-10) → Sequence[Vec2]

Returns intersection points of circle and ray as sequence of Vec2 objects.

Parameters
  • ray – intersection ray

  • abs_tol – absolute tolerance for tests (e.g. test for tangents)

Returns

tuple of Vec2 objects

tuple size

Description

0

no intersection

1

ray is a tangent to circle

2

ray intersects with the circle

intersect_circle(other: ConstructionCircle, abs_tol: float = 1e-10) → Sequence[Vec2]

Returns intersection points of two circles as sequence of Vec2 objects.

Parameters
  • other – intersection circle

  • abs_tol – absolute tolerance for tests (e.g. test for circle touch point)

Returns

tuple of Vec2 objects

tuple size

Description

0

no intersection

1

circle touches the other circle at one point

2

circle intersects with the other circle

ConstructionArc

class ezdxf.math.ConstructionArc(center: Vertex = (0, 0), radius: float = 1, start_angle: float = 0, end_angle: float = 360, is_counter_clockwise: bool = True)

This is a helper class to create parameters for the DXF Arc class.

Parameters
  • center – center point as Vec2 compatible object

  • radius – radius

  • start_angle – start angle in degrees

  • end_angle – end angle in degrees

  • is_counter_clockwise – swaps start- and end angle if False

center

center point as Vec2

radius

radius as float

start_angle

start angle in degrees

end_angle

end angle in degrees

start_angle_rad

start angle in radians.

end_angle_rad

end angle in radians.

start_point

start point of arc as Vec2.

end_point

end point of arc as Vec2.

bounding_box

bounding box of arc as BoundingBox2d.

move(dx: float, dy: float) → None

Move arc about dx in x-axis and about dy in y-axis.

Parameters
  • dx – translation in x-axis

  • dy – translation in y-axis

classmethod from_2p_angle(start_point: Vertex, end_point: Vertex, angle: float, ccw: bool = True) → ConstructionArc

Create arc from two points and enclosing angle. Additional precondition: arc goes by default in counter clockwise orientation from start_point to end_point, can be changed by ccw = False.

Parameters
  • start_point – start point as Vec2 compatible object

  • end_point – end point as Vec2 compatible object

  • angle – enclosing angle in degrees

  • ccw – counter clockwise direction if True

classmethod from_2p_radius(start_point: Vertex, end_point: Vertex, radius: float, ccw: bool = True, center_is_left: bool = True) → ConstructionArc

Create arc from two points and arc radius. Additional precondition: arc goes by default in counter clockwise orientation from start_point to end_point can be changed by ccw = False.

The parameter center_is_left defines if the center of the arc is left or right of the line from start_point to end_point. Parameter ccw = False swaps start- and end point, which inverts the meaning of center_is_left.

Parameters
  • start_point – start point as Vec2 compatible object

  • end_point – end point as Vec2 compatible object

  • radius – arc radius

  • ccw – counter clockwise direction if True

  • center_is_left – center point of arc is left of line from start- to end point if True

classmethod from_3p(start_point: Vertex, end_point: Vertex, def_point: Vertex, ccw: bool = True) → ConstructionArc

Create arc from three points. Additional precondition: arc goes in counter clockwise orientation from start_point to end_point.

Parameters
  • start_point – start point as Vec2 compatible object

  • end_point – end point as Vec2 compatible object

  • def_point – additional definition point as Vec2 compatible object

  • ccw – counter clockwise direction if True

add_to_layout(layout: BaseLayout, ucs: UCS = None, dxfattribs: dict = None) → Arc

Add arc as DXF Arc entity to a layout.

Supports 3D arcs by using an UCS. An ConstructionArc is always defined in the xy-plane, but by using an arbitrary UCS, the arc can be placed in 3D space, automatically OCS transformation included.

Parameters
  • layout – destination layout as BaseLayout object

  • ucs – place arc in 3D space by UCS object

  • dxfattribs – additional DXF attributes for DXF Arc entity

ConstructionBox

class ezdxf.math.ConstructionBox(center: Vertex = (0, 0), width: float = 1, height: float = 1, angle: float = 0)

Helper class to create rectangles.

Parameters
  • center – center of rectangle

  • width – width of rectangle

  • height – height of rectangle

  • angle – angle of rectangle in degrees

center

box center

width

box width

height

box height

angle

rotation angle in degrees

corners

box corners as sequence of Vec2 objects.

bounding_box

BoundingBox2d

incircle_radius

incircle radius

circumcircle_radius

circum circle radius

__iter__() → Iterable[Vec2]

Iterable of box corners as Vec2 objects.

__getitem__(corner) → Vec2

Get corner by index corner, list like slicing is supported.

__repr__() → str

Returns string representation of box as ConstructionBox(center, width, height, angle)

classmethod from_points(p1: Vertex, p2: Vertex) → ConstructionBox

Creates a box from two opposite corners, box sides are parallel to x- and y-axis.

Parameters
  • p1 – first corner as Vec2 compatible object

  • p2 – second corner as Vec2 compatible object

move(dx: float, dy: float) → None

Move box about dx in x-axis and about dy in y-axis.

Parameters
  • dx – translation in x-axis

  • dy – translation in y-axis

expand(dw: float, dh: float) → None

Expand box: dw expand width, dh expand height.

scale(sw: float, sh: float) → None

Scale box: sw scales width, sh scales height.

rotate(angle: float) → None

Rotate box by angle in degrees.

is_inside(point: Vertex) → bool

Returns True if point is inside of box.

is_any_corner_inside(other: ConstructionBox) → bool

Returns True if any corner of other box is inside this box.

is_overlapping(other: ConstructionBox) → bool

Returns True if this box and other box do overlap.

border_lines() → Sequence[ConstructionLine]

Returns border lines of box as sequence of ConstructionLine.

intersect(line: ConstructionLine) → List[Vec2]

Returns 0, 1 or 2 intersection points between line and box border lines.

Parameters

line – line to intersect with border lines

Returns

list of intersection points

list size

Description

0

no intersection

1

line touches box at one corner

2

line intersects with box

Shape2d

class ezdxf.math.Shape2d(vertices: Iterable[Vertex] = None)

2D geometry object as list of Vec2 objects, vertices can be moved, rotated and scaled.

Parameters

vertices – iterable of Vec2 compatible objects.

vertices

List of Vec2 objects

bounding_box

BoundingBox2d

__len__() → int

Returns count of vertices.

__getitem__(item) → Vec2

Get vertex by index item, supports list like slicing.

append(vertex: Vertex) → None

Append single vertex.

Parameters

vertex – vertex as Vec2 compatible object

extend(vertices: Iterable) → None

Append multiple vertices.

Parameters

vertices – iterable of vertices as Vec2 compatible objects

move(dx: float, dy: float) → None

Move shape about dx in x-axis and about dy in y-axis.

Parameters
  • dx – translation in x-axis

  • dy – translation in y-axis

translate(vector: Vertex) → None

Translate shape about vector.

scale(sx: float = 1.0, sy: float = 1.0) → None

Scale shape about sx in x-axis and sy in y-axis.

scale_uniform(scale: float) → None

Scale shape uniform about scale in x- and y-axis.

rotate(angle: float, center: Vertex = None) → None

Rotate shape around rotation center about angle in degrees.

rotate_rad(angle: float, center: Vertex = None) → None

Rotate shape around rotation center about angle in radians.

offset(offset: float, closed: bool = False) → ezdxf.math.shape.Shape2d

Returns a new offset shape, for more information see also ezdxf.math.offset_vertices_2d() function.

New in version 0.11.

Parameters
  • offset – line offset perpendicular to direction of shape segments defined by vertices order, offset > 0 is ‘left’ of line segment, offset < 0 is ‘right’ of line segment

  • closedTrue to handle as closed shape

convex_hull() → ezdxf.math.shape.Shape2d

Returns convex hull as new shape.

Curves

BSpline

class ezdxf.math.BSpline(control_points: Iterable[Vertex], order: int = 4, knots: Iterable[float] = None, weights: Iterable[float] = None)

Calculate the points of a B-spline curve, using an uniform open knot vector (“clamped”).

Accepts 2D points as definition points, but output is always 3D (z-axis = 0).

Parameters
  • control_points – iterable of control points as Vector compatible objects

  • order – spline order

  • knots – iterable of knot values

  • weights – iterable of weight values

control_points

control points as list of Vector

count

Count of control points, (n + 1 in math definition).

degree
order

order of B-spline = degree + 1

max_t

Biggest knot value.

knot_values() → List[float]

Returns a list of knot values as floats, the knot vector always has order+count values (n + p + 2 in math definition).

basis_values(t: float) → List[float]

Returns the basis vector for position t.

approximate(segments: int = 20) → Iterable[Vector]

Approximates the whole B-spline from 0 to max_t, by line segments as a list of vertices, vertices count = segments + 1.

point(t: float) → Vector

Get point at SplineCurve(t) as tuple (x, y, z).

Parameters

t – parameter in range [0, max_t]

Returns: Vector(x, y, z)

insert_knot(t: float) → None

Insert additional knot, without altering the curve shape.

Parameters

t – position of new knot 0 < t < max_t

BSplineU

class ezdxf.math.BSplineU(control_points: Iterable[Vertex], order: int = 4, weights: Iterable[float] = None)

Subclass of BSpline

Calculate the points of a B-spline curve, uniform (periodic) knot vector (open curve).

BSplineClosed

class ezdxf.math.BSplineClosed(control_points: Iterable[Vertex], order: int = 4, weights: Iterable[float] = None)

Subclass of BSpline

Calculate the points of a closed uniform B-spline curve (closed curve).

DBSpline

class ezdxf.math.DBSpline(control_points: Iterable[Vertex], order: int = 4, knots: Iterable[float] = None, weights: Iterable[float] = None)

Subclass of BSpline

Calculate points and derivative of a B-spline curve, using an uniform open knot vector (clamped curve).

point(t: float) → Tuple[Vector, Vector, Vector]

Get point, 1st and 2nd derivative at B-spline(t) as tuple (p, d1, d3), where p, d1 and d2 are Vector objects.

Parameters

t – parameter in range [0, max_t]

DBSplineU

class ezdxf.math.DBSplineU(control_points: Iterable[Vertex], order: int = 4, weights: Iterable[float] = None)

Subclass of DBSpline

Calculate points and derivative of a B-spline curve, uniform (periodic) knot vector (open curve).

DBSplineClosed

class ezdxf.math.DBSplineClosed(control_points: Iterable[Vertex], order: int = 4, weights: Iterable[float] = None)

Subclass of DBSpline

Calculate the points and derivative of a closed uniform B-spline curve (closed curve).

UNTESTED!

Bezier

class ezdxf.math.Bezier(defpoints: Iterable[Vertex])

A Bézier curve is a parametric curve used in computer graphics and related fields. Bézier curves are used to model smooth curves that can be scaled indefinitely. “Paths”, as they are commonly referred to in image manipulation programs, are combinations of linked Bézier curves. Paths are not bound by the limits of rasterized images and are intuitive to modify. (Source: Wikipedia)

This is a general implementation which works with any count of definition points greater than 2, but it is a simple and slow implementation. For more performance look at the specialized Bezier4P class.

Parameters

defpoints – iterable of definition points as Vector compatible objects.

control_points

control points as list of Vector objects.

approximate(segments: int = 20) → Iterable[Vector]

Approximate Bézier curve by vertices, yields segments + 1 vertices as Vector objects.

Parameters

segments – count of segments for approximation

point(t: float) → Vector

Returns a point for location t at the Bézier curve as Vector object.

A Bézier curve is a parametric curve, parameter t goes from 0 to 1, where 0 is the first definition point anf 1 is the last definition point.

Parameters

t – parameter in range [0, 1]

DBezier

class ezdxf.math.DBezier(defpoints: Iterable[Vertex])

Subclass of Bezier.

Calculate vertices and derivative of a Bézier curve.

point(t: float) → Tuple[Vector, Vector, Vector]

Returns (point, 1st derivative, 2nd derivative) tuple for location t at the Bézier curve, all values as Vector objects.

Parameters

t – parameter in range [0, 1]

Bezier4P

class ezdxf.math.Bezier4P(defpoints: List[Sequence[float]])

Implements an optimized cubic Bézier curve for exact 4 control points. A Bézier curve is a parametric curve, parameter t goes from 0 to 1, where 0 is the first control point and 1 is the fourth control point.

Special behavior:

  • 2D control points in, returns 2D results as (x, y) tuples

  • 3D control points in, returns 3D results as (x, y, z) tuples

Parameters

defpoints – iterable of definition points as (x, y[, z]) tuples

control_points

control points as list of (x, y, z), z-axis is 0 for 2D curves.

point(t: float) → Sequence[float]

Returns point for location t` at the Bézier curve.

Parameters

t – curve position in the range [0, 1]

Returns

(x, y[, z]) tuple

Return type

tuple

tangent(t: float) → Sequence[float]

Returns direction vector of tangent for location t at the Bézier curve.

Parameters

t – curve position in the range [0, 1]

Returns

(x, y[, z]) tuple, the direction vector at location t.

Return type

tuple

approximate(segments: int) → Iterable[Sequence[float]]

Approximate Bézier curve by vertices, yields segments + 1 vertices as (x, y[, z]) tuples.

Parameters

segments – count of segments for approximation

Returns

iterable of (x, y[, z]) tuples

approximated_length(segments: int = 100) → float

Returns estimated length of Bézier curve as approximation by line segments.

BezierSurface

class ezdxf.math.BezierSurface(defpoints: List[List[Sequence[float]]])

BezierSurface defines a mesh of m x n control points. This is a parametric surface, which means the m-dimension goes from 0 to 1 as parameter u and the n-dimension goes from 0 to 1 as parameter v.

Parameters

defpoints – matrix (list of lists) of m rows and n columns: [ [m1n1, m1n2, … ], [m2n1, m2n2, …] … ] each element is a 3D location as (x, y, z) tuple.

nrows

count of rows (m-dimension)

ncols

count of columns (n-dimension)

point(u: float, v: float) → Sequence[float]

Returns a point for location (u, v) at the Bézier surface as (x, y, z) tuple, parameters u and v in the range of [0, 1].

approximate(usegs: int, vsegs: int) → List[List[Sequence[float]]]

Approximate surface as grid of (x, y, z) tuples.

Parameters
  • usegs – count of segments in u-direction (m-dimension)

  • vsegs – count of segments in v-direction (n-dimension)

Returns

list of usegs + 1 rows, each row is a list of vsegs + 1 vertices as (x, y, z) tuples.

EulerSpiral

class ezdxf.math.EulerSpiral(curvature: float = 1.0)

This class represents an euler spiral (clothoid) for curvature (Radius of curvature).

This is a parametric curve, which always starts at the origin = (0, 0).

Parameters

curvature – radius of curvature

radius(t: float) → float

Get radius of circle at distance t.

tangent(t: float) → Vector

Get tangent at distance t as :class.`Vector` object.

distance(radius: float) → float

Get distance L from origin for radius.

point(t: float) → Vector

Get point at distance t as :class.`Vector`.

circle_center(t: float) → Vector

Get circle center at distance t.

Changed in version 0.10: renamed from circle_midpoint

approximate(length: float, segments: int) → Iterable[Vector]

Approximate curve of length with line segments.

Generates segments+1 vertices as Vector objects.

bspline(length: float, segments: int = 10, degree: int = 3, method: str = 'uniform') → BSpline

Approximate euler spiral as B-spline.

Parameters
  • length – length of euler spiral

  • segments – count of fit points for B-spline calculation

  • degree – degree of BSpline

  • method – calculation method for parameter vector t

Returns

BSpline