-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathbox-circle.js
More file actions
33 lines (29 loc) · 752 Bytes
/
box-circle.js
File metadata and controls
33 lines (29 loc) · 752 Bytes
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
'use strict'
/**
* box-circle collision
* @param {number} xb top-left corner of box
* @param {number} yb top-left corner of box
* @param {number} wb width of box
* @param {number} hb height of box
* @param {number} xc center of circle
* @param {number} yc center of circle
* @param {number} rc radius of circle
*/
module.exports = function boxCircle(xb, yb, wb, hb, xc, yc, rc)
{
var hw = wb / 2
var hh = hb / 2
var distX = Math.abs(xc - (xb + wb / 2))
var distY = Math.abs(yc - (yb + hb / 2))
if (distX > hw + rc || distY > hh + rc)
{
return false
}
if (distX <= hw || distY <= hh)
{
return true
}
var x = distX - hw
var y = distY - hh
return x * x + y * y <= rc * rc
}