Java examples for java.lang:Math Geometry
Create the geometry of an arrow.
public class Main{ /**/*w w w. j a v a 2 s . c o m*/ * Create the geometry of an arrow. The arrow is positioned at the end (last * point) of the specified polyline, as follows: * * 0,4--, \ --, \ --, \ --, \ --, -------------------------3-----------1 / * --' / --' / --' / --' 2--' * * @param x X coordinates of polyline of where arrow is positioned in the * end. Must contain at least two points. * @param y Y coordinates of polyline of where arrow is positioned in the * end. * @param length Length along the main axis from point 1 to the projection * of point 0. * @param angle Angle between the main axis and the line 1,0 (and 1,2) in * radians. * @param inset Specification of point 3 [0.0-1.0], 1.0 will put point 3 at * distance length from 1, 0.0 will put it at point 1. * @return Array of the five coordinates [x,y,...]. */ public static int[] createArrow(int[] x, int[] y, double length, double angle, double inset) { int[] arrow = new int[10]; int x0 = x[x.length - 1]; int y0 = y[y.length - 1]; arrow[2] = x0; arrow[3] = y0; // Find position of interior of the arrow along the polyline int[] pos1 = new int[2]; GeometryUtils.findPolygonPosition(x, y, length, pos1); // Angles double dx = x0 - pos1[0]; double dy = y0 - pos1[1]; // Polyline angle double v = dx == 0.0 ? Math.PI / 2.0 : Math.atan(Math.abs(dy / dx)); v = dx > 0.0 && dy <= 0.0 ? Math.PI + v : dx > 0.0 && dy >= 0.0 ? Math.PI - v : dx <= 0.0 && dy < 0.0 ? -v : dx <= 0.0 && dy > 0.0 ? +v : 0.0; double v0 = v + angle; double v1 = v - angle; double edgeLength = length / Math.cos(angle); arrow[0] = x0 + (int) Math.round(edgeLength * Math.cos(v0)); arrow[1] = y0 - (int) Math.round(edgeLength * Math.sin(v0)); arrow[4] = x0 + (int) Math.round(edgeLength * Math.cos(v1)); arrow[5] = y0 - (int) Math.round(edgeLength * Math.sin(v1)); double c1 = inset * length; arrow[6] = x0 + (int) Math.round(c1 * Math.cos(v)); arrow[7] = y0 - (int) Math.round(c1 * Math.sin(v)); // Close polygon arrow[8] = arrow[0]; arrow[9] = arrow[1]; return arrow; } /** * Create geometry for an arrow along the specified line and with tip at * x1,y1. See general method above. * * @param x0 X first end point of line. * @param y0 Y first end point of line. * @param x1 X second end point of line. * @param y1 Y second end point of line. * @param length Length along the main axis from point 1 to the projection * of point 0. * @param angle Angle between the main axis and the line 1,0 (and 1.2) * @param inset Specification of point 3 [0.0-1.0], 1.0 will put point 3 at * distance length from 1, 0.0 will put it at point 1. * @return Array of the four coordinates [x,y,...]. */ public static int[] createArrow(int x0, int y0, int x1, int y1, double length, double angle, double inset) { int[] x = { x0, x1 }; int[] y = { y0, y1 }; return createArrow(x, y, length, angle, inset); } /** * Return the x,y position at distance "length" into the given polyline. * * @param x X coordinates of polyline * @param y Y coordinates of polyline * @param length Requested position * @param position Preallocated to int[2] * @return True if point is within polyline, false otherwise */ public static boolean findPolygonPosition(int[] x, int[] y, double length, int[] position) { if (length < 0) { return false; } double accumulatedLength = 0.0; for (int i = 1; i < x.length; i++) { double legLength = GeometryUtils.length(x[i - 1], y[i - 1], x[i], y[i]); if (legLength + accumulatedLength >= length) { double part = length - accumulatedLength; double fraction = part / legLength; position[0] = (int) Math.round(x[i - 1] + fraction * (x[i] - x[i - 1])); position[1] = (int) Math.round(y[i - 1] + fraction * (y[i] - y[i - 1])); return true; } accumulatedLength += legLength; } // Length is longer than polyline return false; } /** * Return the length of a vector. * * @param v Vector to compute length of [x,y,z]. * @return Length of vector. */ public static double length(double[] v) { return Math.sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]); } /** * Compute distance between two points. * * @param p0, p1 Points to compute distance between [x,y,z]. * @return Distance between points. */ public static double length(double[] p0, double[] p1) { double[] v = GeometryUtils.createVector(p0, p1); return length(v); } /** * Compute the length of the line from (x0,y0) to (x1,y1) * * @param x0, y0 First line end point. * @param x1, y1 Second line end point. * @return Length of line from (x0,y0) to (x1,y1). */ public static double length(int x0, int y0, int x1, int y1) { return GeometryUtils.length((double) x0, (double) y0, (double) x1, (double) y1); } /** * Compute the length of the line from (x0,y0) to (x1,y1) * * @param x0, y0 First line end point. * @param x1, y1 Second line end point. * @return Length of line from (x0,y0) to (x1,y1). */ public static double length(double x0, double y0, double x1, double y1) { double dx = x1 - x0; double dy = y1 - y0; return Math.sqrt(dx * dx + dy * dy); } /** * Compute the length of a polyline. * * @param x, y Arrays of x,y coordinates * @param nPoints Number of elements in the above. * @param isClosed True if this is a closed polygon, false otherwise * @return Length of polyline defined by x, y and nPoints. */ public static double length(int[] x, int[] y, boolean isClosed) { double length = 0.0; int nPoints = x.length; for (int i = 0; i < nPoints - 1; i++) { length += GeometryUtils.length(x[i], y[i], x[i + 1], y[i + 1]); } // Add last leg if this is a polygon if (isClosed && nPoints > 1) { length += GeometryUtils.length(x[nPoints - 1], y[nPoints - 1], x[0], y[0]); } return length; } /** * Construct the vector specified by two points. * * @param p0, p1 Points the construct vector between [x,y,z]. * @return v Vector from p0 to p1 [x,y,z]. */ public static double[] createVector(double[] p0, double[] p1) { double v[] = { p1[0] - p0[0], p1[1] - p0[1], p1[2] - p0[2] }; return v; } }