/
hasPoint.js
33 lines (28 loc) · 1001 Bytes
/
hasPoint.js
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
const getBasisInverse = require('./getBasisInverse')
const pointTransitFrom = require('../point2/transitFrom')
module.exports = function (box, point) {
// @affineplane.box2.hasPoint(box, point)
//
// Test if a point is inside the box. If the point is at the box edge,
// it is counted as being inside.
//
// Parameters:
// box
// a box2, in the reference basis.
// point
// a point2, in the reference basis.
//
// Return:
// a boolean
//
// Note: we thought it would be practical to count inner box (0,0) inside
// and inner box (w,h) outside. However, due to rotation that makes things
// confusing. Therefore all edge points are counted as inside.
// Transit the point to the inner box basis for easier checking.
const innerBasis = getBasisInverse(box)
const innerPoint = pointTransitFrom(point, innerBasis)
// Test the point
const px = innerPoint.x
const py = innerPoint.y
return px >= 0 && px <= box.w && py >= 0 && py <= box.h
}