Library
Module
Module type
Parameter
Class
Class type
2-dimensional vector type, including basic mathematical/geometrical operations and transformations, allowing for points in 2d space, and higher level types composed of them (e.g. Path2.t
and Poly2.t
) to be manipulated.
val v : float -> float -> t
v x y
Construct a vector from x
and y
coordinates.
val of_tup : (float * float) -> t
of_tup (x, y)
Construct a vector from a tuple of xy coordinates.
val to_tup : t -> float * float
to_tup t
Convert the vector t
to a tuple of xy coordinates.
val zero : t
Zero vector
approx ?eps a b
Returns true if the distance between vectors a
and b
is less than or equal to the epsilon eps
.
horizontal_op f a b
Hadamard (element-wise) operation between vectors a
and b
using the function f
.
val norm : t -> float
norm t
Calculate the vector norm (a.k.a. magnitude) of t
.
distance a b
Calculate the magnitude of the difference (Hadamard subtraction) between a
and b
.
normalize t
Normalize t
to a vector for which the magnitude is equal to 1. e.g. norm (normalize t) = 1.
cross a b
Vector cross product of a
and b
. In the case of 2d vectors, the cross product is performed with an assumed z = 0.
angle_points a b c
Calculate the angle between the points a
, b
, and c
.
val ccw_theta : t -> float
ccw_theta t
Calculate the angle in radians counter-clockwise t
is from the positive x-axis along the xy plane.
vector_axis a b
Compute the vector perpendicular to the vectors a
and b
.
clockwise_sign ?eps a b c
Returns the rotational ordering (around the z-axis) of the points a
, b
, and c
as a signed float, -1.
for clockwise, and 1.
for counter-clockwise. If the points are collinear (not forming a valid triangle, within the tolerance of eps
), 0.
is returned.
collinear p1 p2 p3
Returns true
if p2
lies on the line between p1
and p3
.
lerpn a b n
Linearly interpolate n
vectors between vectors a
and b
. If endpoint
is true
, the last vector will be equal to b
, otherwise, it will be about a + (b - a) * (1 - 1 / n)
.
distance_to_vector p v
Distance from point p
to the line passing through the origin with unit direction v
.
distance_to_line ?bounds ~line t
Distance between the vector t
, and any point on line
. bounds
indicates whether each end {a; b}
of line
is bounded, or a ray (default = (false, false)
, indicating an infinite line in both directions.).
point_on_line ?eps ?bounds ~line t
Return true
if the point t
falls within eps
distance of the line
. bounds
indicates whether each end {a; b}
of line
is bounded, or a ray (default = (false, false)
, indicating an infinite line in both directions.)
line_closest_point ?bounds ~line t
Find the closest point to t
lying on the provided line
. bounds
indicates whether each end {a; b}
of line
is bounded, or a ray (default = (false, false)
, indicating an infinite line in both directions.)
lower_bounds a b
Compute the lower bounds (minima of each dimension) of the vectors a
and b
.
upper_bounds a b
Compute the upper bounds (maxima of each dimension) of the vectors a
and b
.
bbox_intersect a b
Compute the intersect of the bounding boxes a
and b
.
val bbox_area : bbox -> float
bbox_area bb
Compute the area of the bounding box bb
.
val get_x : t -> float
val get_y : t -> float
val get_z : t -> float
val to_string : t -> string
left_of_line ?eps ~line t
Return 1.
if t
is left of line
, -1.
if it is to the right, and 0.
if it falls on (within eps
) the line
. Float is returned as this is simply a clockwise check.
val line_intersection :
?eps:float ->
?bounds1:(bool * bool) ->
?bounds2:(bool * bool) ->
line ->
line ->
t option
line_intersection ?eps ?bounds1 ?bounds2 a b
Find the intersection (if it exists) between the lines a
and b
. bounds1
and bounds2
indicate whether the ends of a
and b
respectively are bounded or are infinite rays.
line_normal p1 p2
Calculates the normal (perpendicular vector) of the line between p1
and p2
.
Spatial transformations. Quaternion operations are provided when this module is included in OCADml
.
rotate ?about r t
Rotation of t
by r
(in radians) around the origin (or the point about
if provided).
zrot ?about r t
Rotation of t
by r
(in radians) around the origin (or the point about
if provided). Alias to rotate
.
mirror ax t
Mirrors t
on a plane through the origin, defined by the normal vector ax
.
to_v3 ?z v
Create a 3d vector from the 2d vector v
by adding a z
coordinate (default = 0.
)
lift p t
Lift the 2d vector/point t
onto the plane p
. On partial application of p
, a Affine3.t
is computed to perform the lift transform. Alias to Plane.lift
.
affine m t
Apply 2d affine transformation matrix m
to the vector t
.
affine3 m t
Apply 3d affine transformation matrix m
to the vector t
, taking it into the 3rd dimension.
val quaternion : ?about:V3.t -> Quaternion.t -> t -> V3.t
quaternion ?about q t
Rotate t
with the quaternion q
around the origin (or the point about
if provided), taking it into the 3rd dimension.