Intersection Calculation Functions
Intersection Calculation Functions
This chapter explains the intersection functions in the IntersectionCalculator module in detail, including algorithm principles and usage examples.
Overview
IntersectionCalculator provides a set of low-level intersection functions used together with the static methods in GeometryCalculator. These functions support more flexible parameters, including lightweight geometry objects.
import {
getArcLineIntersectionsEx,
getCircleLineIntersectionsAdvanced,
getCircleCircleIntersections,
getComplexLineIntersection,
getLineEntityIntersections
} from 'vjcad';Line-Related Intersections
getComplexLineIntersection() - Complex Line Intersection
Calculates the intersection of two lines according to different intersection modes, supporting segments, extended segments, infinite lines, and more.
function getComplexLineIntersection(
firstLine: LineEnt | ILineGeometry,
secondLine: LineEnt | ILineGeometry,
intersectionType: number
): Point2D | undefinedParameters
| Parameter | Type | Description |
|---|---|---|
firstLine | LineEnt | ILineGeometry | First line |
secondLine | LineEnt | ILineGeometry | Second line |
intersectionType | number | Intersection type (see table below) |
Intersection Types
| Value | Description |
|---|---|
0 | Segment to segment intersection (both endpoints lie on opposite sides of the other segment) |
1 | Extended first segment to second segment |
2 | First segment to extended second segment |
3 | Infinite-line intersection, ignoring endpoint limits |
Example
import { getComplexLineIntersection, 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));
// Type 0: segment intersection
const intersection = getComplexLineIntersection(line1, line2, 0);
// intersection = Point2D(50, 50)
// Type 3: infinite-line intersection
const line3 = new LineEnt(new Point2D(0, 0), new Point2D(10, 10));
const line4 = new LineEnt(new Point2D(100, 0), new Point2D(100, 10));
const extIntersection = getComplexLineIntersection(line3, line4, 3);
// extIntersection = Point2D(100, 100)Algorithm
- Check whether the two lines are parallel by comparing normalized angles
- Handle special cases such as vertical and horizontal lines
- Compute the general-case intersection using the slope-intercept formula
- Validate the intersection according to the selected intersection type
getLineLineIntersections()
Calculate intersections between a line segment and a ray / construction line:
import { getLineLineIntersections, LineEnt, Point2D } from 'vjcad';
const line = new LineEnt(new Point2D(0, 0), new Point2D(100, 100));
const ray = { basePoint: new Point2D(50, 0), farPoint: new Point2D(50, 100) };
const intersections = getLineLineIntersections(line, ray);
// Returns Point2D[]getLineExtLineExtIntersections()
Intersection of two extended lines (infinite lines):
import { getLineExtLineExtIntersections, LineEnt, Point2D } from 'vjcad';
const line1 = new LineEnt(new Point2D(0, 0), new Point2D(10, 10));
const ray = { basePoint: new Point2D(100, 0), farPoint: new Point2D(100, 10) };
const intersections = getLineExtLineExtIntersections(line1, ray);Circle and Line Intersections
getCircleLineIntersectionsAdvanced() - Advanced Version
Accurately computes intersections between a circle and a line, handling all boundary cases such as tangency, separation, and normal intersection.
function getCircleLineIntersectionsAdvanced(
circleEntity: CircleEnt | ICircleGeometry,
lineEntity: LineEnt | ILineGeometry
): Point2D[]Algorithm
- Rotate the line to a horizontal position to simplify calculation
- Compute the distance from the line to the circle center
- Use inverse trigonometry to calculate the intersection angles
- Rotate the result back into the original coordinate system
- Validate whether intersections fall within the segment range
Example
import { getCircleLineIntersectionsAdvanced, CircleEnt, LineEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 30);
const line = new LineEnt(new Point2D(0, 50), new Point2D(100, 50));
const intersections = getCircleLineIntersectionsAdvanced(circle, line);
// intersections = [Point2D(20, 50), Point2D(80, 50)]getCircleLineIntersectionsSimple() - Simple Version
Computes intersections between a circle and an infinite line, without considering segment endpoint limits:
import { getCircleLineIntersectionsSimple, CircleEnt, LineEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 30);
const line = new LineEnt(new Point2D(0, 50), new Point2D(10, 50));
// Even if the segment is short, intersections with the extended line can still be computed
const intersections = getCircleLineIntersectionsSimple(circle, line);getLineCircleIntersections()
Intersections between a line and a circle, considering ray direction:
import { getLineCircleIntersections, 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);
const intersections = getLineCircleIntersections(line, circle);Circle-to-Circle Intersections
getCircleCircleIntersections()
Calculates the intersections of two circles. It uses Heron's formula to compute the triangle area and determine intersection locations.
function getCircleCircleIntersections(
firstCircle: CircleEnt | ICircleGeometry,
secondCircle: CircleEnt | ICircleGeometry
): Point2D[]Positional Relationships
| Case | Number of Intersections |
|---|---|
Separate circles (d > r1 + r2) | 0 |
Externally tangent (d = r1 + r2) | 1 |
| Intersecting (` | r1 - r2 |
| Internally tangent (`d = | r1 - r2 |
| One inside another (`d < | r1 - r2 |
Concentric (d = 0) | 0 |
Algorithm
- Compute the distance
dbetween the two circle centers - Determine the positional relationship
- Use Heron's formula to compute the triangle area formed by the two centers and an intersection point
- Use the triangle height to calculate intersection coordinates
- Rotate back into the original coordinate system
Example
import { getCircleCircleIntersections, CircleEnt, Point2D } from 'vjcad';
const circle1 = new CircleEnt(new Point2D(0, 0), 50);
const circle2 = new CircleEnt(new Point2D(60, 0), 50);
const intersections = getCircleCircleIntersections(circle1, circle2);
// Returns two intersections
console.log(intersections.length); // 2Arc-Related Intersections
getArcLineIntersectionsEx()
Calculates intersections between an arc and a line, then filters only the intersections that lie within the arc angle range.
function getArcLineIntersectionsEx(
arcEntity: ArcEnt | IArcGeometry,
lineEntity: LineEnt | ILineGeometry
): Point2D[]Algorithm
- First compute intersections between the full circle underlying the arc and the line
- Compute the angle of each intersection relative to the arc center
- Check whether the angle lies within the arc's angular range
- Return only the points that lie on the arc
Example
import { getArcLineIntersectionsEx, ArcEnt, LineEnt, Point2D } from 'vjcad';
// Create upper semicircle arc
const arc = new ArcEnt(new Point2D(50, 50), 30, 0, Math.PI);
const line = new LineEnt(new Point2D(0, 50), new Point2D(100, 50));
const intersections = getArcLineIntersectionsEx(arc, line);
// Returns only intersections that lie on the arcgetCircleArcIntersectionsFiltered()
Calculate circle-to-arc intersections, filtered by arc angle range:
import { getCircleArcIntersectionsFiltered, CircleEnt, ArcEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(0, 0), 50);
const arc = new ArcEnt(new Point2D(60, 0), 50, Math.PI / 2, Math.PI);
const intersections = getCircleArcIntersectionsFiltered(circle, arc);getArcCircleIntersections()
Intersections between an arc and a circle:
import { getArcCircleIntersections, ArcEnt, CircleEnt, Point2D } from 'vjcad';
const arc = new ArcEnt(new Point2D(0, 0), 50, 0, Math.PI);
const circle = new CircleEnt(new Point2D(60, 0), 50);
const intersections = getArcCircleIntersections(arc, circle);Entity-Collection Intersections
These functions calculate the intersections between a single geometry entity and an entity collection, and are used in commands such as trim and extend.
getLineEntityIntersections()
Calculate intersections between a line and all entities in an entity collection:
function getLineEntityIntersections(
lineEntity: LineEnt | ILineGeometry,
entityCollection: IEntityCollection
): Point2D[]Example
import { getLineEntityIntersections, LineEnt, PolylineEnt, Point2D } from 'vjcad';
const line = new LineEnt(new Point2D(0, 50), new Point2D(200, 50));
const pline = new PolylineEnt();
// ... configure polyline
const intersections = getLineEntityIntersections(line, pline);
// Returns all intersections between the line and every segment / arc in the polylinegetArcEntityIntersections()
Calculate intersections between an arc and an entity collection:
import { getArcEntityIntersections, ArcEnt, PolylineEnt, Point2D } from 'vjcad';
const arc = new ArcEnt(new Point2D(50, 50), 30, 0, Math.PI);
const intersections = getArcEntityIntersections(arc, pline);getCircleEntityIntersections()
Calculate intersections between a circle and an entity collection, full version:
import { getCircleEntityIntersections, CircleEnt, Point2D } from 'vjcad';
const circle = new CircleEnt(new Point2D(50, 50), 30);
// Supports arcs, circles, lines, and rays
const intersections = getCircleEntityIntersections(circle, entityCollection);getCircleEntityIntersectionsSimple()
Calculate intersections between a circle and an entity collection, simplified version supporting only arcs and lines:
import { getCircleEntityIntersectionsSimple } from 'vjcad';
const intersections = getCircleEntityIntersectionsSimple(circle, pline);Extended-Entity Intersections
getLineExtendedEntityIntersections()
Calculate intersections between a line and an extended entity collection, supporting arcs, circles, lines, and rays:
import { getLineExtendedEntityIntersections } from 'vjcad';
// entityCollection.getEnts_Ext() returns an array of extended entities
const intersections = getLineExtendedEntityIntersections(line, entityCollection);getLineExtExtendedEntityIntersections()
Calculate intersections between an extended line and an extended entity collection:
import { getLineExtExtendedEntityIntersections } from 'vjcad';
const intersections = getLineExtExtendedEntityIntersections(line, entityCollection);getExtendedEntityPolylineIntersections()
Calculate intersections between an extended entity collection and a polyline:
import { getExtendedEntityPolylineIntersections, PolylineEnt } from 'vjcad';
const pline = new PolylineEnt();
// ...
const intersections = getExtendedEntityPolylineIntersections(entityCollection, pline);Helper Functions
getLineIntersections()
Calculate line intersections with an entity array and generate line segments:
import { getLineIntersections, LineEnt, Point2D } from 'vjcad';
const line = new LineEnt(new Point2D(0, 0), new Point2D(100, 100));
const entities = [arc1, circle1, line2];
// Returns an array of segments formed by intersection points (lightweight `GLine` objects)
const segments = getLineIntersections(line, entities);Interface Definitions
ILineGeometry
interface ILineGeometry {
startPoint: Point2D;
endPoint: Point2D;
angle: number;
}IArcGeometry
interface IArcGeometry {
center: Point2D;
radius: number;
startAng: number;
endAng: number;
innerAng?: number;
}ICircleGeometry
interface ICircleGeometry {
center: Point2D;
radius: number;
}IEntityCollection
interface IEntityCollection {
subGeometries?: SubGeometry[];
subEnts?: (LineEnt | ArcEnt | CircleEnt | EllipseEnt)[];
getSubEnts?: () => (LineEnt | ArcEnt | CircleEnt | EllipseEnt)[];
getEnts_Ext?: () => (LineEnt | ArcEnt | CircleEnt | RayEnt)[];
}Recommendations
- Choose the right function: use the
AdvancedorSimpleversion depending on whether endpoint limits need to be considered - Use lightweight interfaces: if full entity functionality is unnecessary, pass plain objects matching the required interface
- Handle empty results: all functions return arrays and may return an empty array, so check length before use
- Deduplicate if needed: polyline intersections may contain duplicates; use
removeDuplicatePointswhen necessary
Next Steps
- Area Calculation - polygon area algorithms
- Boundary Detection - using
BoundaryDetector - Selection Detection - selection-rectangle intersection tests