Java examples for java.lang:Math Geometry Line
Calculates the intersection location of the line formed by (x1, y1), (x2, y2) and the line segment formed by (x3, y3), (x4, y4).
/*/* w ww . java 2 s . 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 formed by (x1, y1), (x2, * y2) and the line segment formed by (x3, y3), (x4, y4). If the line and * line segment are determined to be parallel, then Geom.PARALLEL is * returned and no further computations are done. If the line segment does * not cross the line, 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 * line segment is stored in index location 2. If there in an intersection * then the returned value is Geom.INTERSECT. */ public static Object getLineSegIntersection(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 u = (cx * by - cy * bx) / b_dot_d_perp; if (u < 0 || u > 1) { return null; } if (result != null) { result[0] = x3 + u * dx; result[1] = y3 + u * dy; result[2] = u; } return INTERSECT; } }