use of com.revolsys.geometry.math.DD in project com.revolsys.open by revolsys.
the class CGAlgorithmsDD method intersection.
/**
* Computes an intersection point between two lines
* using DD arithmetic.
* Currently does not handle case of parallel lines.
*
* @param p1
* @param p2
* @param q1
* @param q2
* @return
*/
public static Point intersection(final Point p1, final Point p2, final Point q1, final Point q2) {
final DD denom1 = DD.valueOf(q2.getY()).selfSubtract(q1.getY()).selfMultiply(DD.valueOf(p2.getX()).selfSubtract(p1.getX()));
final DD denom2 = DD.valueOf(q2.getX()).selfSubtract(q1.getX()).selfMultiply(DD.valueOf(p2.getY()).selfSubtract(p1.getY()));
final DD denom = denom1.subtract(denom2);
/**
* Cases:
* - denom is 0 if lines are parallel
* - intersection point lies within line segment p if fracP is between 0 and 1
* - intersection point lies within line segment q if fracQ is between 0 and 1
*/
final DD numx1 = DD.valueOf(q2.getX()).selfSubtract(q1.getX()).selfMultiply(DD.valueOf(p1.getY()).selfSubtract(q1.getY()));
final DD numx2 = DD.valueOf(q2.getY()).selfSubtract(q1.getY()).selfMultiply(DD.valueOf(p1.getX()).selfSubtract(q1.getX()));
final DD numx = numx1.subtract(numx2);
final double fracP = numx.selfDivide(denom).doubleValue();
final double x = DD.valueOf(p1.getX()).selfAdd(DD.valueOf(p2.getX()).selfSubtract(p1.getX()).selfMultiply(fracP)).doubleValue();
final DD numy1 = DD.valueOf(p2.getX()).selfSubtract(p1.getX()).selfMultiply(DD.valueOf(p1.getY()).selfSubtract(q1.getY()));
final DD numy2 = DD.valueOf(p2.getY()).selfSubtract(p1.getY()).selfMultiply(DD.valueOf(p1.getX()).selfSubtract(q1.getX()));
final DD numy = numy1.subtract(numy2);
final double fracQ = numy.selfDivide(denom).doubleValue();
final double y = DD.valueOf(q1.getY()).selfAdd(DD.valueOf(q2.getY()).selfSubtract(q1.getY()).selfMultiply(fracQ)).doubleValue();
return new PointDoubleXY(x, y);
}
use of com.revolsys.geometry.math.DD in project com.revolsys.open by revolsys.
the class DDBasicTest method checkReciprocal.
private void checkReciprocal(final double x, final double errBound) {
final DD xdd = new DD(x);
final DD rr = xdd.reciprocal().reciprocal();
final double err = xdd.subtract(rr).doubleValue();
// System.out.println("DD Recip = " + xdd + " DD delta= " + err
// + " double recip delta= " + (x - 1.0 / (1.0 / x)));
assertTrue(err <= errBound);
}
use of com.revolsys.geometry.math.DD in project com.revolsys.open by revolsys.
the class DDBasicTest method checkSqrt.
private void checkSqrt(final DD x, final double errBound) {
final DD sqrt = x.sqrt();
final DD x2 = sqrt.multiply(sqrt);
checkErrorBound("Sqrt", x, x2, errBound);
}
use of com.revolsys.geometry.math.DD in project com.revolsys.open by revolsys.
the class DDBasicTest method testDivideMultiply.
public void testDivideMultiply() {
checkDivideMultiply(DD.PI, DD.E, 1e-30);
checkDivideMultiply(new DD(39.4), new DD(10), 1e-30);
}
use of com.revolsys.geometry.math.DD in project com.revolsys.open by revolsys.
the class DDBasicTest method checkBinomial2.
void checkBinomial2(final double a, final double b) {
// binomial product
final DD add = new DD(a);
final DD bdd = new DD(b);
final DD aPlusb = add.add(bdd);
final DD aSubb = add.subtract(bdd);
final DD abProd = aPlusb.multiply(aSubb);
// System.out.println("(a+b)^2 = " + abSq);
// expansion
final DD a2dd = add.multiply(add);
final DD b2dd = bdd.multiply(bdd);
// System.out.println("2ab+b^2 = " + sum);
// this should equal b^2
final DD diff = abProd.subtract(a2dd).negate();
// System.out.println("(a+b)^2 - a^2 = " + diff);
final DD delta = diff.subtract(b2dd);
// System.out.println();
// System.out.println("A = " + a + ", B = " + b);
// System.out.println("[DD] (a+b)(a-b) = " + abProd
// + " -((a^2 - b^2) - a^2) = " + diff + " delta = " + delta);
// printBinomialSquareDouble(a,b);
final boolean isSame = diff.equals(b2dd);
assertTrue(isSame);
final boolean isDeltaZero = delta.isZero();
assertTrue(isDeltaZero);
}
Aggregations