@ue-too/math / index / PointCal
クラス: PointCal
定義: index.ts:89
Utility class for point and vector calculations.
Remarks
PointCal provides static methods for common 2D and 3D mathematical operations used in canvas applications. All methods handle both 2D and 3D coordinates seamlessly.
例
import { PointCal, Point } from '@ue-too/math';
const v1: Point = { x: 1, y: 2 };
const v2: Point = { x: 3, y: 4 };
const sum = PointCal.addVector(v1, v2);
const dot = PointCal.dotProduct(v1, v2);コンストラクター
コンストラクター
new PointCal():
PointCal
戻り値
PointCal
Angle Utilities
angleFromA2B()
staticangleFromA2B(a,b):number
定義: index.ts:439
Calculates the signed angle from vector a to vector b.
パラメータ
a
First vector (starting direction)
b
Second vector (ending direction)
戻り値
number
The signed angle in radians, range: (-π, π]
Remarks
- Positive angles indicate counter-clockwise rotation from a to b
- Negative angles indicate clockwise rotation from a to b
- Uses atan2 for proper quadrant handling
例
const right = { x: 1, y: 0 };
const up = { x: 0, y: 1 };
const angle = PointCal.angleFromA2B(right, up); // π/2 (90 degrees CCW)
const down = { x: 0, y: -1 };
const angleDown = PointCal.angleFromA2B(right, down); // -π/2 (90 degrees CW)Geometric Calculations
distanceBetweenPoints()
staticdistanceBetweenPoints(a,b):number
定義: index.ts:497
Calculates the Euclidean distance between two points.
パラメータ
a
First point
b
Second point
戻り値
number
The distance between the two points
Remarks
Equivalent to calculating the magnitude of the vector from a to b.
例
const a = { x: 0, y: 0 };
const b = { x: 3, y: 4 };
const distance = PointCal.distanceBetweenPoints(a, b); // 5getLineIntersection()
staticgetLineIntersection(startPoint,endPoint,startPoint2,endPoint2):object
定義: index.ts:630
Calculates the intersection point of two line segments.
パラメータ
startPoint
Start of first line segment
endPoint
End of first line segment
startPoint2
Start of second line segment
endPoint2
End of second line segment
戻り値
object
Object containing intersection status and details
intersection?
optionalintersection:Point
intersects
intersects:
boolean
offset?
optionaloffset:number
Remarks
Returns an object with:
intersects: Boolean indicating if segments intersectintersection: The intersection point (only if intersects is true)offset: Parameter t where intersection occurs on first segment (0 to 1)
The segments must actually cross within their bounds (not just their infinite extensions).
Use cases:
- Collision detection between line segments
- Ray casting and visibility checks
- Path intersection detection
例
const line1Start = { x: 0, y: 0 };
const line1End = { x: 10, y: 10 };
const line2Start = { x: 0, y: 10 };
const line2End = { x: 10, y: 0 };
const result = PointCal.getLineIntersection(line1Start, line1End, line2Start, line2End);
// { intersects: true, intersection: { x: 5, y: 5 }, offset: 0.5 }isEqual()
staticisEqual(a,b):boolean
定義: index.ts:585
Checks if two points are exactly equal.
パラメータ
a
First point
b
Second point
戻り値
boolean
True if all coordinates are exactly equal
Remarks
Uses strict equality (===) for comparison. For approximate equality with tolerance, use samePoint instead. Missing z-coordinates are treated as 0.
例
const a = { x: 1, y: 2 };
const b = { x: 1, y: 2 };
PointCal.isEqual(a, b); // true
const c = { x: 1.0000001, y: 2 };
PointCal.isEqual(a, c); // false (use samePoint for tolerance)linearInterpolation()
staticlinearInterpolation(a,b,t):point
定義: index.ts:549
Performs linear interpolation between two points.
パラメータ
a
Starting point (t = 0)
b
Ending point (t = 1)
t
number
Interpolation parameter (0 to 1)
戻り値
Interpolated point
Remarks
- t = 0 returns point a
- t = 1 returns point b
- t = 0.5 returns the midpoint
- Values outside [0, 1] perform extrapolation
Performance: Suitable for animation loops and real-time interpolation.
例
const a = { x: 0, y: 0 };
const b = { x: 10, y: 20 };
const mid = PointCal.linearInterpolation(a, b, 0.5); // { x: 5, y: 10 }
const quarter = PointCal.linearInterpolation(a, b, 0.25); // { x: 2.5, y: 5 }unitVectorFromA2B()
staticunitVectorFromA2B(a,b):Point
定義: index.ts:346
Calculates the unit vector pointing from point a to point b.
パラメータ
a
Starting point
b
Ending point
戻り値
Unit vector in the direction from a to b
Remarks
Equivalent to calling unitVector(subVector(b, a))
例
const a = { x: 0, y: 0 };
const b = { x: 3, y: 4 };
const direction = PointCal.unitVectorFromA2B(a, b); // { x: 0.6, y: 0.8 }Transformations
flipYAxis()
staticflipYAxis(point):Point
定義: index.ts:519
Flips a point's y-coordinate (mirrors across the x-axis).
パラメータ
point
Point to flip
戻り値
Point with negated y-coordinate
Remarks
Useful for converting between coordinate systems where the y-axis direction differs. Common when converting between screen coordinates (y-down) and mathematical coordinates (y-up).
例
const point = { x: 10, y: 20 };
const flipped = PointCal.flipYAxis(point); // { x: 10, y: -20 }rotatePoint()
staticrotatePoint(point,angle):Point
定義: index.ts:373
Rotates a point around the origin.
パラメータ
point
Point to rotate
angle
number
Rotation angle in radians (counter-clockwise)
戻り値
Rotated point
Remarks
Rotation is performed around the origin (0, 0). Positive angles rotate counter-clockwise, negative angles rotate clockwise. For rotation around a custom anchor, use transformPointWRTAnchor.
Performance: Uses trigonometric functions (sin/cos). For many rotations with the same angle, pre-calculate sin/cos values and apply the transformation manually.
例
const point = { x: 1, y: 0 };
const rotated = PointCal.rotatePoint(point, Math.PI / 2); // { x: 0, y: 1 }transform2NewAxis()
statictransform2NewAxis(point,angleFromOriginalAxis2DestAxis):Point
定義: index.ts:400
Transforms a point's coordinates to a new rotated axis system.
パラメータ
point
Point in original coordinate system
angleFromOriginalAxis2DestAxis
number
Rotation angle from original to destination axis (radians, CCW positive)
戻り値
Point coordinates in the new axis system
Remarks
This performs an axis rotation transformation, converting coordinates from one reference frame to another rotated by the specified angle.
例
const point = { x: 10, y: 0 };
const angle = Math.PI / 4; // 45 degrees
const transformed = PointCal.transform2NewAxis(point, angle);transformPointWRTAnchor()
statictransformPointWRTAnchor(point,anchor,angle):Point
定義: index.ts:468
Rotates a point around a custom anchor point.
パラメータ
point
Point to rotate
anchor
Anchor point to rotate around
angle
number
Rotation angle in radians (counter-clockwise)
戻り値
Rotated point
Remarks
This is equivalent to:
- Translate point by -anchor
- Rotate around origin
- Translate back by +anchor
例
const point = { x: 10, y: 5 };
const anchor = { x: 5, y: 5 };
const angle = Math.PI / 2; // 90 degrees
const rotated = PointCal.transformPointWRTAnchor(point, anchor, angle);
// Rotates point around anchor (5, 5)Vector Arithmetic
addVector()
staticaddVector(a,b):Point
定義: index.ts:115
Adds two vectors together.
パラメータ
a
First vector
b
Second vector
戻り値
The sum of vectors a and b
Remarks
If either vector lacks a z-coordinate, it's treated as 0. The result will include a z-coordinate if either input has one.
例
const a = { x: 1, y: 2 };
const b = { x: 3, y: 4 };
const sum = PointCal.addVector(a, b); // { x: 4, y: 6 }
// With 3D coordinates
const a3d = { x: 1, y: 2, z: 3 };
const b3d = { x: 4, y: 5, z: 6 };
const sum3d = PointCal.addVector(a3d, b3d); // { x: 5, y: 7, z: 9 }divideVectorByScalar()
staticdivideVectorByScalar(a,b):Point
定義: index.ts:190
Divides a vector by a scalar value.
パラメータ
a
Vector to divide
b
number
Scalar divisor
戻り値
The divided vector, or the original vector if b is 0
Remarks
Division by zero returns the original vector unchanged to prevent NaN values.
例
const v = { x: 10, y: 20 };
const divided = PointCal.divideVectorByScalar(v, 2); // { x: 5, y: 10 }multiplyVectorByScalar()
staticmultiplyVectorByScalar(a,b):Point
定義: index.ts:167
Multiplies a vector by a scalar value.
パラメータ
a
Vector to multiply
b
number
Scalar multiplier
戻り値
The scaled vector
例
const v = { x: 2, y: 3 };
const scaled = PointCal.multiplyVectorByScalar(v, 2.5); // { x: 5, y: 7.5 }subVector()
staticsubVector(a,b):Point
定義: index.ts:143
Subtracts vector b from vector a.
パラメータ
a
Vector to subtract from
b
Vector to subtract
戻り値
The difference (a - b)
Remarks
If either vector lacks a z-coordinate, it's treated as 0.
例
const a = { x: 5, y: 7 };
const b = { x: 2, y: 3 };
const diff = PointCal.subVector(a, b); // { x: 3, y: 4 }Vector Operations
crossProduct()
staticcrossProduct(a,b):Point
定義: index.ts:315
Calculates the cross product of two vectors.
パラメータ
a
First vector
b
Second vector
戻り値
The cross product vector perpendicular to both inputs
Remarks
The cross product is perpendicular to both input vectors, following the right-hand rule. For 2D vectors (z undefined), z is treated as 0.
Properties:
- Result is perpendicular to both input vectors
- Magnitude equals area of parallelogram formed by vectors
- Direction follows right-hand rule
例
const a = { x: 1, y: 0, z: 0 };
const b = { x: 0, y: 1, z: 0 };
const cross = PointCal.crossProduct(a, b); // { x: 0, y: 0, z: 1 }dotProduct()
staticdotProduct(a,b):number
定義: index.ts:281
Calculates the dot product of two vectors.
パラメータ
a
First vector
b
Second vector
戻り値
number
The dot product (scalar value)
Remarks
The dot product is: a.x * b.x + a.y * b.y + a.z * b.z
Use cases:
- Determine if vectors are perpendicular (dot = 0)
- Calculate angle between vectors: θ = acos(dot / (|a| * |b|))
- Project one vector onto another
例
const a = { x: 1, y: 0 };
const b = { x: 0, y: 1 };
const dot = PointCal.dotProduct(a, b); // 0 (perpendicular vectors)
const c = { x: 2, y: 3 };
const d = { x: 4, y: 5 };
const dot2 = PointCal.dotProduct(c, d); // 23magnitude()
staticmagnitude(a):number
定義: index.ts:216
Calculates the magnitude (length) of a vector.
パラメータ
a
Vector to measure
戻り値
number
The magnitude of the vector
Remarks
Uses the Euclidean distance formula: √(x² + y² + z²)
例
const v = { x: 3, y: 4 };
const mag = PointCal.magnitude(v); // 5
const v3d = { x: 1, y: 2, z: 2 };
const mag3d = PointCal.magnitude(v3d); // 3unitVector()
staticunitVector(a):Point
定義: index.ts:242
Converts a vector to its unit vector (normalized to length 1).
パラメータ
a
Vector to normalize
戻り値
Unit vector in the same direction, or zero vector if magnitude is 0
Remarks
A unit vector has magnitude 1 and preserves the original direction. Returns {x: 0, y: 0, z: 0} if the input vector has zero magnitude.
Performance note: This method calls magnitude() twice. For better performance when you need both magnitude and unit vector, calculate magnitude once and divide manually.
例
const v = { x: 3, y: 4 };
const unit = PointCal.unitVector(v); // { x: 0.6, y: 0.8 }