-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathcircleOutline-box.js
44 lines (40 loc) · 1.55 KB
/
circleOutline-box.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
34
35
36
37
38
39
40
41
42
43
44
var circlePoint = require('./circle-point')
var boxCircle = require('./box-circle')
/**
* circleOutline-box (axis-aligned) collision
* @param {number} xc center of circle
* @param {number} yc center of circle
* @param {radius} rc radius of circle
* @param {number} x top-left corner of box
* @param {number} y top-left corner of box
* @param {number} width of box
* @param {number} height of box
* @param {number} thickness of circle outline
*/
module.exports = function circleOutlineBox(xc, yc, rc, x, y, width, height, thickness)
{
thickness = thickness || 1
var count = 0
count += circlePoint(xc, yc, rc, x, y) ? 1 : 0
count += circlePoint(xc, yc, rc, x + width, y) ? 1 : 0
count += circlePoint(xc, yc, rc, x, y + height) ? 1 : 0
count += circlePoint(xc, yc, rc, x + width, y + height) ? 1 : 0
// if no corners are inside the circle, then intersects only if box encloses circle-outline
if (count === 0)
{
return boxCircle(x, y, width, height, xc, yc, rc)
}
// if one corner is inside and one corner is outside then box intersects circle-outline
if (count >= 1 && count <= 3)
{
return true
}
// last check is if box is inside circle, need to check that a corner is not inside the inner circle
if (count === 4)
{
return !circlePoint(xc, yc, rc - thickness, x, y) ||
!circlePoint(xc, yc, rc - thickness, x + width, y) ||
!circlePoint(xc, yc, rc - thickness, x, y + height) ||
!circlePoint(xc, yc, rc - thickness, x + width, y + height)
}
}