# Geometry Utilities : Geometry « 2D Graphics GUI « Java

Geometry Utilities

```

/**
* GeometryUtilities.java
*/

import static java.lang.Math.PI;
import static java.lang.Math.abs;
import static java.lang.Math.atan2;
import static java.lang.Math.sqrt;
import static java.lang.Math.toDegrees;

import java.awt.geom.Arc2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.util.Vector;

/**
* @author Cameron Behar
*/
public class GeometryUtilities {
public static double angle(double x, double y) {
return -(atan2(y, x) - PI / 2.0);
}

public static double angleBetween(double angle0, double angle1) {
// Going counter clock-wise
return angle1 - (angle0 + ((angle0 < angle1) ? 360.0 : 0.0));
}

public static double determinant(double[][] A) {
if (A.length == 1)
return A[0][0];

if (A.length == 2)
return A[0][0] * A[1][1] - A[0][1] * A[1][0];

double result = 0;
for (int i = 0; i < A[0].length; i++) {
double temp[][] = new double[A.length - 1][A[0].length - 1];

for (int j = 1; j < A.length; j++)
for (int k = 0; k < A[0].length; k++)
if (k < i)
temp[j - 1][k] = A[j][k];
else if (k > i)
temp[j - 1][k - 1] = A[j][k];

result += A[0][i] * ((i & 1) == 0 ? 1 : -1) * determinant(temp);
}

return result;
}

public static Vector<Point2D> getCrossings(Arc2D arc0, Point2D arc0Center,
Arc2D arc1, Point2D arc1Center) {
Vector<Point2D> ret = new Vector<Point2D>();

double distance = arc0Center.distance(arc1Center);

// There are no solutions because the circles are separate.
// There are no solutions because one circle is contained within the
// other.
// There are an infinite number of solutions because the circles are
// coincident.
} else {
// Calculate the first intersection
double x0 = arc0Center.getX(), y0 = arc0Center.getY();
double x1 = arc1Center.getX(), y1 = arc1Center.getY();

/ (2 * distance);
double h = sqrt(radius0Squared - a * a);

double x2 = x0 + a * (x1 - x0) / distance;
double y2 = y0 + a * (y1 - y0) / distance;

Point2D.Double intersection = new Point2D.Double(x2 + h * (y1 - y0)
/ distance, y2 - h * (x1 - x0) / distance);
double angle0ToIntersection = toDegrees(atan2(
-(intersection.y - y0), intersection.x - x0));
double angle1ToIntersection = toDegrees(atan2(
-(intersection.y - y1), intersection.x - x1));
if (arc0.containsAngle(angle0ToIntersection)
&& arc1.containsAngle(angle1ToIntersection))

// If the circles aren't tangential, calculate the second
// intersection
intersection = new Point2D.Double(
x2 - h * (y1 - y0) / distance, y2 + h * (x1 - x0)
/ distance);
angle0ToIntersection = toDegrees(atan2(-(intersection.y - y0),
intersection.x - x0));
angle1ToIntersection = toDegrees(atan2(-(intersection.y - y1),
intersection.x - x1));
if (arc0.containsAngle(angle0ToIntersection)
&& arc1.containsAngle(angle1ToIntersection))
}
}

return ret;
}

public static Vector<Point2D> getCrossings(Line2D line0, Line2D line1) {
Vector<Point2D> ret = new Vector<Point2D>();

if (line0.intersectsLine(line1)) {
Point2D.Double intersection = new Point2D.Double(0.0, 0.0);

double xDiff0 = line0.getX2() - line0.getX1();
double xDiff1 = line1.getX2() - line1.getX1();
double yDiff0 = line0.getY2() - line0.getY1();
double yDiff1 = line1.getY2() - line1.getY1();
double xDiff2 = line0.getX1() - line1.getX1();
double yDiff2 = line0.getY1() - line1.getY1();

double div = yDiff1 * xDiff0 - xDiff1 * yDiff0;
double u = (xDiff1 * yDiff2 - yDiff1 * xDiff2) / div;
intersection.x = line0.getX1() + u * xDiff0;
intersection.y = line0.getY1() + u * yDiff0;

}

return ret;
}

public static boolean isBetween(Point2D a, Point2D b, Point2D c) {
double u = c.getX() - a.getX();
double v = c.getY() - a.getY();
double tx = (b.getX() - a.getX()) / u;
double ty = (b.getY() - a.getY()) / v;

return (tx > 0 && tx < 1) || (ty > 0 && ty < 1);
}

public static Point2D midpoint(double x0, double y0, double x1, double y1) {
return new Point2D.Double((x1 + x0) / 2.0, (y1 + y0) / 2.0);
}

public static double slope(double x0, double y0, double x1, double y1) {
return (y1 - y0) / (x1 - x0);
}

public static double slope(Line2D line) {
return slope(line.getX1(), line.getY1(), line.getX2(), line.getY2());
}
}

```

### Related examples in the same category

 1 Collection of geometry utility methods 2 Unions Rectangle2D 3 Interpolates points given in the 2D plane 4 Returns distance between two sets of coords 5 Returns distance between 3D set of coords 6 Returns closest point on segment to point 7 Calculate Angle From 8 Returns distance to segment 9 Hexagon demo 10 Implements an Vector in 3D space. 11 Implementation of the 4 dimensional vector. 12 Quaternion 13 Circle shape 14 This is a Polygon that allows the user to flip and swap the points along it's axis. 15 Fast trigonometric operations 16 A class to represent a latitude and longitude 17 An undirected graph that keeps track of connected components (groups). 18 Generates n logarithmically-spaced points between d1 and d2 using the provided base. 19 Returns a dimension where width and height are inside the bounds of the maxWidth and maxHeight parameters