manual.md
February 24, 2023 ยท View on GitHub
Style
Represents the styling (eg stroke, fill, width) of a 2D or 3D object
const style = Style({stroke:'red'});
style['stroke'] = 'green'; // change it
Value
Represents a generic scalar value which can change dynamically (not used directly)
Matrix2D, 2D Homogeneous Transformation Matrix
Represents a homogeneous transformation matrix for 2D transforms
const m = Matrix2D.translate(tx, ty).mul(Matrix2D.rotate(theta).mul(Matrix2D.scale(sx, sy)));
const invm = m.inv();
// p is a point, p2 is a transformed point
const p2 = m.transform(p);
Object2D, Base Class
Represents a generic 2D object (not used directly)
Properties:
-
id: Stringunique ID for this object -
name: Stringclass/type name of object, eg"Object2D" -
matrix: Matrix2Dthe transform matrix of the object (if it applies) -
style: Stylethe style applied to this object
Methods:
-
clone(): Object2Dget a copy of this object -
transform(matrix2d: Matrix2D): Object2Dget a transformed copy of this object by matrix2d -
setMatrix(matrix2D): selfset matrix for object -
setStyle(style): selfset style for object -
setStyle(prop, value): selfset style property/value for object -
getBoundingBox(): Object{xmin,ymin,xmax,ymax}get bounding box of object -
getConvexHull(): Point2D[]get points of convex hull enclosing object -
getCenter(): Object{x,y}get center of object -
hasPoint(point): Boolcheck if given point is part of the boundary of this object -
hasInsidePoint(point, strict): Boolcheck if given point is part of the interior of this object (where applicable) -
intersects(other): Point2D[]|Boolreturn array of intersection points with other 2d object or false -
intersectsSelf(): Point2D[]|Boolreturn array of intersection points of object with itself or false -
toSVG(): Stringrender object as SVG string -
toSVGPath(): Stringrender object as SVG path string -
toCanvas(ctx): voidrender object in canvas context -
toTex(): Stringget Tex representation of this object -
toString(): Stringget String representation of this object
Point2D (subclass of Object2D)
Represents a point in 2D space
const p = Point2D(x, y);
p.x += 10; // change it
p.y = 5; // change it
Methods:
-
eq(point: Point2D|Object{x,y}|[x,y]): Booldetermine if equals another point-like -
add(other: Point2D): Point2Dadd points coordinate-wise -
add(other: Number): Point2Dadd number to point coordinates -
mul(other: Number): Point2Dmultiply number to point coordinates -
dot(other: Point2D): Numberdot product of points -
cross(other: Point2D): Numbercross product of points -
angle(other: Point2D): Numberangle between points -
between(p1: Point2D, p1: Point2D): Boolcheck if point is on line segment defined by points p1,p2 -
distanceToLine(p1: Point2D, p1: Point2D): Numberdistance of point to line defined by points p1,p2
Topos2D (subclass of Object2D)
Represents a geometric topos, ie a set of points
const topos = Topos2D([p1, p2, p3, .., pn]);
Properties:
points: Point2D[]the points that define this topos
Curve2D (subclass of Topos2D)
Represents a generic curve in 2D space (not used directly)
Properties:
-
length: Numberthe length of the curve -
area: Numberthe area enclosed by the curve
Methods:
-
isConnected(): Booltrue if curve is a connected curve (eg a line) -
isClosed(): Booltrue if curve is a closed curve (eg a circle) -
isConvex(): Booltrue if curve is convex (eg a convex polygon) -
getPointAt(t: Number): Point2Dget point on curve at position specified by parametert (0 <= t <= 1) -
curveUpTo(t: Number): Curve2Dget curve up to point specified by parametert (0 <= t <= 1) -
derivative(): Curve2Dget derivative of curve as curve -
polylinePoints(): Object{x,y}[]get points of polyline that approximates the curve -
bezierPoints(t: Number = 1): Object{x,y}[]get points of cubic bezier curves that approximate the curve (optionally up to point specified by parametert)
Bezier2D (subclass of Curve2D)
Represents a generic Bezier curve in 2D space (not used directly)
EllipticArc2D (subclass of Curve2D)
Represents a part of an arbitrary ellipse in 2D space (not used directly)
ParametricCurve (subclass of Curve2D)
Represents a generic parametric curve in 2D space
// construct a spiral (0 <= t <= 1)
const spiral = ParametricCurve((t) => ({x: cx + t*r*Math.cos(t*6*Math.PI), y: cy + t*r*Math.sin(t*6*Math.PI)}));
CompositeCurve (subclass of Curve2D)
Represents a container of multiple, not necessarily joined curves
// construct a complex curve
const curve = CompositeCurve([Line(p1, p2), QBezier([p3, p4, p5]), Line(p6, p7)]);
Properties:
curves: Curve2D[]array of curves that define this composite curve
Line (equivalent to Linear Bezier, subclass of Bezier2D)
Represents a line segment between 2 points
const line = Line(start, end);
line.start.x += 10; // change it
line.end.y = 20; // change it
Methods:
-
distanceToPoint(p: Point2D): Numberdistance of point to this line segment -
isParallelTo(l: Line): Booldetermine if line is parallel to line l -
isParallelTo(p: Point2D, q: Point2D): Booldetermine if line is parallel to line defined by points p,q -
isPerpendicularTo(l: Line): Booldetermine if line is perpendicular to line l -
isPerpendicularTo(p: Point2D, q: Point2D): Booldetermine if line is perpendicular to line defined by points p,q
QBezier (subclass of Bezier2D)
Represents a quadratic Bezier curve defined by its control points
const qbezier = QBezier([p1, p2, p3]);
qbezier.points[0].x += 10; // change it
qbezier.points[1].x = 20; // change it
CBezier (subclass of Bezier2D)
Represents a cubic Bezier curve defined by its control points
const cbezier = CBezier([p1, p2, p3, p4]);
cbezier.points[0].x += 10; // change it
cbezier.points[2].x = 20; // change it
Polyline (subclass of Curve2D)
Represents an assembly of consecutive line segments between given points
const polyline = Polyline([p1, p2, .., pn]);
polyline.points[0].x += 10; // change it
polyline.points[2].x = 20; // change it
Polygon (subclass of Curve2D)
Represents a polygon (a closed polyline) defined by its vertices
const polygon = Polygon([p1, p2, .., pn]);
polygon.vertices[0].x += 10; // change it
polygon.vertices[2].x = 20; // change it
Arc (subclass of EllipticArc2D)
Represents an elliptic arc between start and end (points) having radiusX, radiusY and rotation angle and given largeArc and sweep flags
const arc = Arc(start, end, radiusX, radiusY, angle, largeArc, sweep);
arc.start.x += 10; // change it
arc.radiusX = 12; // change it
arc.largeArc = false; // change it
Circle (subclass of EllipticArc2D)
Represents a circle of given center (point) and radius
const circle = Circle(center, radius);
circle.center.x += 10; // change it
circle.radius = 12; // change it
Ellipse (subclass of EllipticArc2D)
Represents an ellipse of given center (point), radiusX, radiusY and rotation angle
const ellipse = Ellipse(center, radiusX, radiusY, angle);
ellipse.center.x += 10; // change it
ellipse.radiusX = 12; // change it
Shape2D (subclass of Object2D)
container for 2D geometric objects, grouped together
// construct a complex shape
const shape = Shape2D([Line(p1, p2), Line(p6, p7), Shape2D([Line(p3, p4), Line(p5, p6)])]);
Properties:
objects: Object2D[]array of objects that are part of this shape
Scene2D
scene container for 2D geometric objects
const scene = Scene2D(containerEl, viewBoxMinX, viewBoxMinY, viewBoxMaxX, viewBoxMaxY);
scene.x0 = 20; // change viewport
scene.x1 = 100; // change viewport
scene.y0 = 10; // change viewport
scene.y1 = 200; // change viewport
const line = Line([p1, p2]);
scene.add(line); // add object
scene.remove(line); // remove object
scene.getIntersections(); // return array of points of intersection of all objects in the scene
scene.toSVG(); // render and return scene as SVG string
scene.toCanvas(); // render and return scene as Canvas
scene.toIMG(); // render and return scene as base64 encoded PNG image
Utilities
Geometry
Geometry utilities:
-
linearBezierCurve(t: Number, points: Object{x,y}[]): Object{x,y}get point on linear Bezier curve att, 0 <= t <= 1given its control points -
quadraticBezierCurve(t: Number, points: Object{x,y}[]): Object{x,y}get point on quadratic Bezier curve att, 0 <= t <= 1given its control points -
cubicBezierCurve(t: Number, points: Object{x,y}[]): Object{x,y}get point on cubic Bezier curve att, 0 <= t <= 1given its control points -
ellipticArcCurve(t: Number, cx: Number=0, cy: Number=0, rx: Number=1, ry: Number=rx, angle: Number=0): Object{x,y}get point on elliptic arc curve att, 0 <= t <= 1given its center, radii and angle of rotation -
computeConvexHull(points: Object{x,y}[]): Point2D[]compute convex hull of points
Math
Math utilities:
-
deg(x)radians to degrees -
rad(x)degrees to radians -
hypot(x, y)hypotenuse -
solveLinear(a, b)solve linear equationax+b=0 -
solveQuadratic(a, b, c)solve quadratic equationax^2+bx+c=0 -
solveCubic(a, b, c, d)solve cubic equationax^3+bx^2+cx+d=0 -
solveQuartic(a, b, c, d, e)solve quartic equationax^4+bx^3+cx^2+dx+e=0 -
solveLinearLinear(a, b, c, d, e, f)solve system of 2 linear equations in 2 unknownsax+by+c=0dx+ey+f=0 -
solveLinearQuadratic(n, m, k, a, b, c, d, e, f)solve system of a linear and a quadratic equation in 2 unknownsnx+my+k=0ax^2+by^2+cxy+dx+ey+f=0 -
solveQuadraticQuadratic(a1, b1, c1, d1, e1, f1, a2, b2, c2, d2, e2, f2)solve system of 2 quadratic equations in 2 unknownsa1x^2+b1y^2+c1xy+d1x+e1y+f1=0a2x^2+b2y^2+c2xy+d2x+e2y+f2=0