function
setup() {
createCanvas(640, 480);
}
function
draw() {
background(255);
noFill()
stroke(0)
strokeWeight(5)
triangle(320, 140, 220, 340, 420, 340)
drawInCircle(320, 140, 220, 340, 420, 340)
noLoop()
}
function
drawInCircle(x1, y1, x2, y2, x3, y3){
let side=getSides(x1, y1, x2, y2, x3, y3)
let a=side.a, b=side.b, c=side.c;
let inCenter=getIncenter(a, b, c, x1, y1, x2, y2, x3, y3);
let inRadius=getInradius(a, b, c);
circle(inCenter.x, inCenter.y, 2*inRadius)
}
function
getSides(Ax, Ay, Bx, By, Cx, Cy){
return
{
a: dist(Bx, By, Cx, Cy),
b: dist(Cx, Cy, Ax, Ay),
c: dist(Ax, Ay, Bx, By),
}
}
function
getIncenter(a, b, c, x1, y1, x2, y2, x3, y3){
return
{
x: (a*x1 + b*x2 + c*x3)/(a + b + c),
y: (a*y1 + b*y2 + c*y3)/(a + b + c)
}
}
function
getInradius(a, b, c){
let s=(a+b+c)/2
let area=sqrt(s*(s-a)*(s-b)*(s-c))
return
area/s
}