Android examples for java.lang:Math
segments Intersection
import java.util.Vector; import android.graphics.PointF; public class Main{ /**//from www.ja v a 2 s. c o m * * http://alienryderflex.com/intersect/ * * @param a0x * @param a0y * @param a1x * @param a1y * @param b0x * @param b0y * @param b1x * @param b1y * @return Intersecci?n entre dos segmentos */ public static PointF segmentsIntersection(float a0x, float a0y, float a1x, float a1y, float b0x, float b0y, float b1x, float b1y) { float distA, theCos, theSin, newX, aPos; // Fail if either line segment is zero-length. if (a0x == a1x && a0y == a1y || b0x == b1x && b0y == b1y) { return null; } // Fail if the segments share an end-point. if (a0x == b0x && a0y == b0y || a1x == b0x && a1y == b0y || a0x == b1x && a0y == b1y || a1x == b1x && a1y == b1y) { return null; } // (1) Translate the system so that point a0 is on the origin. a1x -= a0x; a1y -= a0y; b0x -= a0x; b0y -= a0y; b1x -= a0x; b1y -= a0y; // Discover the length of segment a0 - a1 distA = MathUtils.getDistance(a1x, a1y); // (2) Rotate the system so that point a1 is on the positive X axis. theCos = a1x / distA; theSin = a1y / distA; newX = b0x * theCos + b0y * theSin; b0y = b0y * theCos - b0x * theSin; b0x = newX; newX = b1x * theCos + b1y * theSin; b1y = b1y * theCos - b1x * theSin; b1x = newX; // Fail if segment b0-b1 doesn't cross line a0-a1 if (b0y < 0 && b1y < 0 || b0y >= 0 && b1y >= 0) { return null; } // (3) Discover the position of the intersection point along line a0-a1 aPos = b1x + (b0x - b1x) * b1y / (b1y - b0y); // Fail if segment b0-b1 crosses line a0-a1 outside of segment a0-a1 if (aPos < 0 || aPos > distA) { return null; } // (4) Apply the discovered position to line a0-a1 in the original // coordinate system. // Success. return new PointF(a0x + aPos * theCos, a0y + aPos * theSin); } /** * @param x0 * @param y0 * @param x1 * @param y1 * @return distancia entre dos puntos */ public static float getDistance(float x0, float y0, float x1, float y1) { return getDistance(x1 - x0, y1 - y0); } /** * @param side0 * @param side1 * @return distancia entre dos puntos en la misma recta */ public static float getDistance(float side0, float side1) { return (float) Math.sqrt(side0 * side0 + side1 * side1); } }