|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||
java.lang.Object | +--com.beartronics.FP
| 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 |
public static final int PI
public static final int PI_OVER_2
public static final int E
public static final int HALF
public static int xIntersect
public static int yIntersect
| Constructor Detail |
public FP()
| Method Detail |
public static int toInt(int x)
public static int intToFP(int x)
public static int Mul(int x,
int y)
public static int Div(int x,
int y)
public static int Sqrt(int n)
public static int round(int n)
public static int Sin(int f)
public static int Cos(int f)
public static int Tan(int f)
public static int ArcTan(int f)
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).
public static int ArcSin(int f)
public static int ArcCos(int f)
public static int Exp(int x)
public static int Ln(int x)
public static boolean intersects(int ax0,
int ay0,
int ax1,
int ay1,
int bx0,
int by0,
int bx1,
int by1)
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.
|
|||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||