com.beartronics
Class FP

java.lang.Object
  |
  +--com.beartronics.FP

public class FP
extends java.lang.Object


Field Summary
static int E
           
static int HALF
           
static int PI
           
static int PI_OVER_2
           
static int xIntersect
           
static int yIntersect
           
 
Constructor Summary
FP()
           
 
Method Summary
static int ArcCos(int f)
          Compute ArcCos(f), 0 <= f <= 1
static int ArcSin(int f)
          Compute ArcSin(f), 0 <= f <= 1
static int ArcTan(int f)
          Computes ArcTan(f), f is a fixed point number |f| <= 1
static int Cos(int f)
          Computes COS(f), f is a fixed point number in radians.
static int Div(int x, int y)
          Divides two fixed-point numbers
static int Exp(int x)
           
static boolean intersects(int ax0, int ay0, int ax1, int ay1, int bx0, int by0, int bx1, int by1)
          Does line segment A intersection line segment B? Assumes 16 bit fixed point numbers with 16 bits of fraction.
static int intToFP(int x)
          Convert an int to a 16:16 fixed-point
static int Ln(int x)
           
static int Mul(int x, int y)
          Multiply two fixed-point numbers
static int round(int n)
          Round to nearest fixed poitn integer
static int Sin(int f)
          Computes SIN(f), f is a fixed point number in radians.
static int Sqrt(int n)
          Compute square-root of a 16:16 fixed point number
static int Tan(int f)
           
static int toInt(int x)
          Convert a 16:16 fixed-point to an int
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PI

public static final int PI

PI_OVER_2

public static final int PI_OVER_2

E

public static final int E

HALF

public static final int HALF

xIntersect

public static int xIntersect

yIntersect

public static int yIntersect
Constructor Detail

FP

public FP()
Method Detail

toInt

public static int toInt(int x)
Convert a 16:16 fixed-point to an int

intToFP

public static int intToFP(int x)
Convert an int to a 16:16 fixed-point

Mul

public static int Mul(int x,
                      int y)
Multiply two fixed-point numbers

Div

public static int Div(int x,
                      int y)
Divides two fixed-point numbers

Sqrt

public static int Sqrt(int n)
Compute square-root of a 16:16 fixed point number

round

public static int round(int n)
Round to nearest fixed poitn integer

Sin

public static int Sin(int f)
Computes SIN(f), f is a fixed point number in radians. 0 <= f <= 2PI

Cos

public static int Cos(int f)
Computes COS(f), f is a fixed point number in radians. 0 <= f <= PI/2

Tan

public static int Tan(int f)

ArcTan

public static int ArcTan(int f)
Computes ArcTan(f), f is a fixed point number |f| <= 1

For the inverse tangent calls, all approximations are valid for |t| <= 1. To compute ATAN(t) for t > 1, use ATAN(t) = PI/2 - ATAN(1/t). For t < -1, use ATAN(t) = -PI/2 - ATAN(1/t).


ArcSin

public static int ArcSin(int f)
Compute ArcSin(f), 0 <= f <= 1

ArcCos

public static int ArcCos(int f)
Compute ArcCos(f), 0 <= f <= 1

Exp

public static int Exp(int x)

Ln

public static int Ln(int x)

intersects

public static boolean intersects(int ax0,
                                 int ay0,
                                 int ax1,
                                 int ay1,
                                 int bx0,
                                 int by0,
                                 int bx1,
                                 int by1)
Does line segment A intersection line segment B? Assumes 16 bit fixed point numbers with 16 bits of fraction. For debugging, side effect xint, yint, the intersection point.
 Algorithm 
 
 As an example of algorithm development, consider the intersection of
 two line segments.  Given line segment A goes from point XA1 and YA1
 to point XA2 and YA2 and given line segment B goes from point XB1 and
 YB1 to point XB2 and YB2.  Find whether there is zero, one, or an
 infinite number of points of intersection (the line segments overlap)
 and the values of the points of intersection.  Assume all numbers are
 double.
 
 For case 1 where line segment A is not vertical, line segment B is not
 vertical, and line segment A is not parallel to line segment B, the
 equations for line segment A and B are:
 
 
 XMA = (YA2-YA1)/(XA2-XA1) = slope of line segment A
 XBA = YA1 - XA1*XMA = Y-intercept for line segment A
 YA = XMA*XA + XBA
 
 XMB = (YB2-YB1)/(XB2-XB1) = slope of line segment B
 XBB = YB1 - XB1*XMB = Y-intercept for line segment B
 YB = XMB*XB + XBB
 
 At the intersection of line segment A and B, XA=XB=XINT and YA=YB=YINT.
 YINT = XMA*XINT + XBA
 YINT = XMB*XINT + XBB
 XMA*XINT + XBA = XMB*XINT + XBB
 XMA*XINT - XMB*XINT = XBB - XBA
 XINT*(XMA-XMB) = XBB - XBA
 XINT = (XBB-XBA)/(XMA-XMB)
 YINT = XMA*XINT + XBA
 There is one point of intersection.
 
 For case 2 where line segment A is vertical (XA1 is close to XA2) and
 line segment B is not vertical, the equations for line segment A and B
 are:
 
 XA = 0.5*(XA1+XA2)
 
 XMB = (YB2-YB1)/(XB2-XB1) = slope of line segment B
 XBB = YB1 - XB1*XMB = Y-intercept for line segment B
 YB = XMB*XB + XBB
 
 At the intersection of line segment A and B, XA=XB=XINT and YA=YB=YINT.
 XINT = XA
 YINT = XMB*XINT + XBB
 There is one point of intersection.
 
 For case 3 where line segment A is not vertical and line segment B is
 vertical (XB1 is close to XB2), the equations for line segment A and B
 are:
 
 XMA = (YA2-YA1)/(XA2-XA1) = slope of line segment A
 XBA = YA1 - XA1*XMA = Y-intercept for line segment A
 YA = XMA*XA + XBA
 
 XB= 0.5*(XB1+XB2)
 
 At the intersection of line segment A and B, XA=XB=XINT and YA=YB=YINT.
 XINT = XB
 YINT = XMA*XINT + XBA
 There is one point of intersection.
 
 For case 4 where line segment A is vertical (XA1 is close to XA2) and
 line segment B is vertical (XB1 is close to XB2), the distance between
 the parallel line segments is:
 
 DIST = ABS ( 0.5*(XA1+XA2) - 0.5*(XB1+XB2) )
 
 If DIST is close to zero, then:
 
 XINT1 = 0.5*(0.5*(XA1+XA2)+0.5*(XB1+XB2))
 YINT1 = MAX(MIN(YA1,YA2),MIN(YB1,YB2))
 XINT2 = XINT1
 YINT2 = MIN(MAX(YA1,YA2),MAX(YB1,YB2))
 There are two points of intersection.
 
 For case 5 where line segment A is not vertical, line segment B is not
 vertical, and line segment A is parallel to line segment B (XMA is
 close to XMB), the equations for line segment A and B are:
 
 XMA = (YA2-YA1)/(XA2-XA1) = slope of line segment A
 XBA = YA1 - XA1*XMA = Y-intercept for line segment A
 YA = XMA*XA + XBA
 
 XMB = (YB2-YB1)/(XB2-XB1) = slope of line segment B
 XBB = YB1 - XB1*XMB = Y-intercept for line segment B
 YB = XMB*XB + XBB
 
 The distance between the parallel line segments is:
 
 DIST = ABS(XBA-XBB)*COS(ATAN(0.5*(XMA+XMB)))
 
 If DIST is close to zero, then:
 
 XINT1 = MAX(MIN(XA1,XA2),MIN(XB1,XB2))
 YINT1 = MAX(MIN(YA1,YA2),MIN(YB1,YB2))
 XINT2 = MIN(MAX(XA1,XA2),MAX(XB1,XB2))
 YINT2 = MIN(MAX(YA1,YA2),MAX(YB1,YB2))
 There are two points of intersection.
 
 After the point or points of intersection are calculated, each
 solution must be checked to ensure that the point of intersection lies
 on line segment A and B by checking if XINT >= MIN(XA1,XA2) and XINT
 <= MAX(XA1,XA2) and YINT >= MIN(YA1,YA2) and YINT <= MAX(YA1,YA2) and
 checking if XINT >= MIN(XB1,XB2) and XINT <= MAX(XB1,XB2) and YINT >=
 MIN(XB1,XB2) and YINT <= MAX(YB1,YB2).
 
 Note that case 2, 3, 4, and 5 are all special instances of case 1
 where a division by zero would have caused the creation of an infinite
 number and thus a program error.