Java examples for java.lang:Math Geometry Line
Calculates the intersection location of the line segments formed by (x1, y1), (x2, y2) and (x3, y3), (x4, y4).
/*//from w w w.ja v a2s . c om * Copyright (c) JenSoft API * This source file is part of JenSoft API, All rights reserved. * JENSOFT PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ //package com.java2s; public class Main { public static final Object PARALLEL = new Object(); public static final Object INTERSECT = new Object(); /** * Calculates the intersection location of the line segments formed by (x1, * y1), (x2, y2) and (x3, y3), (x4, y4). If the line segments are determined * to be parallel, then Geom.PARALLEL is returned and no further * computations are done. If the segments do not cross each other then null * is returned and no further computations are done. Otherwise the * intersection location is stored in index locations 0 and 1 of the * specified array. The parametric value with respect to the first line * segment is stored in index location 2. If there is an intersection, then * the returned value is Geom.INTERSECT. */ public static Object getSegSegIntersection(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4, double[] result) { double bx = x2 - x1; double by = y2 - y1; double dx = x4 - x3; double dy = y4 - y3; double b_dot_d_perp = bx * dy - by * dx; if (b_dot_d_perp == 0) { return PARALLEL; } double cx = x3 - x1; double cy = y3 - y1; double t = (cx * dy - cy * dx) / b_dot_d_perp; if (t < 0 || t > 1) { return null; } double u = (cx * by - cy * bx) / b_dot_d_perp; if (u < 0 || u > 1) { return null; } if (result != null) { result[0] = x1 + t * bx; result[1] = y1 + t * by; result[2] = t; } return INTERSECT; } }