xxxxxxxxxx
102
let t;
const GRAB_RADIUS = 5;
let grabVector;
function setup() {
createCanvas(400, 400);
noFill();
t = new Tri(50, 50, 350, 50, 200, 300);
}
function draw() {
background(220);
t.draw();
}
function mousePressed() {
grabVector = t.getGrabVector(mouseX, mouseY);
}
function mouseDragged() {
if(grabVector) {
grabVector.x = mouseX;
grabVector.y = mouseY;
}
}
function mouseReleased() {
grabVector = null;
}
class Tri {
constructor(ax, ay, bx, by, cx, cy) {
this.a = createVector(ax, ay);
this.b = createVector(bx, by);
this.c = createVector(cx, cy);
}
getGrabVector(x, y) {
let p = createVector(x, y);
if(inGrabRadius(this.a, p)) {
return this.a;
}
if(inGrabRadius(this.b, p)) {
return this.b;
}
if(inGrabRadius(this.c, p)) {
return this.c;
}
return null;
}
//x = (x1 sin 2A + x2 sin 2B + x3 sin 2C)/ (sin 2A + sin 2B + sin 2C)
//y = (y1 sin 2A + y2 sin 2B + y3 sin 2C)/ (sin 2A + sin 2B + sin 2C)
//r = abc/sqrt(a+b+c)(b+c−a)(c+a−b)(a+b−c)
circumCircle() {
let ab = p5.Vector.sub(this.a, this.b);
let ac = p5.Vector.sub(this.a, this.c);
let ba = p5.Vector.sub(this.b, this.a);
let bc = p5.Vector.sub(this.b, this.c);
let ca = p5.Vector.sub(this.c, this.a);
let cb = p5.Vector.sub(this.c, this.b);
let A = ab.angleBetween(ac);
let B = bc.angleBetween(ba);
let C = ca.angleBetween(cb);
let div = sin(2 * A) + sin(2 * B) + sin(2 * C);
let x = (this.a.x * sin(2*A) + this.b.x * sin(2*B) + this.c.x * sin(2*C))/div;
let y = (this.a.y * sin(2*A) + this.b.y * sin(2*B) + this.c.y * sin(2*C))/div;
let la = ab.mag();
let lb = bc.mag();
let lc = ca.mag();
let r = (la*lb*lc)/sqrt((la+lb+lc)*(lb+lc-la)*(lc+la-lb)*(la+lb-lc));
return createVector(x, y, r);
}
draw() {
triangle(this.a.x, this.a.y, this.b.x, this.b.y, this.c.x, this.c.y);
circle(this.a.x, this.a.y, GRAB_RADIUS * 2);
circle(this.b.x, this.b.y, GRAB_RADIUS * 2);
circle(this.c.x, this.c.y, GRAB_RADIUS * 2);
let circ = this.circumCircle();
circle(circ.x, circ.y, circ.z * 2);
circle(circ.x, circ.y, GRAB_RADIUS * 2);
}
}
function inGrabRadius(v1, v2) {
return v1.dist(v2) <= GRAB_RADIUS;
}