- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
import './pointExtras';
import './rectangleExtras';
import type { IPointData } from '@pixi/math';
import { Point } from '@pixi/math';
/**
* The idea of a relative epsilon comparison is to find the difference between the two numbers,
* and see if it is less than `Math.EPSILON`.
*
* _Note: Only available with **@pixi/math-extras**._
* @param {number} a - First floating number to compare.
* @param {number} b - Second floating number to compare.
* @returns {boolean} Returns `true` if the difference between the values is less than `Math.EPSILON`; otherwise `false`.
*/
export function floatEqual(a: number, b: number): boolean;
/**
* The idea of a relative epsilon comparison is to find the difference between the two numbers,
* and see if it is less than a given epsilon.
* A good epsilon would be the N% of the largest of the two values or `Math.EPSILON`.
*
* _Note: Only available with **@pixi/math-extras**._
* @memberof PIXI
* @param {number} a - First floating number to compare.
* @param {number} b - Second floating number to compare.
* @param {number} epsilon - The epsilon to compare to.
* The larger the epsilon, the easier for the numbers to be considered equals.
* @returns {boolean} Returns `true` if the difference between the values is less than the given epsilon;
* otherwise `false`.
*/
export function floatEqual(a: number, b: number, epsilon: number): boolean;
export function floatEqual(a: number, b: number, epsilon: number = Number.EPSILON): boolean
{
if (a === b)
{
return true;
}
const diff = Math.abs(a - b);
return diff < epsilon;
}
/**
* Generic line or segment intersection.
* A line can intersect outside the two points defining it, the segment can't.
* @param aStart - First point of the first line.
* @param aEnd - Second point of the first line.
* @param bStart - First point of the second line.
* @param bEnd - Second point of the second line.
* @param isLine - Set to true if you want Line (unbounded) intersection.
* @param {IPointData} [outPoint] - A Point-like object in which to store the value,
* optional (otherwise will create a new Point).
* @returns {IPointData} The point where the lines/segments intersect or a `NaN` Point.
*/
function genericLineIntersection<T extends IPointData>(
aStart: IPointData,
aEnd: IPointData,
bStart: IPointData,
bEnd: IPointData,
isLine: boolean,
outPoint?: T): T
{
if (!outPoint)
{
(outPoint as any) = new Point();
}
const dxa = aEnd.x - aStart.x;
const dya = aEnd.y - aStart.y;
const dxb = bEnd.x - bStart.x;
const dyb = bEnd.y - bStart.y;
// In order to find the position of the intersection in respect to the line segments, we can define lines
// in terms of first degree Bézier parameters, and find the two parameters `ua` and `ub` for the two lines to touch.
// both `ua` and `ub` formula share the same denominator so it is only calculated once.
const denominator = ((dyb * dxa) - (dxb * dya));
// If lines are parallel or overlapping, the intersection can be nowhere or everywhere... NaN.
if (floatEqual(denominator, 0))
{
outPoint.x = NaN;
outPoint.y = NaN;
return outPoint;
}
// ua is the factor of line a where the intersection occurs. ub is the factor of line b where the intersection occurs.
const ua = ((dxb * (aStart.y - bStart.y)) - (dyb * (aStart.x - bStart.x))) / denominator;
const ub = ((dxa * (aStart.y - bStart.y)) - (dya * (aStart.x - bStart.x))) / denominator;
// Line intersection extends beyond the bounds of the segment.
// The intersection is inside the segments if 0.0 ≤ ua ≤ 1.0 and 0.0 ≤ ub ≤ 1.0
if (!isLine && (ua < 0 || ua > 1 || ub < 0 || ub > 1))
{
outPoint.x = NaN;
outPoint.y = NaN;
return outPoint;
}
outPoint.x = aStart.x + (ua * dxa);
outPoint.y = bStart.y + (ub * dyb);
return outPoint;
}
/**
* Computes the point where non-coincident and non-parallel Lines intersect.
* Coincident or parallel lines return a `NaN` point `{x: NaN, y: NaN}`.
* The intersection point may land outside the extents of the lines.
*
* _Note: Only available with **@pixi/math-extras**._
* @param aStart - First point of the first line.
* @param aEnd - Second point of the first line.
* @param bStart - First point of the second line.
* @param bEnd - Second point of the second line.
* @returns {IPointData} The point where the lines intersect.
*/
export function lineIntersection(aStart: IPointData, aEnd: IPointData, bStart: IPointData, bEnd: IPointData): Point;
/**
* Computes the point where non-coincident and non-parallel Lines intersect.
* Coincident or parallel lines return a `NaN` point `{x: NaN, y: NaN}`.
* The intersection point may land outside the extents of the lines.
*
* _Note: Only available with **@pixi/math-extras**._
* @memberof PIXI
* @param aStart - First point of the first line.
* @param aEnd - Second point of the first line.
* @param bStart - First point of the second line.
* @param bEnd - Second point of the second line.
* @param {IPointData} outPoint - A Point-like object in which to store the value,
* optional (otherwise will create a new Point).
* @returns {IPointData} The point where the lines intersect or a `NaN` Point.
*/
export function lineIntersection
<T extends IPointData>(aStart: IPointData, aEnd: IPointData, bStart: IPointData, bEnd: IPointData, outPoint: T): T;
export function lineIntersection
<T extends IPointData>(aStart: IPointData, aEnd: IPointData, bStart: IPointData, bEnd: IPointData, outPoint?: T): T
{
return genericLineIntersection(aStart, aEnd, bStart, bEnd, true, outPoint);
}
/**
* Computes the point where non-coincident and non-parallel segments intersect.
* Coincident, parallel or non-intersecting segments return a `NaN` point `{x: NaN, y: NaN}`.
* The intersection point must land inside the extents of the segments or return a `NaN` Point.
*
* _Note: Only available with **@pixi/math-extras**._
* @param aStart - Starting point of the first segment.
* @param aEnd - Ending point of the first segment.
* @param bStart - Starting point of the second segment.
* @param bEnd - Ending point of the second segment.
* @returns {IPointData} The point where the segments intersect.
*/
export function segmentIntersection(aStart: IPointData, aEnd: IPointData, bStart: IPointData, bEnd: IPointData): Point;
/**
* Computes the point where non-coincident and non-parallel segments intersect.
* Coincident, parallel or non-intersecting segments return a `NaN` point `{x: NaN, y: NaN}`.
* The intersection point must land inside the extents of the segments or return a `NaN` Point.
*
* _Note: Only available with **@pixi/math-extras**._
* @memberof PIXI
* @param aStart - Starting point of the first segment.
* @param aEnd - Ending point of the first segment.
* @param bStart - Starting point of the second segment.
* @param bEnd - Ending point of the second segment.
* @param {IPointData} outPoint - A Point-like object in which to store the value,
* optional (otherwise will create a new Point).
* @returns {IPointData} The point where the segments intersect or a `NaN` Point.
*/
export function segmentIntersection
<T extends IPointData>(aStart: IPointData, aEnd: IPointData, bStart: IPointData, bEnd: IPointData, outPoint: T): T;
export function segmentIntersection
<T extends IPointData>(aStart: IPointData, aEnd: IPointData, bStart: IPointData, bEnd: IPointData, outPoint?: T): T
{
return genericLineIntersection(aStart, aEnd, bStart, bEnd, false, outPoint);
}