Maximum and 2nd longest diameters of polygon
I have a polygon in the form of a Point[] of Cartesian (x, y) pairs?for the sake of argument lets just say it齭 a simple closed loop and no crossing sections and I would like to calculate the second longest perpendicular diameter based on the maximum diameter which I have obtained by the following code:
publicboolean longestDiameter(Point[] points){
Point currentPoint;
Point focusPoint;
double maxDistance = 0.0;
double currentDistance = 0.0;
boolean contains =false;
Line2D.Double diameter;
for(int i = 0; i < points.length; i++){
focusPoint = points[i];
for(int j = 0; j < points.length; j++){
currentPoint = points[j];
currentDistance = focusPoint.distance(currentPoint);
if(!focusPoint.equals(currentPoint)){
diameter =new Line2D.Double(focusPoint, currentPoint);
//System.out.println("Current Point: X " + currentPoint.getX() + " Y " + currentPoint.getY());
//System.out.println("Diameter Points: X1 " + diameter.getX1() + " Y1 " + diameter.getY1() +
//" X2 " + diameter.getX2() + " Y2 " + diameter.getY2());
//contains = checkLineContainment(diameter, points);
//contains = checkLineContainment(getLinePoints(diameter), points);
contains =true;
if((maxDistance < currentDistance) && contains){
maxDistance = currentDistance;
longestDiameterPoints[0] = focusPoint;
longestDiameterPoints[1] = currentPoint;
}
}
}
}
if(maxDistance == 0.0)
returnfalse;
returntrue;
}
You齦l notice that I have checkLineContainment as a separate function. Basically I wrote this Boolean function to check to see if the points along the variable called diameter are contained inside the polygon, but I forced it to true for now. I also wrote a getLinePoints method because to my knowledge JDK 1.4.1 doesn齮 have a built in iterator which will provide a point by point array of the individual points on a line:
Its pretty long winded but it accounts for all possible slopes that you might expect, depending on where the two points being compared lie in relation to one another. This should provide me with a point by point vector which I can then use the opposite inverse slope of the maximum line to proceed in my calculation of the second longest diameter but it doesn齮.
// Gets the integer line points from a line and stores them in a vector
public Vector getLinePoints(Line2D.Double line){
Vector linePoints =new Vector();
Point2D startPoint = line.getP1();
Point2D endPoint = line.getP2();
Point2D currentPoint = startPoint;
double deltaY = (endPoint.getY() - startPoint.getY());
double deltaX = (endPoint.getX() - startPoint.getX());
double slope = 0.0;
if(deltaX != 0){
slope = deltaY / deltaX;
System.out.println("Slope: " + slope);
}
double b = (startPoint.getY() - slope*startPoint.getX());
while(!currentPoint.equals(endPoint)){
System.out.println("CurrentPoint X: " + currentPoint.getX() +" Y: " + currentPoint.getY());
System.out.println("StartPoint X: " + startPoint.getX() +" Y: " + startPoint.getY());
System.out.println("EndPoint X: " + endPoint.getX() +" Y: " + endPoint.getY());
// Positive Slope
if((startPoint.getX() > endPoint.getX()) &
(startPoint.getY() < endPoint.getY())){
System.out.println("Positive");
double y = (slope*(currentPoint.getX()-1)+b);
//System.out.println("Y=" + slope*(currentPoint.getX()-1)+b);
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX()-1, (int)roundDouble(y));
}
elseif((startPoint.getX() < endPoint.getX()) &
(startPoint.getY() > endPoint.getY())){
System.out.println("Positive");
double y = (slope*(currentPoint.getX()+1)+b);
//System.out.println("Y=" + slope*(currentPoint.getX()+1)+b);
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX()+1, (int)roundDouble(y));
}
// Negative Slope
elseif((startPoint.getX() < endPoint.getX()) &
(startPoint.getY() < endPoint.getY())){
System.out.println("Negative");
double y = (slope*(currentPoint.getX()+1)+b);
//System.out.println("Y=" + slope*(currentPoint.getX()+1)+b);
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX()+1, (int)roundDouble(y));
}
elseif((startPoint.getX() > endPoint.getX()) &
(startPoint.getY() > endPoint.getY())){
System.out.println("Negative");
double y = (slope*(currentPoint.getX()-1)+b);
//System.out.println("Y=" + slope*(currentPoint.getX()-1)+b);
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX()-1, (int)roundDouble(y));
}
// Horizontal Line
elseif((startPoint.getX() < endPoint.getX()) &
(startPoint.getY() == endPoint.getY())){
System.out.println("Zero");
double y = currentPoint.getY();
//System.out.println("Y=" + slope*(currentPoint.getX()+1)+b);
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX()+1, (int)roundDouble(y));
}
elseif((startPoint.getX() > endPoint.getX()) &
(startPoint.getY() == endPoint.getY())){
System.out.println("Zero");
double y = currentPoint.getY();
//System.out.println("Y=" + slope*(currentPoint.getX()-1)+b);
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX()-1, (int)roundDouble(y));
}
// Vertical Line
elseif(deltaX == 0 &&
(startPoint.getX() == endPoint.getX() &
startPoint.getY() < endPoint.getY()) ){
double y = currentPoint.getY()+1;
System.out.println("Undefinded");
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX(), (int)roundDouble(y));
}
elseif(deltaX == 0 &&
(startPoint.getX() == endPoint.getX() &
startPoint.getY() > endPoint.getY()) ){
int y = (int)currentPoint.getY()-1;
System.out.println("Undefinded");
System.out.println("Y=" + y);
currentPoint =new Point((int)currentPoint.getX(), (int)roundDouble(y));
}
linePoints.addElement(currentPoint);
}
for(int i = 0; i < linePoints.size(); i++){
System.out.println("Point @ " + i +" X: " + ((Point2D)linePoints.elementAt(i)).getX() +
" Y: " + ((Point2D)linePoints.elementAt(i)).getY() +"\n");
}
return linePoints;
}
I was wondering if anyone had any insight on this or had already done something similar.

