/* built with Studio Sketchpad:
* https://sketchpad.cc
*
* observe the evolution of this sketch:
* https://studio.sketchpad.cc/sp/pad/view/ro.MWiBupPH-US/rev.1594
*
* authors:
* Pavel Kostenko (Chrome)
* Pavel Kostenko
* Pavel Kostenko (Opera)
* license (unless otherwise specified):
* creative commons attribution-share alike 3.0 license.
* https://creativecommons.org/licenses/by-sa/3.0/
*/
// Pressing Control-R will render this sketch.
// You can use leftMouseButton and RightMouseButton to change vectors
v1 = new PVector(50, 100); // red
v2 = new PVector(100, 50); // blue
vSum = PVector.add(v1, v2);
vSub = PVector.sub(v1, v2);
float dx = dy = 150;
float v1mag = v1.mag();
float v2mag = v2.mag();
float vDist = vSub.mag();
void setup() { // this is run once.
// set the background color
background(255);
// specify canvas size
size(300, 300);
// smooth edges
smooth();
// limit the number of frames per second
frameRate(30);
// set the width of the line.
strokeWeight(2);
colorMode(HSB, 255);
}
void draw() { // this is run repeatedly.
background(255);
fill(1, 0, 0);
// Vector#1
stroke(10, 250, 250);
line(0 + dx, 0 + dy, v1.x + dx, v1.y + dy);
circle(v1.x + dx, v1.y + dy, 2);
// Vector#2
stroke(180, 250, 250);
line(0 + dx, 0 + dy, v2.x + dx, v2.y + dy);
circle(v2.x + dx, v2.y + dy, 2);
// draw vector addition
stroke(110, 250, 250);
line(0 + dx, 0 + dy, vSum.x + dx, vSum.y + dy);
circle(vSum.x + dx, vSum.y + dy, 3);
//
text('v1.mag = ' + v1mag, 0, 10);
text('v2.mag = ' + v2mag, 0, 20);
text('vDist = ' + vDist, 0, 30);
}
void mousePressed() {
//x1 = mouseX;
// y1 = mouseY;
//stroke(random(220, 250), 250, 250);
}
void mouseDragged() {
if (mouseButton == LEFT) {
v1.x = mouseX - dx;
v1.y = mouseY - dy;
v1mag = v1.mag();
} else {
v2.x = mouseX - dx;
v2.y = mouseY - dy;
v2mag = v2.mag();
}
vSum = PVector.add(v1, v2);
vSub = PVector.sub(v1, v2);
vDist = vSub.mag();
fill(1, 0, 0);
}
void mouseReleased() {
if (mouseButton == LEFT) {
v1.x = mouseX - dx;
v1.y = mouseY - dy;
} else if (mouseButton == RIGHT) {
v2.x = mouseX - dx;
v2.y = mouseY - dy;
}
vSum = PVector.add(v1, v2);
vSub = PVector.sub(v1, v2);
//println('angle between vectors is ' + round(getAngleBetweenVectors(v1, v2)));
//println('distance between vector heads is ' + round(vSub.mag()));
}
void circle(x, y, r) {
ellipse(x, y, r * 2, r * 2);
}
float getAngleBetweenVectors(v1, v2) {
float theta = acos(v1.dot(v2) / (v1.mag() * v2.mag()));
theta = (theta * 180) / PI; // convert radians to degrees
return theta;
}