Home > Back-end >  Best way to find the angle between two nodes in an lattice graph?
Best way to find the angle between two nodes in an lattice graph?

Time:03-15

If I have a lattice graph which looks like this:

0:0 0:1 0:2 0:3
1:0 1:1 1:2 1:3
2:0 2:1 2:2 2:3
3:0 3:1 3:2 3:3

, what is the best way to find an angle between two nodes?
Example:

angle(0:0, 0:1) = 0;
angle(0:0, 1:1) = 45;

This will be used during rendering to visualize a path in this graph with lines, a line will be placed at the center of a node and then rotated to end in the next node on the path. Currently I hardcoded every coordinateS difference and match it to the corresponding angle with a switch statement, are there better ways?

CodePudding user response:

Assuming x:y format, and an angle measured clockwise from the horizontal:

angle(x1:y1, x2:y2) = arctan((x2-x1)/(y2-y1))
  • Related