91
/** Returns whether the point is within the triangle formed by the following 6 x/y point pairs */
92
export function isPointWithinTriangle(
94
>
ax: number, ay: number,
95
>
bx: number, by: number,
96
>
cx: number, cy: number
97
>
) {
98
>
const v0x = cx - ax;
99
>
const v0y = cy - ay;
100
>
const v1x = bx - ax;
101
>
const v1y = by - ay;
102
>
const v2x = x - ax;
103
>
const v2y = y - ay;
104
>
105
>
const dot00 = v0x * v0x + v0y * v0y;
106
>
const dot01 = v0x * v1x + v0y * v1y;
107
>
const dot02 = v0x * v2x + v0y * v2y;
108
>
const dot11 = v1x * v1x + v1y * v1y;
109
>
const dot12 = v1x * v2x + v1y * v2y;
110
>
111
>
const invDenom = 1 / (dot00 * dot11 - dot01 * dot01);
112
>
const u = (dot11 * dot02 - dot01 * dot12) * invDenom;
113
>
const v = (dot00 * dot12 - dot01 * dot02) * invDenom;
114
>
115
>
return u >= 0 && v >= 0 && u + v < 1;
116
>
}
117
118
export function randomChance(p: number): boolean {