xxxxxxxxxx
77
var p1, p2, p3;
var refresh = false;
function setup() {
createCanvas(600, 600);
p1 = createRandPt();
p2 = createRandPt();
p3 = createRandPt();
}
function draw() {
background("green");
if(refresh){
p1 = createRandPt();
p2 = createRandPt();
refresh = false;
}
p3 = new pt(mouseX, mouseY);
stroke("yellow");
strokeWeight(3);
line(p1.x, p1.y, p3.x, p3.y);
line(p2.x, p2.y, p3.x, p3.y);
fill("lightskyblue");
p1.drawPoint();
fill("pink");
p2.drawPoint();
var magV13 = sqrt(sq(p1.x-p3.x) + sq(p1.y-p3.y));
var magV23 = sqrt(sq(p3.x-p2.x) + sq(p3.y-p2.y));
var dotProd = (p1.x-p3.x)*(p2.x-p3.x) + (p1.y-p3.y)*(p2.y-p3.y);
var angle = int(180*acos(dotProd/(magV13*magV23))/PI);
textFont("cursive");
textAlign(CENTER);
textSize(30);
text("The Interior Angle is " + angle + " Degrees", width/2, 100);
}
function createRandPt() {
return new pt(random(100,500), random(200,500));
}
class pt {
constructor(x, y){
this.x = x;
this.y = y;
}
drawPoint(){
heart(this.x, this.y, 30, 30);
}
}
function mousePressed() {
refresh = true;
}
function heart(x, y, w, h){
noStroke();
for(var sign = -1; sign <= 1; sign+=2){
beginShape();
vertex(x, y-(h/3.5));
curveVertex(x, y-(h/3.5));
curveVertex(x+sign*(w/3.5), y-(h/2));
curveVertex(x+sign*(w/2), y-(h/2.5));
curveVertex(x+sign*(w/2), y-(h/8));
curveVertex(x+sign*(w/8), y+(h/4));
curveVertex(x, y+(h/2));
vertex(x, y+(h/2));
endShape();
}
}