#twodee
Two dimensional geometry manipulation
##Install
$ npm install twodee##Use
###Creating a simple Triangle
twodee uses xyzw for vector manipulation.
$ npm install xyzwimport 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
pVector2 The center pointrnumber The radius
The center point
The radius
Redefines the instance
Parameters
pVector2 The center pointrVector2 The radius
Returns Circle2
The area
Returns true if p intersects the instance, false otherwise
Parameters
pVector2 The point
Returns boolean
Returns true if circle intersects the instance, false otherwise
Parameters
Returns boolean
The transformation of circle
Parameters
circleCircle2 The sourcetransformMatrix3 The transform
Returns Circle2
The copy of circle
Parameters
circleCircle2 The source
Returns Circle2
The transformation of the instance
Parameters
transformMatrix3 The transform
Returns Circle2
Returns a string representation of the instance
Parameters
digitsint 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
pVector2 The circle centerrnumber The circle radiusqVector2 The point
Returns boolean
Returns true if circle (p0,r0) intersects circle (p1,r1), false otherwise
Parameters
p0Vector2 The first circle centerr0number The first circle radiusp1Vector2 The second circle centerr1number The second circle radiuspointsArray<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
p0Vector2p1Vector2p2Vector2p3Vector2
Defines the instance
Parameters
p0Vector2p1Vector2p2Vector2p3Vector2
Returns CubicBezier2
Returns the point at t
Parameters
tnumber The position
Returns Vector2
The copy of source
Parameters
sourceCubicBezier2 The source bezier
Returns CubicBezier2
Returns a copy of source
Parameters
sourceCubicBezier2 The source curvetargetCubicBezier2? The target curve
Returns CubicBezier2
Return the point at t of cubic bezier curve p0,p1,p2,p3
Parameters
p0Vector2p1Vector2p2Vector2p3Vector2tNumber The position
Returns Vector2
Returns the partial segments of source split at t
Parameters
sourceCubicBezier2 The source curvetnumber 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
faceint The face index
Returns boolean
Returns a ccw-ordered Array of edge indices associated with face
Parameters
faceint The face indexvertexint? 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
faceint The face indexedgeint? The edge index of the first ccw vertex index
Returns Array<int>
Returns the ccw ordered points associated with face Proxies Polygon2#vertexOfFace
Parameters
faceint The face indexedgeint? The edge index of the first ccw vertex index
Returns Array<Vector2>
Returns true if edge is a defined edge index, false otherwise
Parameters
edgeint The edge index
Returns boolean
Returns a front,back sorted Array of face indices associated with edge
Parameters
edgeint The edge indexfaceint? The second face index
Returns Array<int>
Returns a from,to sorted Array of vertex indices associated with edge
Parameters
edgeint The edge indexvertexint? The second vertex index
Returns Array<int>
Returns the from, to ordered points associated with edge Proxies Polygon2#vertexOfEdge
Parameters
edgeint The edgevertexint? The second vertex
Returns Array<Vector2>
Returns true if vertex is a defined vertex index, false otherwise
Parameters
vertexint The vertex index
Returns boolean
Returns an Array of face indices associated with vertex
Parameters
vertexint The vertex indexconstraintArray<int>? Array of complementary face vertex indices
Returns Array<int>
Returns an Array of edge indices associated with vertex
Parameters
vertexint The vertex indexconstraintArray<int>? Array of complementary edge vertex indices
Returns Array<int>
Returns the point associated with vertex
Parameters
vertexint The vertex
Returns Vector2
Returns true if point is a defined point, false otherwise
Parameters
pointVector2 The point
Returns boolean
Returns the point at coordinates of point if found, null otherwise
Parameters
pointVector2 The point
Returns (Vector2 | null)
Returns the vertex index associated with point
Parameters
pointVector2 The point
Returns int
Returns the index of the face created between vertex0, vertex1 and vertex2
Parameters
vertex0int The first vertexvertex1int The second vertexvertex2int The third vertex
Returns int
Removes face
Parameters
faceint The face index
Returns the index of the vertex created from p
Parameters
pVector2 The point
Returns int
Removes vertex and all associated faces
Parameters
vertexint
Removes all isolated vertices
Returns Polygon2
Returns the subdivision vertex index of the faces created by subdividing face
Parameters
faceint The source face indexpointVector2? The subdivision point
Returns int
Returns the split vertex index of the edges created by splitting edge
Parameters
edgeint The source edge indexpointVector2? The splitting point
Returns int
Returns the index of the edge created by turning edge
Parameters
edgeint 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
polyPolygon2 The antagonist
Returns boolean
The transformation of poly
Parameters
polyPolygon2 The sourcetransformMatrix3 The transform
Returns Polygon2
The copy of poly
Parameters
polyPolygon2 The source polygon
Returns Polygon2
The transformation of the instance
Parameters
transformMatrix3 the transform
Returns Polygon2
Returns a json reprentation of the instance
Returns {f: Array<int>, p: Array<float>}
Returns a defined instance
Parameters
targetPolygon2? The target instance
Returns Polygon2
Returns an instance created from json
Parameters
Returns Polygon2
Returns an instance from points Using TriangleSubdivisionTree
Parameters
pointsArray<Vector2> The points
Returns Polygon2
Returns an instance from outline Using TriangleSubdivisionTree
Parameters
outlinePolyLine2 The outline
Returns Polygon2
Returns a copy of poly
Parameters
Returns Polygon2
Planar geometric primitive, first order
Creates a new instance
Parameters
pointArray<Vector2> The points
The points
Redefines the instance
Parameters
pointArray<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
pointVector2 The antagonist
Returns boolean
Returns true if the instance intersects with poly, false otherwise Alias for PolyLine2.intersect
Parameters
polyPolyLine2 The antagonistpointArray<Vector2>? The intersection points References the intersection points if polylines intersect
Returns boolean
The transformation of poly
Parameters
polyPolyLine2 The sourcetransformMatrix3 The transform
Returns PolyLine2
The copy of poly
Parameters
polyPolyLine2 The source
Returns PolyLine2
The transformation of the instance
Parameters
transformMatrix3 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
rectangleRectangle2 The sourcetargetPolyLine2? 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
pNArray<Vector2> The poly line segmentsq0Vector2 The point
Returns boolean
Returns true if segment (p0,p1) intersects segment (q0,q1), false otherwise (RRp781)
Parameters
p0Vector2 The first point of the first segmentp1Vector2 The second point of the first segmentq0Vector2 The first point of the second segmentq1Vector2 The second point of the second segmentrVector2? The intersection point References the intersection point if segments intersect
Returns boolean
Returns true if pN intersects qN, false otherwise
Parameters
pNArray<Vector2> The first array of pointsqNArray<Vector2> The second array of pointsrArray<Vector2>? The intersection point(s) References the intersection point(s) if primitives intersect
Returns boolean
Planar Ray
Creates a new instance
Parameters
originVector2 The ray originorientationVector2 The orientation
The origin
The orientation
Redefines the instance
Parameters
originVector2 The ray originorientationVector2 The orientation
Returns Ray2
Returns true if the instance intersects line segment (p0, p1), false otherwise
Parameters
p0Vector2 The first point of the segmentp1Vector2 The second point of the segmentrVector2? The intersection point
Returns boolean
Returns true if the instance intersects ray, false otherwise
Parameters
rayRay2 The antagonistrVector2? The intersection point
Returns boolean
The copy of ray
Parameters
rayRay2 The source
Returns Ray2
Returns a string representation of the instance
Parameters
digitsint? The decimal digits (optional, default3)
Returns string
Returns a defined instance
Parameters
originVector2 The ray originorientationVector2 The ray orientationtargetRay2? The target instance
Returns Ray2
Returns true if ray (pa,oa) intersects line segment (q0,q1), false otherwise
Parameters
paVector2 The ray originoaVector2 The ray orientationq0Vector2 The first point of the line segmentq1Vector2 The second point of the line segmentrVector2? The intersection point
Returns boolean
Returns true if ray (pa,oa) and ray (pb,ob) intersect, false otherwise
Parameters
paVector2 The origin of the first rayoaVector2 The orientation of the first raypbVector2 The origin of the second rayobVector2 The orientation of the second rayrVector2? 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
aRectangle2 The protagonistbRectangle2 The antagonist
Returns boolean
Creates a new instance
Parameters
transformMatrix3? The transformextendVector2? The extend
The transform
The half-dimensions
Redefines the instance
Parameters
transformMatrix3? The transformextendVector2? 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
pVector2 The antagonist
Returns boolean
Returns true if the instance intersects with segment (p0,p1), false otherwise Alias of Polyline2.intersect
Parameters
p0Vector2 The first point of the antagonistp1Vector2 The second point of the antagonistrArray<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
rectangleRectangle2 The antagonistpointRectangle2? The intersection point(s) References the intersection point(s) if obbs intersect
Returns boolean
The transformation of rectangle
Parameters
rectangleRectangle2 The sourcetransformMatrix3 The transform
Returns Rectangle2
The copy of rectangle
Parameters
rectangleRectangle2 The source
Returns Rectangle2
The transformation of the instance
Parameters
transformMatrix3 The transform
Returns Rectangle2
Returns a string representation of the instance
Returns string
Defines an instance
Parameters
transformMatrix3 The transformextendVector2 The extendtargetRectangle2? The target instance
Returns Rectangle2
Returns a new instance from w, h
Parameters
wnumber The widthhnumber The heighttargetRectangle2? The target instance
Returns a new instance from point
Parameters
pointArray<Vector2> The pointstargetRectangle2? The target instance
Returns Rectangle2
Returns an instance representing the transformation of rectangle
Parameters
rectangleRectangle2 The sourcetransformMatrix3 The transformtargetRectangle2? The target instance
Returns Rectangle2
Returns a copy of rectangle
Parameters
rectangleRectangle2 The sourcetargetRectangle2? The target instance
Returns Rectangle2
Returns true if obb (tA,eA) intersects with point (p), false otherwise
Parameters
tAMatrix3 The transform of the obbeAVector2 The half-dimensions of the obbpVector2 The point
Returns boolean
Returns true if obb (t, e) intersects with triangle(p0,p1,p2)
Parameters
tMatrix3 The obb transformeVector2 The obb extendp0Vector2 The first point of the trianglep1Vector2 The second point of the trianglep2Vector2 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
tAMatrix3 The transform of the first obbeAVector2 The half-dimensions of the first obbtBMatrix3 The transform of the second obbeBVector2 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
p0Vector2? The first pointp1Vector2? The second pointp2Vector2? The third point
The first point
The second point
The third point
Redefines the instance
Parameters
p0Vector2? The first pointp1Vector2? The second pointp2Vector2? 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
qVector2 The pointuvArray<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
q0Vector2 The first point of the segmentq1Vector2 The second point of the segmentrArray<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
triangleTriangle2 The opposing TrianglepointArray<Vector2>? The intersection point(s) References the intersection points if instances intersect
Returns boolean
The transformation of triangle
Parameters
triangleTriangle2 The sourcetransformMatrix3 The transform
Returns Triangle2
The copy of triangle
Parameters
triangleTriangle2 The source
Returns Triangle2
The transformation of the instance
Parameters
transformMatrix3 The transform
Returns Triangle2
Returns a string representation of the instance
Parameters
digitsint? The decimal places (optional, default3)
Returns string
Returns an instance representing the equilateral triangle circumscribed|inscribed by r, rotated by rad
Parameters
pVector2 The centroid pointrnumber The distance between centroid and pointradnumber? The angle (optional, default0.0)fboolean? The inscription factor (optional, default0.0)targetTriangle2? 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
p0Vector2 The first pointp1Vector2 The second pointp2Vector2 The third point
Returns Vector2
Returns a Vector2 representing the circumcenter of triangle (p0,p1,p2)
Parameters
p0Vector2 The first pointp1Vector2 The second pointp2Vector2 The third point
Returns Vector2
Returns the area (1/2)|AB x AC| of triangle (p0,p1,p2)
Parameters
p0Vector2 The first pointp1Vector2 The second pointp2Vector2 The third point
Returns number
Returns true if the circumcircle of ccw triangle (p0,p1,p2) intersects with point (q0), false otherwise
Parameters
p0Vector2 The first point of the trianglep1Vector2 The second point of the trianglep2Vector2 The third point of the triangleq0Vector2 The antagonist
Returns boolean
Returns true if triangle (p0,p1,p2) intersects with point (q0), false otherwise
Parameters
p0Vector2 The first point of the trianglep1Vector2 The second point of the trianglep2Vector2 The third point of the triangleqVector2 The pointuvArray<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
p0Vector2 The first point of the trianglep1Vector2 The second point of the trianglep2Vector2 The third point of the triangleq0Vector2 The first point of the segmentq1Vector2 The second point of the segmentrArray<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
p0Vector2 The first point of the first trianglep1Vector2 The second point of the first trianglep2Vector2 The third point of the first triangleq0Vector2 The first point of the second triangleq1Vector2 The second point of the second triangleq2Vector2 The third point of the second trianglerArray<Vector2>? The intersection point(s) References the intersection point(s) if triangles intersect
Returns boolean
Delaunay triangulation subdivision tree
Creates a new instance
Parameters
boundaryTriangle2 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
qVector2 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
face0int The face index of the first faceedgeint The edge index of the edge
Returns boolean
Adds a point to the subdivision mesh
Parameters
pointVector2 The point
Adds points to the subdivision mesh
Parameters
pointsArray<Vector2> The points
Intersects outline with the subdivision mesh
Parameters
outlinePolyLine2 The outline