/* * Moonlight|3D Copyright (C) 2006 The Moonlight|3D team * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. * * You should have received a copy of the GNU Lesser General Public License * along with this library; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * Created on Oct 31, 2006 */ package ml.math; public class Line2D { public Vector2D end1, end2; public Line2D() { end1=new Vector2D(); end2=new Vector2D(); } public Line2D(Vector2D end1, Vector2D end2) { this.end1=end1.clone(); this.end2=end2.clone(); } public Vector2D getIntersectionPoint(Line2D line) { Vector2D direction, otherDirection; double lambda, tau; direction=end2.sub(end1); otherDirection=line.end2.sub(line.end1); double denominator=(-direction.X1*otherDirection.X2+direction.X2*otherDirection.X1); if(denominator==0) { return null; // lines are parallel - no intersection possible } tau = (-direction.X1*end1.X2+direction.X1*line.end1.X2+direction.X2*end1.X1-direction.X2*line.end1.X1)/denominator; lambda = -(-end1.X1*otherDirection.X2+line.end1.X1*otherDirection.X2+otherDirection.X1*end1.X2-otherDirection.X1*line.end1.X2)/denominator; if(!(0 <= tau && tau <=1) || !(0 <= lambda && lambda <= 1)) { // calculated intersection point is outside of at least one line - so no intersection return null; } Vector2D intersectionPoint=end1.add(direction.multiply(lambda)); return intersectionPoint; } }