We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Okay, so i am trying to get the angle of a line, drawen between two points.
Here are the code:
float x1 = 10;
float y1 = 10;
float x2 = 90;
float y2 = 90;
PVector v1 = new PVector(x1, y1);
PVector v2 = new PVector(x2, y2);
void setup() {
background(200, 100, 12);
size(600, 600);
stroke(45);
translate(300, 300);
line(0, -20, 0, 20); //Set a cross for visual reference
line(-20, 0, 20, 0); //Set a cross for visual reference
line(x1, y1, x2, y2); //Draw a line between the two points
float a = PVector.angleBetween(v1, v2);
println(degrees(a)); // Prints "8.537736E-7"
}
The result are: 8.537736E-7, were i would have hoped it was -/+45 degrees ( -/+135 depending on rotation). I have tried with other number as variables, but it does not yield a meaningful results. Any pointers of what i am doing wrong or other ways to get the angle?
Answers
The angle is measured from 0,0. A point at 10,10 and another at 90,90 construct a straight line from 0,0.
Hey Bird thank you for your answer, but that does not make any sense to me. Even if the line runs through 0,0, it can (and in this case should) still have an angle. Unless zero is rotated with -1/4*Pi from the x-axis. But i tried with other number and non of them returns anything i can make any sense of.
Secondly i thught it was atan2 were it was starting at origo (0,0). What are the purpose of feeding angleBetween() with two points, if it uses origo? Then you would have 3 points, which does not make any sense, at least not to me. But i would love an explanation :)
It is atan2
float a = atan2 (v2. y - v1. y, v2. x - v1. x) ;
The
angleBetween
returns the angle between 2 lines so requires one point to represent the line intersection point plus 2 for the line ends,hence 3 points. TheangleBetween
assumes that the intersection is at the origin.Thank you guys. I don't know how i got that idea stuck in my head, that the two point was a line and that the angle was always measured from the positive x-axis as rotational zero. But i see know how it works. Thanks again :)