/*
 * jssf - Java Side Scroller Format library
 * Copyright (C) 2015 Delwink, LLC
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, version 3 only.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.delwink.jssf.geom;

/**
 * A line with fixed length.
 * @author David McMackins II
 */
public class LineSegment {
    private Point start, end;
    
    public LineSegment(Point start, Point end) {
        this.setPoints(start, end);
    }
    
    public LineSegment(LineSegment ls) {
        this.setLineSegment(ls);
    }
    
    public final void setPoints(Point start, Point end) {
        this.start = start;
        this.end = end;
    }
    
    public final void setLineSegment(LineSegment ls) {
        this.setPoints(ls.getStart(), ls.getEnd());
    }
    
    public void shift(float x, float y) {
        this.start.shift(x, y);
        this.end.shift(x, y);
    }
    
    public Point getIntersection(LineSegment other) {
        LineSegment diff = new LineSegment(
                new Point(this.end.getX() - this.start.getX(), this.end.getY() - this.start.getY()),
                new Point(other.end.getX() - other.start.getX(), other.end.getY() - other.start.getY()));
        
        float div = -(diff.end.getX() * diff.start.getY()) + (diff.start.getX() * diff.end.getY());
        if (div == 0)
            return null;
        
        float xDiff = this.start.getX() - other.start.getX();
        float yDiff = this.start.getY() - other.start.getY();
        float s = ((-diff.start.getY() * xDiff) + (diff.start.getX() * yDiff)) / div;
        float t = ((diff.end.getX() * yDiff) - (diff.end.getY() * xDiff)) / div;
        
        if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
            return new Point(this.start.getX() + (t * diff.start.getX()),
                    this.start.getY() + (t * diff.start.getY()));
        
        return null;
    }
    
    public Point getStart() {
        return new Point(this.start);
    }
    
    public void setStart(Point start) {
        this.start = start;
    }
    
    public Point getEnd() {
        return new Point(this.end);
    }
    
    public void setEnd(Point end) {
        this.end = end;
    }
}

@@ -1458,41 +1458,59 @@
 ent 
-diff = new LineSegment(%0A 
+l1 = this;%0A        LineSegment l2 = other;%0A
         
     
@@ -1509,29 +1509,35 @@
     
+%0A
        
-new Point(this
+ float deltaX1 = l1
 .end
@@ -1542,28 +1542,26 @@
 nd.getX() - 
-this
+l1
 .start.getX(
@@ -1565,14 +1565,36 @@
 tX()
-, this
+;%0A        float deltaY1 = l1
 .end
@@ -1603,20 +1603,18 @@
 etY() - 
-this
+l1
 .start.g
@@ -1622,42 +1622,36 @@
 tY()
-),%0A                new Point(other
+;%0A        float deltaX2 = l2
 .end
@@ -1652,37 +1652,34 @@
 l2.end.getX() - 
-other
+l2
 .start.getX(), o
@@ -1679,15 +1679,36 @@
 tX()
-, other
+;%0A        float deltaY2 = l2
 .end
@@ -1709,37 +1709,34 @@
 l2.end.getY() - 
-other
+l2
 .start.getY()));
@@ -1724,34 +1724,32 @@
  l2.start.getY()
-))
 ;%0A        %0A     
@@ -1767,85 +1767,48 @@
 v = 
--
 (d
-iff.end.getX() * diff.start.getY()) + (diff.start.getX() * diff.end.getY()
+eltaX1 * deltaY2) - (deltaX2 * deltaY1
 );%0A 
@@ -1848,24 +1848,71 @@
 return null;
+ // this can happen if the lines are collinear!
 %0A        %0A  
@@ -1927,28 +1927,26 @@
 oat xDiff = 
-this
+l1
 .start.getX(
@@ -1945,29 +1945,26 @@
 rt.getX() - 
-other
+l2
 .start.getX(
@@ -1988,20 +1988,18 @@
 yDiff = 
-this
+l1
 .start.g
@@ -2006,21 +2006,18 @@
 etY() - 
-other
+l2
 .start.g
@@ -2047,60 +2047,39 @@
 = ((
--diff.start.getY() * x
+deltaX1 * y
 Diff) 
-+ (diff.start.getX() * y
+- (deltaY1 * x
 Diff
@@ -2113,50 +2113,34 @@
  ((d
-iff.end.getX() * yDiff) - (diff.end.getY()
+eltaX2 * yDiff) - (deltaY2
  * x
@@ -2237,28 +2237,26 @@
 n new Point(
-this
+l1
 .start.getX(
@@ -2269,51 +2269,19 @@
  * d
-iff.start.getX()),%0A                    this
+eltaX1), l1
 .sta
@@ -2302,24 +2302,14 @@
  * d
-iff.start.getY()
+eltaY1
 ));%0A

Improve readability of intersection point finder