net.java.dev.joode.util
Class Line2D

java.lang.Object
  extended by net.java.dev.joode.util.Line2D

public class Line2D
extends java.lang.Object

Implementation of a line and some math to go with it. Ported from Phys2D


Constructor Summary
Line2D(float[] x1, float[] x2)
          Create a new line based on two points
Line2D(float x, float y)
          Create a new line based on the origin and a single point
Line2D(float x1, float y1, float x2, float y2)
          Create a new line based on two points
Line2D(ROVector2 start, ROVector2 end)
          Create a new line based on two points
 
Method Summary
 float distance(ROVector2 point)
          Get the shortest distance from a point to this line
 float distanceSquared(ROVector2 point)
          Get the shortest distance squared from a point to this line
 void getClosestPoint(ROVector2 point, Vector2 result)
          Get the closest point on the line to a given point
 float getDX()
          Get the x direction of this line
 float getDY()
          Get the y direction of this line
 ROVector2 getEnd()
          Get the end point of the line
 Line2D getPositionedLine(ROVector2 displacement, float rotation)
          Get a line starting a x,y and ending offset from the current end point.
 ROVector2 getStart()
          Get the start point of the line
 Vector2[] getVertices(ROVector2 displacement, float rotation)
          Return a translated and rotated line.
 float getX1()
          Get the x coordinate of the start point
 float getX2()
          Get the x coordinate of the end point
 float getY1()
          Get the y coordinate of the start point
 float getY2()
          Get the y coordinate of the end point
 Vector2 intersect(Line2D other)
          Intersect this line with another
 float length()
          Find the length of the line
 float lengthSquared()
          Find the length of the line squared (cheaper and good for comparisons)
 void move(ROVector2 v)
          Move this line a certain amount
 void set(ROVector2 start, ROVector2 end)
          Configure the line
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Line2D

public Line2D(float x,
              float y)
Create a new line based on the origin and a single point

Parameters:
x - The end point of the line
y - The end point of the line

Line2D

public Line2D(float x1,
              float y1,
              float x2,
              float y2)
Create a new line based on two points

Parameters:
x1 - The x coordinate of the start point
y1 - The y coordinate of the start point
x2 - The x coordinate of the end point
y2 - The y coordinate of the end point

Line2D

public Line2D(float[] x1,
              float[] x2)
Create a new line based on two points

Parameters:
x1 - The start coordinate packed into a two element array
x2 - The end coordinate packed into a two element array

Line2D

public Line2D(ROVector2 start,
              ROVector2 end)
Create a new line based on two points

Parameters:
start - The start point
end - The end point
Method Detail

getStart

public ROVector2 getStart()
Get the start point of the line

Returns:
The start point of the line

getEnd

public ROVector2 getEnd()
Get the end point of the line

Returns:
The end point of the line

length

public float length()
Find the length of the line

Returns:
The the length of the line

lengthSquared

public float lengthSquared()
Find the length of the line squared (cheaper and good for comparisons)

Returns:
The length of the line squared

set

public void set(ROVector2 start,
                ROVector2 end)
Configure the line

Parameters:
start - The start point of the line
end - The end point of the line

getDX

public float getDX()
Get the x direction of this line

Returns:
The x direction of this line

getDY

public float getDY()
Get the y direction of this line

Returns:
The y direction of this line

getX1

public float getX1()
Get the x coordinate of the start point

Returns:
The x coordinate of the start point

getY1

public float getY1()
Get the y coordinate of the start point

Returns:
The y coordinate of the start point

getX2

public float getX2()
Get the x coordinate of the end point

Returns:
The x coordinate of the end point

getY2

public float getY2()
Get the y coordinate of the end point

Returns:
The y coordinate of the end point

distance

public float distance(ROVector2 point)
Get the shortest distance from a point to this line

Parameters:
point - The point from which we want the distance
Returns:
The distance from the line to the point

distanceSquared

public float distanceSquared(ROVector2 point)
Get the shortest distance squared from a point to this line

Parameters:
point - The point from which we want the distance
Returns:
The distance squared from the line to the point

getClosestPoint

public void getClosestPoint(ROVector2 point,
                            Vector2 result)
Get the closest point on the line to a given point

Parameters:
point - The point which we want to project
result - The point on the line closest to the given point

getPositionedLine

public Line2D getPositionedLine(ROVector2 displacement,
                                float rotation)
Get a line starting a x,y and ending offset from the current end point. Curious huh?

Parameters:
displacement - The displacement of the line
rotation - The rotation of the line in radians
Returns:
The newly created line

getVertices

public Vector2[] getVertices(ROVector2 displacement,
                             float rotation)
Return a translated and rotated line.

Parameters:
displacement - The displacement of the line
rotation - The rotation of the line in radians
Returns:
The two endpoints of this line

move

public void move(ROVector2 v)
Move this line a certain amount

Parameters:
v - The amount to move the line

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object
See Also:
Object.toString()

intersect

public Vector2 intersect(Line2D other)
Intersect this line with another

Parameters:
other - The other line we should intersect with
Returns:
The intersection point or null if the lines are parallel