Skip to content

Instantly share code, notes, and snippets.

@SocraticPhoenix
Created May 11, 2016 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SocraticPhoenix/ad3efa61107428060665790cea9a2053 to your computer and use it in GitHub Desktop.
Save SocraticPhoenix/ad3efa61107428060665790cea9a2053 to your computer and use it in GitHub Desktop.
public static PlasmaPoint2D[] possibleCenters(PlasmaPoint2D pointa, PlasmaPoint2D pointb, double radius) {
double x1 = pointa.getX();
double x2 = pointb.getX();
double y1 = pointa.getY();
double y2 = pointb.getY();
double r = radius;
double ca = (x1 * x1) + (y1 * y1) - (x2 * x2) - (y2 * y2);
double x3 = x1 - x2;
double y3 = y1 - y2;
double cy = (ca/(2 * y3)) - y1;
double a = ((x3 * x3) + (y3 * y3)) / (y3 * y3);
double b = ((2 * x1) - (2 * (cy) * x3/y3));
double c = ((x1 * x1) + (cy * cy) - (r * r));
double[] roots;
double denominator = 2 * a;
double bNumerator = -b;
double underSquare = (b * b) - (4 * a * c);
if (underSquare < 0 || denominator == 0) {
roots = new double[0];
} else {
double sqrt = Math.sqrt(underSquare);
roots = new double[]{(bNumerator + sqrt) / denominator, (bNumerator - sqrt) / denominator};
}
PlasmaPoint2D[] points = new PlasmaPoint2D[roots.length];
for (int i = 0; i < roots.length; i++) {
double x = roots[i];
double y = -(x3/y3) * x + (c/(2 * y3));
points[i] = new PlasmaPoint2D(x, y);
}
return points;
}
@aocalderon
Copy link

This approach will not work when points lie in the same y coordinate. It will lead to y3 == 0 in line 10 and 0 division errors in lines 11, 13, 14. There are alternatives implementation in https://rosettacode.org/wiki/Circles_of_given_radius_through_two_points.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment