I've created a class that extends the awt.Polygon class. I'm trying to write a method that given the PathIterator of the polygon and a Point representing a vertex, adds the point at the appropriate location in the path.
For Example: A Polygon thats points are (0,0) (0,10) (10,10) (10,0) (A square), given the point (1,5) would make the polygon (0,0) (1,5) (0,10) (10,10) (开发者_开发知识库10,0)
Thanks in advance
Expanding on @normalocity's idea, this appears to be a possible approach.
Addendum: For reference, this approach uses only public APIs, but other variations are possible.
Console:
MoveTo: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] LineTo: [0.0, 10.0, 0.0, 0.0, 0.0, 0.0] LineTo: [10.0, 10.0, 0.0, 0.0, 0.0, 0.0] LineTo: [10.0, 0.0, 0.0, 0.0, 0.0, 0.0] Close: [10.0, 0.0, 0.0, 0.0, 0.0, 0.0] MoveTo: [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] LineTo: [1.0, 5.0, 0.0, 0.0, 0.0, 0.0] LineTo: [0.0, 10.0, 0.0, 0.0, 0.0, 0.0] LineTo: [10.0, 10.0, 0.0, 0.0, 0.0, 0.0] LineTo: [10.0, 0.0, 0.0, 0.0, 0.0, 0.0] Close: [10.0, 0.0, 0.0, 0.0, 0.0, 0.0]
Code:
import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.PathIterator;
import java.util.Arrays;
/** @see http://stackoverflow.com/questions/5877646 */
public class MyPoly extends Polygon {
public static void main(String[] args) {
final MyPoly square = new MyPoly();
square.addPoint(0, 0);
square.addPoint(0, 10);
square.addPoint(10, 10);
square.addPoint(10, 0);
System.out.println(square.toString());
MyPoly pentagon = square.insert(1, new Point(1, 5));
System.out.println(pentagon.toString());
}
/**
* Insert a point at the specified index
*
* @param index at which to insert the new point
* @param point the <code>Point</code> to insert
* @return a new <code>Polygon</code> with the new <code>Point</code>
*/
public MyPoly insert(int index, Point point) {
MyPoly mp = new MyPoly();
PathIterator pi = this.getPathIterator(null);
double[] coords = new double[6];
int i = 0;
while (!pi.isDone()) {
if (i == index) {
mp.addPoint(point.x, point.y);
} else {
if (pi.currentSegment(coords) != PathIterator.SEG_CLOSE) {
mp.addPoint((int) coords[0], (int) coords[1]);
}
pi.next();
}
i++;
}
return mp;
}
@Override
public String toString() {
PathIterator pi = this.getPathIterator(null);
double[] coords = new double[6];
StringBuilder sb = new StringBuilder();
while (!pi.isDone()) {
int kind = pi.currentSegment(coords);
switch (kind) {
case PathIterator.SEG_MOVETO:
sb.append("MoveTo: ");
break;
case PathIterator.SEG_LINETO:
sb.append("LineTo: ");
break;
case PathIterator.SEG_CLOSE:
sb.append("Close: ");
break;
default:
throw new IllegalArgumentException("Bad path segment");
}
sb.append(Arrays.toString(coords));
sb.append("\n");
pi.next();
}
return sb.toString();
}
}
Try using the "addPoint(x, y)" method, except write your own version (or override it) such that it lets you specify where the point is inserted in the series of points (e.g. first, second, third, etc.).
So, write a class that inherits from java.awt.Polygon public class InsertablePolygon extends java.awt.Polygon
, and define a method on it, something like, public void insertPoint(int index, Point additionalPoint)
.
Inside the additionalPoint method, you should have direct access to the int[] xpoints
and int[] ypoints
arrays, which store the information. Simply modify those arrays (or copy them, insert your point, and then replace them), and you should be good.
精彩评论