#twodee
Two dimensional geometry manipulation
##Install
$ npm install twodee
##Use
###Creating a simple Triangle
twodee uses xyzw for vector manipulation.
$ npm install xyzw
import Vector2 from 'xyzw/source/Vector2';
import Triangle2 from 'twodee/source/Triangle2';
To get es5 safe versions of all files replace /source with /es5
###Creating a triangle
const triangleA = new Triangle2(
new Vector2([0.0, 0.0]),
new Vector2([0.0, 1.0]),
new Vector2([1.0, 0.0])
);
const point0 = triangleA.p0;
const point1 = triangleA.p1;
const point2 = triangleA.p2;
const orientation = triangleA.orientation;
const centroid = triangleA.centroid;
const circumcenter = triangleA.circumcenter
const area = triangleA.area;
####Factory constructors
All primitives come with convenient factory constructors:
const center = new Vector2([0.0, 0.0]);
const radius = 1.0;
const rotation = Math.PI;
const triangleB = Triangle2.Equilateral(center, radius, rotation);
####Collision testing
Triangles can test for collisions with points, segments and other triangles:
const intersections = [];
const collision = triangleA.intersects(triangleB, intersections);
All intersection tests have static versions that do not require actual objects:
const intersections = [];
const collision = Triangle2.intersect(
triangleA.p0, triangleA.p1, triangleA.p2,
triangleB.p0, triangleB.p1, triangleB.p2,
intersections
)
###Creating a ray
import Ray2 from 'twodee/source/Ray2';
const ray = new Ray2(new Vector2([0.0, 0.0]), new Vector2([0.0, 1.0]));
const origin = ray.origin;
const orientation = ray.orientation;
Rays can test for intersections with line segments and other rays. All line intersection tests guarantee valid results for parallel and co-linear entities.
const point0 = new Vector2([4.0, -0.5]);
const point1 = new Vector2([4.0, 0.5]);
const intersection = new Vector2();
const collision = ray.intersectsSegment(point0, point1, intersection);
###Creating a Bounding Box Rectangle
import Vector2 from 'xyzw/source/Vector2';
import Rectangle2 from 'twodee/source/Rectangle2';
const box = Rectangle2.AABB([
new Vector2([0.0, 0.0]),
new Vector2([0.0, 1.0]),
new Vector2([1.0, 0.0])
]);
const transform = box.transform;
const extend = box.extend;
const center = box.center;
const width = box.width;
const height = box.height;
const aspect = box.aspect;
const area = box.area;
Rectangles can test for collisions with points, segments and other rectangles
###Creating a segmented line
import PolyLine2 from 'twodee/source/PolyLine2';
const lineA = PolyLine2.Rectangle2(box);
const points = lineA.point;
const segments = lineA.segments;
const closed = lineA.closed;
Segmented lines can test for collisions with points, line segments and other segment lines. All line intersection test guarantee valid results for parallel and co-linear entities.
const lineB = PolyLine2.ConvexHullGraham([
new Vector2([0.0, 0.0]),
new Vector2([0.0, 1.0]),
new Vector2([1.0, 0.0]),
new Vector2([1.0, 1.0])
]);
const intersections = [];
const collision = lineB.intersects(lineA, intersections);
###Creating a Polygon
import Polygon2 from 'twodee/source/Polygon2';
const poly = new Polygon2();
const v0 = poly.createVertex(new Vector2([0.0, 0.0]));
const v1 = poly.createVertex(new Vector2([0.0, 1.0]));
const v2 = poly.createVertex(new Vector2([1.0, 0.0]));
const v3 = poly.createVertex(new Vector2([1.0, 1.0]));
const f0 = poly.createFace(v0, v1, v2);
const f1 = poly.createFace(v1, v3, v2);
const center = poly.centroid;
const area = poly.area;
const vertices = poly.vertex;
const edges = poly.edge;
const faces = poly.face;
const point = poly.point;
const drawList = poly.indexList;
Polygons expose a rich api for geometry manipulations:
const [e0] = poly.edgeOfVertex(v1, [v2]);
const e1 = poly.turnEdge(e0);
const [f2, f3] = poly.faceOfEdge(e1);
const [f4, f5, f6] = poly.subdivideFace(f3);
Polygons can test for collisions with Points and other Polygons
const polyB = Polygon2.PolyLine2(lineB);
const intersections = [];
const collision = poly.intersects(polyB);
- Circle2
- CubicBezier2
- Polygon2
- constructor
- define
- face
- edge
- vertex
- point
- indexList
- centroid
- area
- hasFace
- edgeOfFace
- vertexOfFace
- pointOfFace
- hasEdge
- faceOfEdge
- vertexOfEdge
- pointOfEdge
- hasVertex
- faceOfVertex
- edgeOfVertex
- pointOfVertex
- hasPoint
- pointAt
- vertexOfPoint
- createFace
- removeFace
- createVertex
- removeVertex
- clearIsolatedVertices
- subdivideFace
- splitEdge
- turnEdge
- intersectsPoint
- intersects
- transformationOf
- copyOf
- transformation
- toJSON
- Define
- JSON
- Points
- PolyLine2
- Copy
- PolyLine2
- Ray2
- Rectangle2
- CW
- CCW
- DEGENERATE
- Triangle2
- TriangleSubdivisionTree
Circle geometry
Creates a new instance
Parameters
p
Vector2 The center pointr
number The radius
The center point
The radius
Redefines the instance
Parameters
p
Vector2 The center pointr
Vector2 The radius
Returns Circle2
The area
Returns true if p intersects the instance, false otherwise
Parameters
p
Vector2 The point
Returns boolean
Returns true if circle intersects the instance, false otherwise
Parameters
Returns boolean
The transformation of circle
Parameters
circle
Circle2 The sourcetransform
Matrix3 The transform
Returns Circle2
The copy of circle
Parameters
circle
Circle2 The source
Returns Circle2
The transformation of the instance
Parameters
transform
Matrix3 The transform
Returns Circle2
Returns a string representation of the instance
Parameters
digits
int The decimal places
Returns string
Returns an instance representing p and r
Parameters
Returns Circle2
Returns an instance at p with area a
Parameters
Returns Circle2
Returns a copy of circle
Parameters
Returns Circle2
Returns true if q intersects circle (p,r), false otherwise
Parameters
p
Vector2 The circle centerr
number The circle radiusq
Vector2 The point
Returns boolean
Returns true if circle (p0,r0) intersects circle (p1,r1), false otherwise
Parameters
p0
Vector2 The first circle centerr0
number The first circle radiusp1
Vector2 The second circle centerr1
number The second circle radiuspoints
Array<Vector2>? The intersection points
Returns boolean
Returns true if a and b are equal (a == b), false otherwise
Parameters
Returns boolean
Cubic Bezier Curve
Creates an instance
Parameters
p0
Vector2p1
Vector2p2
Vector2p3
Vector2
Defines the instance
Parameters
p0
Vector2p1
Vector2p2
Vector2p3
Vector2
Returns CubicBezier2
Returns the point at t
Parameters
t
number The position
Returns Vector2
The copy of source
Parameters
source
CubicBezier2 The source bezier
Returns CubicBezier2
Returns a copy of source
Parameters
source
CubicBezier2 The source curvetarget
CubicBezier2? The target curve
Returns CubicBezier2
Return the point at t of cubic bezier curve p0,p1,p2,p3
Parameters
p0
Vector2p1
Vector2p2
Vector2p3
Vector2t
Number The position
Returns Vector2
Returns the partial segments of source split at t
Parameters
source
CubicBezier2 The source curvet
number The position
Returns Array<CubicBezier2>
Returns true if a and b represent the same curve, false otherwise
Parameters
Returns boolean
Planar triangle mesh
Creates a new instance
Redefines the instance
Returns Polygon2
The dereferenced defined face indices of the instance
The dereferenced defined edge indices of the instance
The dereferenced defined vertex indices of the instance
The deferenced list of points of the instance
The dereferenced display list of vertex indices of the instance
The dereferenced centroid point
The area sum((1/2)|AB x AC|)
Returns true if face is a defined face index, false otherwise
Parameters
face
int The face index
Returns boolean
Returns a ccw-ordered Array of edge indices associated with face
Parameters
face
int The face indexvertex
int? The first ccw vertex index of the first edge index
Returns Array<int>
Returns a ccw-ordered Array of vertex indices associated with face
Parameters
face
int The face indexedge
int? The edge index of the first ccw vertex index
Returns Array<int>
Returns the ccw ordered points associated with face Proxies Polygon2#vertexOfFace
Parameters
face
int The face indexedge
int? The edge index of the first ccw vertex index
Returns Array<Vector2>
Returns true if edge is a defined edge index, false otherwise
Parameters
edge
int The edge index
Returns boolean
Returns a front,back sorted Array of face indices associated with edge
Parameters
edge
int The edge indexface
int? The second face index
Returns Array<int>
Returns a from,to sorted Array of vertex indices associated with edge
Parameters
edge
int The edge indexvertex
int? The second vertex index
Returns Array<int>
Returns the from, to ordered points associated with edge Proxies Polygon2#vertexOfEdge
Parameters
edge
int The edgevertex
int? The second vertex
Returns Array<Vector2>
Returns true if vertex is a defined vertex index, false otherwise
Parameters
vertex
int The vertex index
Returns boolean
Returns an Array of face indices associated with vertex
Parameters
vertex
int The vertex indexconstraint
Array<int>? Array of complementary face vertex indices
Returns Array<int>
Returns an Array of edge indices associated with vertex
Parameters
vertex
int The vertex indexconstraint
Array<int>? Array of complementary edge vertex indices
Returns Array<int>
Returns the point associated with vertex
Parameters
vertex
int The vertex
Returns Vector2
Returns true if point is a defined point, false otherwise
Parameters
point
Vector2 The point
Returns boolean
Returns the point at coordinates of point if found, null otherwise
Parameters
point
Vector2 The point
Returns (Vector2 | null)
Returns the vertex index associated with point
Parameters
point
Vector2 The point
Returns int
Returns the index of the face created between vertex0, vertex1 and vertex2
Parameters
vertex0
int The first vertexvertex1
int The second vertexvertex2
int The third vertex
Returns int
Removes face
Parameters
face
int The face index
Returns the index of the vertex created from p
Parameters
p
Vector2 The point
Returns int
Removes vertex and all associated faces
Parameters
vertex
int
Removes all isolated vertices
Returns Polygon2
Returns the subdivision vertex index of the faces created by subdividing face
Parameters
face
int The source face indexpoint
Vector2? The subdivision point
Returns int
Returns the split vertex index of the edges created by splitting edge
Parameters
edge
int The source edge indexpoint
Vector2? The splitting point
Returns int
Returns the index of the edge created by turning edge
Parameters
edge
int The edge
Returns int
Returns true if the instance intersects with q, false otherwise
Parameters
Returns boolean
Returns true if the instance intersects with poly, false otherwise
Parameters
poly
Polygon2 The antagonist
Returns boolean
The transformation of poly
Parameters
poly
Polygon2 The sourcetransform
Matrix3 The transform
Returns Polygon2
The copy of poly
Parameters
poly
Polygon2 The source polygon
Returns Polygon2
The transformation of the instance
Parameters
transform
Matrix3 the transform
Returns Polygon2
Returns a json reprentation of the instance
Returns {f: Array<int>, p: Array<float>}
Returns a defined instance
Parameters
target
Polygon2? The target instance
Returns Polygon2
Returns an instance created from json
Parameters
Returns Polygon2
Returns an instance from points Using TriangleSubdivisionTree
Parameters
points
Array<Vector2> The points
Returns Polygon2
Returns an instance from outline Using TriangleSubdivisionTree
Parameters
outline
PolyLine2 The outline
Returns Polygon2
Returns a copy of poly
Parameters
Returns Polygon2
Planar geometric primitive, first order
Creates a new instance
Parameters
point
Array<Vector2> The points
The points
Redefines the instance
Parameters
point
Array<Vector2>? The points
Returns PolyLine2
The number of segments
true if the first and last points are identical (===), false otherwise
Returns true if the instance intersects with point, false otherwise Alias for PolyLine2.intersectsPoint
Parameters
point
Vector2 The antagonist
Returns boolean
Returns true if the instance intersects with poly, false otherwise Alias for PolyLine2.intersect
Parameters
poly
PolyLine2 The antagonistpoint
Array<Vector2>? The intersection points References the intersection points if polylines intersect
Returns boolean
The transformation of poly
Parameters
poly
PolyLine2 The sourcetransform
Matrix3 The transform
Returns PolyLine2
The copy of poly
Parameters
poly
PolyLine2 The source
Returns PolyLine2
The transformation of the instance
Parameters
transform
Matrix3 The transform
Returns PolyLine2
Returns a string representation of the instance
Returns string
Returns a new instance from the convex hull of point Using graham scanning
Parameters
Returns PolyLine2
Returns a new instance from rectangle
Parameters
rectangle
Rectangle2 The sourcetarget
PolyLine2? The target instance
Returns PolyLine2
Returns an instance representing the transformation of poly
Parameters
Returns Polyline2
Returns a copy of poly
Parameters
Returns PolyLine2
Returns true if point q0 intersects poly line pN, false otherwise Using the crossings test (RRp754)
Parameters
pN
Array<Vector2> The poly line segmentsq0
Vector2 The point
Returns boolean
Returns true if segment (p0,p1) intersects segment (q0,q1), false otherwise (RRp781)
Parameters
p0
Vector2 The first point of the first segmentp1
Vector2 The second point of the first segmentq0
Vector2 The first point of the second segmentq1
Vector2 The second point of the second segmentr
Vector2? The intersection point References the intersection point if segments intersect
Returns boolean
Returns true if pN intersects qN, false otherwise
Parameters
pN
Array<Vector2> The first array of pointsqN
Array<Vector2> The second array of pointsr
Array<Vector2>? The intersection point(s) References the intersection point(s) if primitives intersect
Returns boolean
Planar Ray
Creates a new instance
Parameters
origin
Vector2 The ray originorientation
Vector2 The orientation
The origin
The orientation
Redefines the instance
Parameters
origin
Vector2 The ray originorientation
Vector2 The orientation
Returns Ray2
Returns true if the instance intersects line segment (p0, p1), false otherwise
Parameters
p0
Vector2 The first point of the segmentp1
Vector2 The second point of the segmentr
Vector2? The intersection point
Returns boolean
Returns true if the instance intersects ray, false otherwise
Parameters
ray
Ray2 The antagonistr
Vector2? The intersection point
Returns boolean
The copy of ray
Parameters
ray
Ray2 The source
Returns Ray2
Returns a string representation of the instance
Parameters
digits
int? The decimal digits (optional, default3
)
Returns string
Returns a defined instance
Parameters
origin
Vector2 The ray originorientation
Vector2 The ray orientationtarget
Ray2? The target instance
Returns Ray2
Returns true if ray (pa,oa) intersects line segment (q0,q1), false otherwise
Parameters
pa
Vector2 The ray originoa
Vector2 The ray orientationq0
Vector2 The first point of the line segmentq1
Vector2 The second point of the line segmentr
Vector2? The intersection point
Returns boolean
Returns true if ray (pa,oa) and ray (pb,ob) intersect, false otherwise
Parameters
pa
Vector2 The origin of the first rayoa
Vector2 The orientation of the first raypb
Vector2 The origin of the second rayob
Vector2 The orientation of the second rayr
Vector2? The intersection point
Returns boolean
Returns true if a and b represent the same ray (a == b), false otherwise
Parameters
Returns boolean
Planar geometric primitive, second order
Returns true if a and b represent the same rectangle, false otherwise
Parameters
a
Rectangle2 The protagonistb
Rectangle2 The antagonist
Returns boolean
Creates a new instance
Parameters
transform
Matrix3? The transformextend
Vector2? The extend
The transform
The half-dimensions
Redefines the instance
Parameters
transform
Matrix3? The transformextend
Vector2? The half-dimensions
Returns Rectangle2
The dereferenced center point
The width Alias of Rectangle2#extend
The height Alias of Rectangle#extend
The aspect (w/h)
The area (w*h)
Returns true if the instance intersects with p, false otherwise Alias for Rectangle2.intersectPoint
Parameters
p
Vector2 The antagonist
Returns boolean
Returns true if the instance intersects with segment (p0,p1), false otherwise Alias of Polyline2.intersect
Parameters
p0
Vector2 The first point of the antagonistp1
Vector2 The second point of the antagonistr
Array<Vector2>? The intersection points References the intersection points if instances intersect
Returns boolean
Returns true if the instance intersects with rectangle, false otherwise Alias of Rectangle2.intersect
Parameters
rectangle
Rectangle2 The antagonistpoint
Rectangle2? The intersection point(s) References the intersection point(s) if obbs intersect
Returns boolean
The transformation of rectangle
Parameters
rectangle
Rectangle2 The sourcetransform
Matrix3 The transform
Returns Rectangle2
The copy of rectangle
Parameters
rectangle
Rectangle2 The source
Returns Rectangle2
The transformation of the instance
Parameters
transform
Matrix3 The transform
Returns Rectangle2
Returns a string representation of the instance
Returns string
Defines an instance
Parameters
transform
Matrix3 The transformextend
Vector2 The extendtarget
Rectangle2? The target instance
Returns Rectangle2
Returns a new instance from w, h
Parameters
w
number The widthh
number The heighttarget
Rectangle2? The target instance
Returns a new instance from point
Parameters
point
Array<Vector2> The pointstarget
Rectangle2? The target instance
Returns Rectangle2
Returns an instance representing the transformation of rectangle
Parameters
rectangle
Rectangle2 The sourcetransform
Matrix3 The transformtarget
Rectangle2? The target instance
Returns Rectangle2
Returns a copy of rectangle
Parameters
rectangle
Rectangle2 The sourcetarget
Rectangle2? The target instance
Returns Rectangle2
Returns true if obb (tA,eA) intersects with point (p), false otherwise
Parameters
tA
Matrix3 The transform of the obbeA
Vector2 The half-dimensions of the obbp
Vector2 The point
Returns boolean
Returns true if obb (t, e) intersects with triangle(p0,p1,p2)
Parameters
t
Matrix3 The obb transforme
Vector2 The obb extendp0
Vector2 The first point of the trianglep1
Vector2 The second point of the trianglep2
Vector2 The third point of the triangle
Returns boolean
Returns true if obb (tA,eA) intersects with obb (tB,eB), false otherwise OBB intersection test using the seperating axis method by Gottschalk et. al. [RRp767]
Parameters
tA
Matrix3 The transform of the first obbeA
Vector2 The half-dimensions of the first obbtB
Matrix3 The transform of the second obbeB
Vector2 The half-dimensions of the second obb
Returns boolean
The clockwise orientation sign
Type: number
The counter-clockwise orientation sign
The degenerate orientation sign
Type: number
Planar geometric primitive, second order
Creates a new instance
Parameters
p0
Vector2? The first pointp1
Vector2? The second pointp2
Vector2? The third point
The first point
The second point
The third point
Redefines the instance
Parameters
p0
Vector2? The first pointp1
Vector2? The second pointp2
Vector2? The third point
Returns Triangle2
CW (1) if the instance is cw rotated, CCW (-1) if the instance is ccw rotated, DEGENERATE (0) if the instance is degenerate
The dereferenced centroid point
The dereferenced center of the enclosing circle
The area (1/2)|AB x AC|
Returns true if the instance intersects with point (q), false otherwise Alias of Triangle2.intersectPoint
Parameters
q
Vector2 The pointuv
Array<number>? Array holding the barycentric (u,v) coordinates References the barycentric intersection coordinates(u,v) if primitives intersect
Returns boolean
Returns true if the instance intersects with segment (q0,q1), false otherwise Alias of Triangle2.intersectSegment
Parameters
q0
Vector2 The first point of the segmentq1
Vector2 The second point of the segmentr
Array<Vector2>? The intersection point(s) References the intersection points if instances intersect
Returns boolean
Returns true if the instance intersects with triangle, false otherwise Alias of Triangle2.intersect
Parameters
triangle
Triangle2 The opposing Trianglepoint
Array<Vector2>? The intersection point(s) References the intersection points if instances intersect
Returns boolean
The transformation of triangle
Parameters
triangle
Triangle2 The sourcetransform
Matrix3 The transform
Returns Triangle2
The copy of triangle
Parameters
triangle
Triangle2 The source
Returns Triangle2
The transformation of the instance
Parameters
transform
Matrix3 The transform
Returns Triangle2
Returns a string representation of the instance
Parameters
digits
int? The decimal places (optional, default3
)
Returns string
Returns an instance representing the equilateral triangle circumscribed|inscribed by r, rotated by rad
Parameters
p
Vector2 The centroid pointr
number The distance between centroid and pointrad
number? The angle (optional, default0.0
)f
boolean? The inscription factor (optional, default0.0
)target
Triangle2? The target instance
Returns Triangle2
Returns an instance representing the transformation of triangle
Parameters
Returns Triangle2
Returns a copy of triangle
Parameters
Returns Triangle2
Returns a Vector2 representing the centroid of triangle (p0,p1,p2)
Parameters
p0
Vector2 The first pointp1
Vector2 The second pointp2
Vector2 The third point
Returns Vector2
Returns a Vector2 representing the circumcenter of triangle (p0,p1,p2)
Parameters
p0
Vector2 The first pointp1
Vector2 The second pointp2
Vector2 The third point
Returns Vector2
Returns the area (1/2)|AB x AC| of triangle (p0,p1,p2)
Parameters
p0
Vector2 The first pointp1
Vector2 The second pointp2
Vector2 The third point
Returns number
Returns true if the circumcircle of ccw triangle (p0,p1,p2) intersects with point (q0), false otherwise
Parameters
p0
Vector2 The first point of the trianglep1
Vector2 The second point of the trianglep2
Vector2 The third point of the triangleq0
Vector2 The antagonist
Returns boolean
Returns true if triangle (p0,p1,p2) intersects with point (q0), false otherwise
Parameters
p0
Vector2 The first point of the trianglep1
Vector2 The second point of the trianglep2
Vector2 The third point of the triangleq
Vector2 The pointuv
Array<number>? Array holding the barycentric (u,v) coordinates References the barycentric intersection coordinates(u,v) if primitives intersect
Returns boolean
Returns true if triangle (p0,p1,p2) intersects with segment (q0,q1), false otherwise
Parameters
p0
Vector2 The first point of the trianglep1
Vector2 The second point of the trianglep2
Vector2 The third point of the triangleq0
Vector2 The first point of the segmentq1
Vector2 The second point of the segmentr
Array<Vector2>? The intersection point(s) References the intersection points if primitives intersect
Returns boolean
Returns true if triangle (p0,p1,p2) intersects with triangle(q0,q1,q2), false otherwise
Parameters
p0
Vector2 The first point of the first trianglep1
Vector2 The second point of the first trianglep2
Vector2 The third point of the first triangleq0
Vector2 The first point of the second triangleq1
Vector2 The second point of the second triangleq2
Vector2 The third point of the second triangler
Array<Vector2>? The intersection point(s) References the intersection point(s) if triangles intersect
Returns boolean
Delaunay triangulation subdivision tree
Creates a new instance
Parameters
boundary
Triangle2 The boundary triangle
Returns a dereferenced polygon representing the subdivision state
Returns Polygon2
Returns the intersection face and barycentric coordinate if q intersects with the subdivision tree, null otherwise
Parameters
q
Vector2 The point
Returns (null | Object)
Returns true if edge is the optimal edge for the quad of face0 and the face opposite of edge, false otherwise
Parameters
face0
int The face index of the first faceedge
int The edge index of the edge
Returns boolean
Adds a point to the subdivision mesh
Parameters
point
Vector2 The point
Adds points to the subdivision mesh
Parameters
points
Array<Vector2> The points
Intersects outline with the subdivision mesh
Parameters
outline
PolyLine2 The outline