Skip to content

Instantly share code, notes, and snippets.

@xyfeng
Created February 23, 2017 21:40
Show Gist options
  • Save xyfeng/749d1a9583c9b7498008176e88e43f58 to your computer and use it in GitHub Desktop.
Save xyfeng/749d1a9583c9b7498008176e88e43f58 to your computer and use it in GitHub Desktop.
3 points and circle
// calcuate center point from given three points
// http://paulbourke.net/geometry/circlesphere/
PVector calculateCenter(PVector p1, PVector p2, PVector p3) {
float ma = (p2.y - p1.y) / (p2.x - p1.x);
float mb = (p3.y - p2.y) / (p3.x - p2.x);
float x = (ma * mb * (p1.y - p3.y) + mb * (p1.x + p2.x) - ma * (p2.x + p3.x)) / ( 2 * (mb - ma));
float y = (p1.x - p3.x + ma * (p1.y + p2.y) - mb * (p2.y + p3.y)) / (2 * (ma - mb));
return new PVector(x, y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment