/** * Project the value given along the line segment, where 0 represents A and 1 represents B. * Tolerance is used to snap points to A or B if close enough. If u < 0 return A and if u > 1 * return B and the point is beyond the tolerance, return null * * @param u * @param tolerance tolerance for snapping to end points * @param target target vector * @throws IllegalArgumentException if u was infinite or NaN * @throws NullPointerException if tolerance or target was null */ public void projectClosest(double u, Tolerance tolerance, VectBuilder target) throws NullPointerException, IllegalArgumentException { double x = (u * (bx - ax)) + ax; double y = (u * (by - ay)) + ay; if ((u < 0) || tolerance.match(x, y, ax, ay)) { getA(target); } else if ((u > 1) || tolerance.match(x, y, bx, by)) { getB(target); } else { target.set(x, y); } }
static boolean project( double ax, double ay, double bx, double by, double u, Tolerance tolerance, VectBuilder target) { double x = (u * (bx - ax)) + ax; double y = (u * (by - ay)) + ay; if (tolerance.match(x, y, ax, ay)) { target.set(ax, ay); return true; } else if (tolerance.match(x, y, bx, by)) { target.set(bx, by); return true; } else { target.set(x, y); return ((u > 0) && (u < 1)); } }
/** * Determine if segments intersect. Segments which do not intersect, but are within the tolerance * given from each other are considered to intersect unless they are parallel * * @param line * @param tolerance * @return true if segments intersect, false otherwise * @throws NullPointerException if line or tolerance was null */ public boolean intersectsSeg(Line line, Tolerance tolerance) throws NullPointerException { double jax = line.ax; double jay = line.ay; double jbx = line.bx; double jby = line.by; double denom = getDenom(ax, ay, bx, by, jax, jay, jbx, jby); if (denom == 0.0) { // Lines are parallel. return false; } double ui = ((jbx - jax) * (ay - jay) - (jby - jay) * (ax - jax)) / denom; // projected distance along i and j double uj = ((bx - ax) * (ay - jay) - (by - ay) * (ax - jax)) / denom; if ((ui >= 0) && (ui <= 1) && (uj >= 0) && (uj <= 1)) { return true; } double x = (ui * (bx - ax)) + ax; double y = (ui * (by - ay)) + ay; if (ui < 0) { if (!tolerance.match(x, y, ax, ay)) { return false; } } else if (ui > 1) { if (!tolerance.match(x, y, bx, by)) { return false; } } if (uj < 0) { if (!tolerance.match(x, y, jax, jay)) { return false; } } else if (uj > 1) { if (!tolerance.match(x, y, jbx, jby)) { return false; } } return true; }
static boolean intersectionSegInternal( double ax, double ay, double bx, double by, double jax, double jay, double jbx, double jby, Tolerance tolerance, VectBuilder target) throws NullPointerException { if (Vect.compare(ax, ay, bx, by) > 0) { double tmp = ax; ax = bx; bx = tmp; tmp = ay; ay = by; by = tmp; } if (Vect.compare(jax, jay, jbx, jby) > 0) { double tmp = jax; jax = jbx; jbx = tmp; tmp = jay; jay = jby; jby = tmp; } if (compare(ax, ay, bx, by, jax, jay, jbx, jby) > 0) { double tmp = ax; ax = jax; jax = tmp; tmp = ay; ay = jay; jay = tmp; tmp = bx; bx = jbx; jbx = tmp; tmp = by; by = jby; jby = tmp; } double denom = getDenom(ax, ay, bx, by, jax, jay, jbx, jby); if (denom == 0.0) { // Lines are parallel. return false; } double ui = ((jbx - jax) * (ay - jay) - (jby - jay) * (ax - jax)) / denom; // projected distance along i and j double uj = ((bx - ax) * (ay - jay) - (by - ay) * (ax - jax)) / denom; double x, y; if (ax == bx) { x = ax; } else if (jax == jbx) { x = jax; } else { x = (ui * (bx - ax)) + ax; } if (ay == by) { y = ay; } else if (jay == jby) { y = jay; } else { y = (ui * (by - ay)) + ay; } boolean ia = tolerance.match(x, y, ax, ay); boolean ib = tolerance.match(x, y, bx, by); boolean i = ia || ib || ((ui >= 0) && (ui <= 1)); boolean ja = tolerance.match(x, y, jax, jay); boolean jb = tolerance.match(x, y, jbx, jby); boolean j = ja || jb || ((uj >= 0) && (uj <= 1)); if (i && j) { if (ia) { target.set(ax, ay); } else if (ib) { target.set(bx, by); } else if (ja) { target.set(jax, jay); } else if (jb) { target.set(jbx, jby); } else { target.set(x, y); } return true; } return false; }
/** * Determine if this line is parallell to that given, with differences in slope within the * tolerance * * @param line * @param tolerance * @return * @throws NullPointerException if line or tolerance was null */ public boolean isParallel(Line line, Tolerance tolerance) throws NullPointerException { double denom = getDenom(ax, ay, bx, by, line.ax, line.ay, line.bx, line.by); return tolerance.match(denom, 0); }
/** * Determine if the distance between the end points is greater than the tolerance given * * @param tolerance * @return true if distance is greater, false otherwise */ public boolean isValid(Tolerance tolerance) { return (!tolerance.match(ax, ay, bx, by)); }