填充多边形算法

时间:2015-11-18 09:14:32

标签: java fill polygons

我正在尝试填充使用点击和线条绘制的一些多边形。我的代码在上面:

static class Pineda {
        private static Vector2d normal(final Point p0, final Point p1) {
            return new Vector2d(p1.y - p0.y, -(p1.x - p0.x));
        }

        public static double distance(Point p0, Point p1, Point p) {
            Vector2d V_P0 = new Vector2d(p0.x, p0.y);
            Vector2d V_n = normal(p0, p1);
            Vector2d V_Pos = new Vector2d(p.x, p.y);

            return (V_n.scalarProd(V_Pos) - V_n.scalarProd(V_P0)) / V_n.abs();
        }

        public static void fillPolygons() {
            int x0=0;
            int y0=0;
            Point p0= new Point(x0,y0);
            int width=400;
            int height=400;
            int nr, nr1;
            int[] data = new int[1];
            data[0]=0;


            for(int x=x0, y=y0; x<=width; x++){
                for( x=x0, y=y0; y<=height; y++){
                    Point p1= new Point(x,y);
                    for (int j = 0; j < polygons.size(); j++) {
                        nr1=0; nr=0;
                        for (int p = 0; p < polygons.get(j).size()-1; p++) {
                            nr=nr+1;
                            if (distance(p1,polygons.get(j).get(p),polygons.get(j).get(p+1))>0) nr1=nr1+1;

                        }
                        if (nr==nr1) {         //if the pixel is at the right of every line it will be drawn
                        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
                        WritableRaster raster = image.getRaster();
                        raster.setPixel(x,y,data);

                        }
                    }
                }
            }
        }

        static class Vector2d {
            private double x, y;

            Vector2d(double x, double y) {
                this.x = x;
                this.y = y;
            }

            double abs() {
                return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
            }

            double scalarProd(Vector2d v) {
                return x * v.x + y * v.y;
            }
        }
    }
}

单击按钮时调用函数fillPolygons()。在我点击它之前,一切正常,在我点击按钮之后...它没有填充多边形,而且窗口冻结,我不能做任何事情(这包括关闭窗口)。问题在哪里?

0 个答案:

没有答案