Adding a point to a polygon

I created a class that extends the awt.Polygon class. I am trying to write a method that specified a PathIterator for a polygon and a Point representing a vertex adds a point to the appropriate location on the path.

For example: Polygon points, which are points (0,0) (0,10) (10,10) (10,0) (square A), taking into account point (1,5) would make a polygon (0,0) ( 1.5) (0.10) (10.10) (10.0)

Thanks in advance

+3
source share
2 answers

Extending the idea of ​​@normalocity, this seems to be a possible approach.

Application. For reference, this approach uses only public APIs, but other options 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]

:

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

"addPoint (x, y)", ( ), , ​​ (, , , , .).

, , java.awt.Polygon public class InsertablePolygon extends java.awt.Polygon, , - public void insertPoint(int index, Point additionalPoint).

addPoint int[] xpoints int[] ypoints, . ( , , ), .

+1

All Articles