Skip to content

Instantly share code, notes, and snippets.

@natevm
Created October 11, 2017 21:37
Show Gist options
  • Save natevm/59f17050f424067a79c029578e9d1aa8 to your computer and use it in GitHub Desktop.
Save natevm/59f17050f424067a79c029578e9d1aa8 to your computer and use it in GitHub Desktop.
Pseudo-angle method
// angle relative to the x axis.
inline float pseudoangle(float2 p1, float2 p2) {
float dx, dy, t;
dx = p2.x - p1.x;
dy = p2.y - p1.y;
if ((dx == 0.0) && (dy == 0.0)) {
return -1; // indicating error
}
else {
t = dy / (fabs(dx) + fabs(dy));
/* Now correct for quadrant -- first quadrant: [0,1] */
if (dx < 0.0)
return 2.0 - t;
/* Inside second or third quadrant (1,3)*/
else if (dy < 0.0)
return 4.0 + t;
return t;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment