添加指向多边形 [英] Adding a point to polygon

查看:130
本文介绍了添加指向多边形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个扩展awt.Polygon类的类。我试着写,鉴于多边形和一个点再presenting一个顶点的PathIterator的方法,加点的路径中适当的位置。

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.

例如:多边形那点是(0,0)(0,10)(10,10)(10,0)给出的点(A方),(1,5)
将使多边形(0,0)(1,5)(0,10)(10,10)(10,0)

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)

在此先感谢

推荐答案

拓展上@ normalocity的想法,这似乎是一个可能的方法。

Expanding on @normalocity's idea, this appears to be a possible approach.

附录:作为参考,这种方法只使用公共API,但其他变化是可能的。

Addendum: For reference, this approach uses only public APIs, but other variations are possible.

控制台:


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();
    }
}

这篇关于添加指向多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆