How do I measure the angle of a vertex?

Let me have three points, and the two lines from one of them, the vertex point, to the others two.
How do I calculate the angle between the two lines?
I am trying not to use PVectors.

use atan2 (remark below)


//https://discourse.processing.org/t/how-do-i-measure-the-angle-of-a-vertex/34511
//https://processing.org/reference/atan2_.html

void setup() {
  size(999, 999);
}

void draw() {
  background(204);

  checkPoint(100, 100);
  checkPoint(555, 120);

  noLoop();
}

//------------------------------------------------------------------------

void checkPoint(float x1, float y1) {
  // 450,450 is the same for all angles. 
  // x1,y1 is the point we want get the angle from

  ellipse(450, 450, 19, 19);
  ellipse(x1, y1, 19, 19);
  line(x1, y1, 
    450, 450); 

  pushMatrix(); 
  translate(450, 450);

  float angle = atan2(y1-450, x1-450); // use y,x here, see https://processing.org/reference/atan2_.html
  println(degrees(angle));
  rotate(angle);

  rect(-30, -5, 60, 10);
  popMatrix(); 
  //
}


Remark

Ah, I just realized, atan2() sucks. And maths too.

  • atan2 gives you often negative values imho
  • processing has it’s 0° degrees to the east (and NOT north)

here is a quick work-around:

//https://discourse.processing.org/t/how-do-i-measure-the-angle-of-a-vertex/34511
//https://processing.org/reference/atan2_.html

void setup() {
  size(999, 999);
}

void draw() {
  background(204);

  pushMatrix(); 

  translate(width/2, height/2);
  float a = atan2(mouseY-height/2, mouseX-width/2);
  rotate(a);
  rect(-30, -5, 60, 10);

  if (a<0) {
    //a=a-PI;
    a=map(a, -PI, 0, PI, TWO_PI);
  }
  a+=HALF_PI;
  if (a>TWO_PI) {
    a-=TWO_PI;
  } 
  popMatrix();

  text(degrees(a), mouseX+13, mouseY);
}

Hello @mario60,

Try to implement this:
https://www.cuemath.com/geometry/angle-between-two-lines/

:)

1 Like

Yes, nice advise. I’ll let you know. Thanks. Cheers.

2 Likes

Dor Product of normed vectors is cos of angle.

1 Like