java-向多边形添加点

我创建了一个扩展awt.Polygon类的类.我正在尝试编写一种方法,该方法给出了多边形的PathIterator和一个表示顶点的Point,将点添加到路径中的适当位置.

例如:给定点(1,5)的点为(0,0)(0,10)(10,10)(10,0)(A正方形)的多边形
将使多边形(0,0)(1,5)(0,10)(10,10)(10,0)

提前致谢

解决方法:

扩展@normalocity的想法,这似乎是一种可能的方法.

附录:仅供参考,此方法仅使用公共API,但其他变体也是可能的.

安慰:

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]

码:

import java.awt.Point;
import java.awt.Polygon;
import java.awt.geom.PathIterator;
import java.util.Arrays;

/** @see https://*.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();
    }
}
上一篇:GIU 监听事件


下一篇:截屏代码