MCPcopy
hub / github.com/KilledByAPixel/LittleJS / isIntersecting

Function isIntersecting

src/engineMath.js:230–262  ·  view source on GitHub ↗

Returns true if a line segment is intersecting an axis aligned box * @param {Vector2} start - Start of raycast * @param {Vector2} end - End of raycast * @param {Vector2} pos - Center of box * @param {Vector2} size - Size of box * @return {boolean} - True if intersecting * @memb

(start, end, pos, size)

Source from the content-addressed store, hash-verified

228 * @return {boolean} - True if intersecting
229 * @memberof Math */
230function isIntersecting(start, end, pos, size)
231{
232 // Liang-Barsky algorithm
233 const boxMin = pos.subtract(size.scale(.5));
234 const boxMax = boxMin.add(size);
235 const delta = end.subtract(start);
236 const a = start.subtract(boxMin);
237 const b = start.subtract(boxMax);
238 const p = [-delta.x, delta.x, -delta.y, delta.y];
239 const q = [a.x, -b.x, a.y, -b.y];
240 let tMin = 0, tMax = 1;
241 for (let i = 4; i--;)
242 {
243 if (p[i])
244 {
245 const t = q[i] / p[i];
246 if (p[i] < 0)
247 {
248 if (t > tMax) return false;
249 tMin = max(t, tMin);
250 }
251 else
252 {
253 if (t < tMin) return false;
254 tMax = min(t, tMax);
255 }
256 }
257 else if (q[i] < 0)
258 return false;
259 }
260
261 return true;
262}
263
264/** Returns an oscillating wave between 0 and amplitude with frequency of 1 Hz by default
265 * @param {number} [frequency] - Frequency of the wave in Hz

Callers 2

math.test.mjsFile · 0.90
engineObjectsRaycastFunction · 0.85

Calls 3

subtractMethod · 0.45
scaleMethod · 0.45
addMethod · 0.45

Tested by

no test coverage detected