Java examples for java.lang:Math Geometry Line
This will check if two lines collide.
//package com.java2s; import java.awt.geom.Point2D.Double; public class Main { /**/* ww w . ja v a2 s .co m*/ * This will check if two lines collide. It will return the collision point * which is null if their is no collision. * * @param x1 * the x position of the first line firsts point. * @param y1 * the y position of the first line firsts point. * @param x2 * the x position of the first line second point. * @param y2 * the y position of the first line second point. * @param x3 * the x position of the second line firsts point. * @param y3 * the y position of the second line firsts point. * @param x4 * the x position of the second line second point. * @param y4 * the y position of the second line second point. * @return the collision point which can be null. */ public static Double lineIntersect(double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) { double denom = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); if (denom == 0.0) return null; double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / denom; double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / denom; if (ua >= 0.0f && ua <= 1.0f && ub >= 0.0f && ub <= 1.0f) return new Double((x1 + ua * (x2 - x1)), (y1 + ua * (y2 - y1))); return null; } }