More Line methods.

This commit is contained in:
Michael Bayne
2011-06-09 16:36:09 -07:00
parent 61dbc0e38d
commit bdc3edb985
3 changed files with 140 additions and 0 deletions
@@ -43,6 +43,46 @@ public abstract class AbstractLine implements ILine
return new Point(getX2(), getY2());
}
@Override // from interface ILine
public float pointLineDistSq (float px, float py) {
return Geometry.pointLineDistSq(px, py, getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointLineDistSq (IPoint p) {
return Geometry.pointLineDistSq(p.getX(), p.getY(), getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointLineDist (float px, float py) {
return Geometry.pointLineDist(px, py, getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointLineDist (IPoint p) {
return Geometry.pointLineDist(p.getX(), p.getY(), getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointSegDistSq (float px, float py) {
return Geometry.pointSegDistSq(px, py, getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointSegDistSq (IPoint p) {
return Geometry.pointSegDistSq(p.getX(), p.getY(), getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointSegDist (float px, float py) {
return Geometry.pointSegDist(px, py, getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public float pointSegDist (IPoint p) {
return Geometry.pointSegDist(p.getX(), p.getY(), getX1(), getY1(), getX2(), getY2());
}
@Override // from interface ILine
public Line clone () {
return new Line(getX1(), getY1(), getX2(), getY2());
+58
View File
@@ -80,6 +80,64 @@ public class Geometry
|| linesIntersect(rr, ry, rx, rb, x1, y1, x2, y2);
}
/**
* Returns the square of the distance from the specified point to the specified line.
*/
public static float pointLineDistSq (float px, float py,
float x1, float y1, float x2, float y2) {
x2 -= x1;
y2 -= y1;
px -= x1;
py -= y1;
float s = px * y2 - py * x2;
return (s * s) / (x2 * x2 + y2 * y2);
}
/**
* Returns the distance from the specified point to the specified line.
*/
public static float pointLineDist (float px, float py, float x1, float y1, float x2, float y2) {
return (float)Math.sqrt(pointLineDistSq(px, py, x1, y1, x2, y2));
}
/**
* Returns the square of the distance between the specified point and the specified line
* segment.
*/
public static float pointSegDistSq (float px, float py,
float x1, float y1, float x2, float y2) {
// A = (x2 - x1, y2 - y1)
// P = (px - x1, py - y1)
x2 -= x1; // A = (x2, y2)
y2 -= y1;
px -= x1; // P = (px, py)
py -= y1;
float dist;
if (px * x2 + py * y2 <= 0.0) { // P*A
dist = px * px + py * py;
} else {
px = x2 - px; // P = A - P = (x2 - px, y2 - py)
py = y2 - py;
if (px * x2 + py * y2 <= 0.0) { // P*A
dist = px * px + py * py;
} else {
dist = px * y2 - py * x2;
dist = dist * dist / (x2 * x2 + y2 * y2); // pxA/|A|
}
}
if (dist < 0) {
dist = 0;
}
return dist;
}
/**
* Returns the distance between the specified point and the specified line segment.
*/
public static float pointSegDist (float px, float py, float x1, float y1, float x2, float y2) {
return (float)Math.sqrt(pointSegDistSq(px, py, x1, y1, x2, y2));
}
/**
* Returns a string describing the supplied point, of the form <code>+x+y</code>,
* <code>+x-y</code>, <code>-x-y</code>, etc.
+42
View File
@@ -52,6 +52,48 @@ public interface ILine extends IShape, Cloneable
*/
Point getP2 ();
/**
* Returns the square of the distance from the specified point to the line defined by this line
* segment.
*/
float pointLineDistSq (float px, float py);
/**
* Returns the square of the distance from the supplied point to the line defined by this line
* segment.
*/
float pointLineDistSq (IPoint p);
/**
* Returns the distance from the specified point to the line defined by this line segment.
*/
float pointLineDist (float px, float py);
/**
* Returns the distance from the supplied point to the line defined by this line segment.
*/
float pointLineDist (IPoint p);
/**
* Returns the square of the distance from the specified point this line segment.
*/
float pointSegDistSq (float px, float py);
/**
* Returns the square of the distance from the supplied point this line segment.
*/
float pointSegDistSq (IPoint p);
/**
* Returns the distance from the specified point this line segment.
*/
float pointSegDist (float px, float py);
/**
* Returns the distance from the supplied point this line segment.
*/
float pointSegDist (IPoint p);
/**
* Returns a mutable copy of this line.
*/