How do I write a trig equation in processing?
in
Programming Questions
•
2 years ago
Okay I'm editing my post to make more sense...
basically, I am trying to get the blue line in the sketch match the angle of the black line that completes the right angle triangle between two randomly generated points.
Heres my code:
basically, I am trying to get the blue line in the sketch match the angle of the black line that completes the right angle triangle between two randomly generated points.
Heres my code:
- float randx1;
float randy1;
float randx2;
float randy2;
float plus=0;
void setup() {
size(500, 500);
// randx1 = random(0, width/2);
randx2= random(width/2, width);
// randy1 = random(height);
randy2= random(height);
}
void draw() {
background(255);
float mouseMove= constrain(mouseX, 0, width/2);
randx1= mouseMove;
randy1= mouseY;
//split tank line
line(width/2, 0, width/2, height);
// red= 1
noStroke();
fill(255, 0, 0);
ellipse(randx1, randy1, 10, 10);
// green 2
noStroke();
fill(0, 255, 0);
ellipse(randx2, randy2, 10, 10);
stroke(255, 0, 0);
//imaginary triangle
//line(randx1, randy1, randx1, randx1++);
strokeWeight(3);
//---------------------if RED is 'higher up' than green-----------------------------------
// if red ellipse
if (randy1 < randy2) {
stroke(255, 0, 0);
//horizontal for red (1)
line(randx1, randy1, randx2, randy1);
//vertical for green (2)
line(randx2, randy2, randx2, randy1);
///------------ROTATED LINE... im trying to get this blue line to 'follow' the black one
stroke(0, 0, 255);
strokeWeight(3);
//--------calculations
// hypotenuse
float h= dist(randx1, randy1, randx2, randy2);
println("h is: " + h);
//opposite (rise)
float o= dist(randx2, randy2, randx2, randy1);
println("o i: " + o);
float thing= sin(o/h);
float p = degrees(thing);
println("thing is: " + thing);
//------------- blue line
pushMatrix();
translate(randx1, randy1);
rotate(thing);
line(0, 0, 50, 0);
popMatrix();
}
//---------------------if GREEN is 'higher up' than red---------------------------------------
// green
if (randy2 < randy1) {
stroke(0, 255, 0);
//horizontal for green (2)
line(randx2, randy2, randx1, randy2);
stroke(255, 0, 0);
//vertical for red (1)
line(randx1, randy1, randx1, randy2);
///------------ROTATED LINE... im trying to get this blue line to 'follow' the black one
stroke(0, 0, 255);
strokeWeight(3);
//--------calculations
// hypotenuse
float h= dist(randx1, randy1, randx2, randy2);
println("h is: " + h);
//opposite (rise)
float o2= dist(randx2, randy2, randx2, randy1);
o2=o2*(-1);
println("o2 i: " + o2);
float thing= sin(o2/h);
float p = degrees(thing);
println("thing is: " + thing);
//------------- blue line
pushMatrix();
translate(randx1, randy1);
rotate(thing);
line(0, 0, 50, 0);
popMatrix();
}
//------------diagonal
stroke(0);
strokeWeight(1);
line(randx1, randy1, randx2, randy2);
}
1