This chapter introduces geometry calculation methods in WebCAD, including common operations such as distance, angle, and intersection calculations.
import { Point2D, distance } from 'vjcad';
const p1 = new Point2D(0, 0);
const p2 = new Point2D(100, 100);
// Distance between two points
const dist = distance(p1, p2); // ~141.42
function distance(p1: Point2D, p2: Point2D): number {
const dx = p2.x - p1.x;
const dy = p2.y - p1.y;
return Math.sqrt(dx * dx + dy * dy);
}
function pointToLineDistance(
point: Point2D,
lineStart: Point2D,
lineEnd: Point2D
): number {
const A = point.x - lineStart.x;
const B = point.y - lineStart.y;
const C = lineEnd.x - lineStart.x;
const D = lineEnd.y - lineStart.y;
const dot = A * C + B * D;
const lenSq = C * C + D * D;
let param = -1;
if (lenSq !== 0) param = dot / lenSq;
let xx, yy;
if (param < 0) {
xx = lineStart.x;
yy = lineStart.y;
} else if (param > 1) {
xx = lineEnd.x;
yy = lineEnd.y;
} else {
xx = lineStart.x + param * C;
yy = lineStart.y + param * D;
}
const dx = point.x - xx;
const dy = point.y - yy;
return Math.sqrt(dx * dx + dy * dy);
}
import { getAngleBetweenPoints, normalizeAngle } from 'vjcad';
const p1 = new Point2D(0, 0);
const p2 = new Point2D(100, 100);
// Angle between two points (radians)
const angle = getAngleBetweenPoints(p1, p2); // ~0.785 (45 degrees)
// Normalize angle to [0, 2π)
const normalized = normalizeAngle(angle);
// Radians to degrees
const degrees = radians * 180 / Math.PI;
// Degrees to radians
const radians = degrees * Math.PI / 180;
import { getMidPoint } from 'vjcad';
const p1 = new Point2D(0, 0);
const p2 = new Point2D(100, 100);
const mid = getMidPoint(p1, p2); // Point2D(50, 50)
import { polarToCartesian } from 'vjcad';
// Create point from polar coordinates
const center = new Point2D(0, 0);
const angle = Math.PI / 4; // 45 degrees
const distance = 100;
const endPoint = polarToCartesian(center, angle, distance);
// endPoint ≈ Point2D(70.71, 70.71)
GeometryCalculator is a static utility class that provides many geometry calculation methods, especially for intersection calculations.
Use the cross product to determine which side of a line a point lies on:
import { GeometryCalculator, Point2D } from 'vjcad';
const lineStart = new Point2D(0, 0);
const lineEnd = new Point2D(100, 0); // Horizontal line
const testPoint = new Point2D(50, 50);
// Return value: -1 = left side, 0 = on line, 1 = right side
const side = GeometryCalculator.witchSidePointToLine(lineStart, lineEnd, testPoint);
// side = 1 (point is on the right / above the line)
Normalize an angle into the range [0, 2π):
const angle1 = GeometryCalculator.adjustAngle(-Math.PI / 2); // 3π/2
const angle2 = GeometryCalculator.adjustAngle(3 * Math.PI); // π
// Check whether two numbers are equal within a specified tolerance
const isEqual = GeometryCalculator.eq(1.0001, 1.0002, 0.001); // true
// Determine the position of a value relative to two bounds
const position = GeometryCalculator.pos(50, 0, 100);
// Returns: "center" | "Left" | "Right" | "EqualLeft" | "EqualRight"
GeometryCalculator provides more than 100 static intersection methods that support many combinations of geometric entities.
Intersection methods follow the naming pattern {EntityA}To{EntityB}:
| Suffix | Description |
|---|
Line | Line segment (finite length) |
LineExt | Extended line segment (infinite extension) |
Arc | Arc (with angle range) |
ArcExt | Extended arc (full circle) |
Circle | Circle |
Ray | Ray (one-way infinite) |
RayExt | Extended ray |
Xline | Construction line (infinite in both directions) |
Pline | Polyline |
PlineExt | Extended polyline |
Hatch | Hatch pattern |
import { GeometryCalculator, LineEnt, Point2D } from 'vjcad';
const line1 = new LineEnt(new Point2D(0, 0), new Point2D(100, 100));
const line2 = new LineEnt(new Point2D(0, 100), new Point2D(100, 0));
// Segment to segment
const intersections = GeometryCalculator.LineToLine(line1, line2);
// Returns Point2D[], here [Point2D(50, 50)]
// Extended line to segment
GeometryCalculator.LineExtToLine(line1, line2);
// Two extended lines (infinite lines)
GeometryCalculator.LineExtToLineExt(line1, line2);
// Segment to extended line
GeometryCalculator.LineToLineExt(line1, line2);
import { GeometryCalculator, LineEnt, ArcEnt, Point2D } from 'vjcad';
const line = new LineEnt(new Point2D(0, 50), new Point2D(100, 50));
const arc = new ArcEnt(new Point2D(50, 50), 30, 0, Math.PI);
// Segment to arc
const intersections = GeometryCalculator.LineToArc(line, arc);
// Extended line to arc
GeometryCalculator.LineExtToArc(line, arc);
// Segment to extended arc (full circle)
GeometryCalculator.LineToArcExt(line, arc);
// Extended line to extended arc
GeometryCalculator.LineExtToArcExt(line, arc);
import { GeometryCalculator, LineEnt, CircleEnt, Point2D } from 'vjcad';
const line = new LineEnt(new Point2D(0, 50), new Point2D(100, 50));
const circle = new CircleEnt(new Point2D(50, 50), 30);
// Segment to circle
const intersections = GeometryCalculator.LineToCircle(line, circle);
// Extended line to circle
GeometryCalculator.LineExtToCircle(line, circle);
// Circle to segment
GeometryCalculator.CircleToLine(circle, line);
// Circle to extended line
GeometryCalculator.CircleToLineExt(circle, line);
import { GeometryCalculator, ArcEnt, CircleEnt, Point2D } from 'vjcad';
const arc1 = new ArcEnt(new Point2D(0, 0), 50, 0, Math.PI / 2);
const arc2 = new ArcEnt(new Point2D(50, 0), 50, Math.PI / 2, Math.PI);
const circle = new CircleEnt(new Point2D(25, 25), 30);
// Arc to arc
GeometryCalculator.ArcToArc(arc1, arc2);
// Arc to circle
GeometryCalculator.ArcToCircle(arc1, circle);
// Extended arc to arc
GeometryCalculator.ArcExtToArc(arc1, arc2);
// Extended arc to extended arc (two full circles)
GeometryCalculator.ArcExtToArcExt(arc1, arc2);
// Extended arc to circle
GeometryCalculator.ArcExtToCircle(arc1, circle);
import { GeometryCalculator, CircleEnt, Point2D } from 'vjcad';
const circle1 = new CircleEnt(new Point2D(0, 0), 50);
const circle2 = new CircleEnt(new Point2D(60, 0), 50);
// Circle to circle (up to 2 intersections)
const intersections = GeometryCalculator.CircleToCircle(circle1, circle2);
import { GeometryCalculator, RayEnt, LineEnt, ArcEnt, CircleEnt, Point2D } from 'vjcad';
const ray = new RayEnt(new Point2D(0, 0), Math.PI / 4); // 45-degree ray
const line = new LineEnt(new Point2D(50, 0), new Point2D(50, 100));
// Ray to line
GeometryCalculator.RayToLine(ray, line);
// Ray to circle
GeometryCalculator.RayToCircle(ray, circle);
// Ray to arc
GeometryCalculator.RayToArc(ray, arc);
// Ray to ray
GeometryCalculator.RayToRay(ray1, ray2);
// Extended-ray related
GeometryCalculator.RayExtToLine(ray, line);
GeometryCalculator.RayExtToCircle(ray, circle);
GeometryCalculator.RayToRayExt(ray1, ray2);
import { GeometryCalculator, XLineEnt, LineEnt, Point2D } from 'vjcad';
const xline = new XLineEnt(new Point2D(0, 50), Math.PI / 4);
const line = new LineEnt(new Point2D(0, 0), new Point2D(100, 100));
// Construction line to line
GeometryCalculator.XlineToLine(xline, line);
// Construction line to circle
GeometryCalculator.XlineToCircle(xline, circle);
// Construction line to arc
GeometryCalculator.XlineToArc(xline, arc);
// Construction line to ray
GeometryCalculator.XlineToRay(xline, ray);
// Construction line to construction line
GeometryCalculator.XlineToXline(xline1, xline2);
import { GeometryCalculator, PolylineEnt, LineEnt, CircleEnt, Point2D } from 'vjcad';
const pline = new PolylineEnt();
// ... set polyline vertices
// Line to polyline
GeometryCalculator.LineToPline(line, pline);
// Extended line to polyline
GeometryCalculator.LineExtToPline(line, pline);
// Circle to polyline
GeometryCalculator.CircleToPline(circle, pline);
// Arc to polyline
GeometryCalculator.ArcToPline(arc, pline);
// Polyline to polyline
GeometryCalculator.PlineToPline(pline1, pline2);
// Extended polyline related
GeometryCalculator.PlineExtToLine(pline, line);
GeometryCalculator.PlineToPlineExt(pline1, pline2);
import { GeometryCalculator, HatchEnt, LineEnt, RayEnt, Point2D } from 'vjcad';
// Line to hatch
GeometryCalculator.LineToHatch(line, hatch);
// Extended line to hatch
GeometryCalculator.LineExtToHatch(line, hatch);
// Ray to hatch
GeometryCalculator.RayToHatch(ray, hatch);
// Hatch to line (reverse parameter order)
GeometryCalculator.HatchToLine(hatch, line);
GeometryCalculator.HatchToRay(hatch, ray);
| Method | Description |
|---|
LineToLine | Line segment to line segment |
LineToLineExt | Line segment to extended line segment |
LineToArc | Line segment to arc |
LineToArcExt | Line segment to extended arc |
LineToCircle | Line segment to circle |
LineToRay | Line segment to ray |
LineToRayExt | Line segment to extended ray |
LineToXline | Line segment to construction line |
LineToPline | Line segment to polyline |
LineToPlineExt | Line segment to extended polyline |
LineToHatch | Line segment to hatch |
| Method | Description |
|---|
LineExtToLine | Extended line segment to line segment |
LineExtToLineExt | Extended line segment to extended line segment |
LineExtToArc | Extended line segment to arc |
LineExtToArcExt | Extended line segment to extended arc |
LineExtToCircle | Extended line segment to circle |
LineExtToRay | Extended line segment to ray |
LineExtToRayExt | Extended line segment to extended ray |
LineExtToXline | Extended line segment to construction line |
LineExtToPline | Extended line segment to polyline |
LineExtToPlineExt | Extended line segment to extended polyline |
LineExtToHatch | Extended line segment to hatch |
| Method | Description |
|---|
ArcToArc | Arc to arc |
ArcToArcExt | Arc to extended arc |
ArcToCircle | Arc to circle |
ArcToLine | Arc to line segment |
ArcToLineExt | Arc to extended line segment |
ArcToRay | Arc to ray |
ArcToRayExt | Arc to extended ray |
ArcToXline | Arc to construction line |
ArcToPline | Arc to polyline |
ArcToPlineExt | Arc to extended polyline |
| Method | Description |
|---|
ArcExtToArc | Extended arc to arc |
ArcExtToArcExt | Extended arc to extended arc |
ArcExtToCircle | Extended arc to circle |
ArcExtToLine | Extended arc to line segment |
ArcExtToLineExt | Extended arc to extended line segment |
ArcExtToRay | Extended arc to ray |
ArcExtToRayExt | Extended arc to extended ray |
ArcExtToXline | Extended arc to construction line |
ArcExtToPline | Extended arc to polyline |
ArcExtToPlineExt | Extended arc to extended polyline |
| Method | Description |
|---|
CircleToCircle | Circle to circle |
CircleToArc | Circle to arc |
CircleToArcExt | Circle to extended arc |
CircleToLine | Circle to line segment |
CircleToLineExt | Circle to extended line segment |
CircleToRay | Circle to ray |
CircleToRayExt | Circle to extended ray |
CircleToXline | Circle to construction line |
CircleToPline | Circle to polyline |
CircleToPlineExt | Circle to extended polyline |
| Method | Description |
|---|
RayToRay | Ray to ray |
RayToRayExt | Ray to extended ray |
RayToLine | Ray to line segment |
RayToLineExt | Ray to extended line segment |
RayToArc | Ray to arc |
RayToArcExt | Ray to extended arc |
RayToCircle | Ray to circle |
RayToXline | Ray to construction line |
RayToPline | Ray to polyline |
RayToPlineExt | Ray to extended polyline |
RayToHatch | Ray to hatch |
| Method | Description |
|---|
XlineToXline | Construction line to construction line |
XlineToLine | Construction line to line segment |
XlineToLineExt | Construction line to extended line segment |
XlineToArc | Construction line to arc |
XlineToArcExt | Construction line to extended arc |
XlineToCircle | Construction line to circle |
XlineToRay | Construction line to ray |
XlineToRayExt | Construction line to extended ray |
XlineToPline | Construction line to polyline |
| Method | Description |
|---|
PlineToPline | Polyline to polyline |
PlineToPlineExt | Polyline to extended polyline |
PlineToLine | Polyline to line segment |
PlineToLineExt | Polyline to extended line segment |
PlineToArc | Polyline to arc |
PlineToArcExt | Polyline to extended arc |
PlineToCircle | Polyline to circle |
PlineToRay | Polyline to ray |
PlineToRayExt | Polyline to extended ray |
PlineToXline | Polyline to construction line |