Intersecting_Circles/intersecting-circles-modifiers/util.wren
2021-12-14 12:52:41 +01:00

32 lines
No EOL
1 KiB
Text

import "luxe: math" for Math
class Util{
//cirle circle intersections, largely taken from http://paulbourke.net/geometry/circlesphere/
static circle_intersect_circle(o1, r1, o2, r2){
var diff_x = o2.x - o1.x
var diff_y = o2.y - o1.y
var distance = Math.length(diff_x, diff_y)
if(distance > r1 + r2) return //too far away
if(distance < (r1 - r2).abs) return //contain each other
if(distance <= Num.smallest) return //infinite solutions
var a = (r1*r1 - r2*r2 + distance*distance) / (2 * distance) //adjacient length of triangle between o1, midpoint and intersection
var h = (r1*r1 - a*a).sqrt //hypotenuse length of triangle between o1, midpoint and intersection
var mid_x = o1.x + a * (o2.x - o1.x) / distance
var mid_y = o1.y + a * (o2.y - o1.y) / distance
var inter1 = [
mid_x + h * diff_y / distance,
mid_y - h * diff_x / distance
]
var inter2 = [
mid_x - h * diff_y / distance,
mid_y + h * diff_x / distance
]
return [inter1, inter2]
}
}