0

I am trying to fill some polygons that i have drawn using clicks and lines. My code is above:

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

The function fillPolygons() is called when i click on a button. Before i click it, everything works, after i click the button well.. it is not filling the polygons and furthermore the window freezes and i cannot do anything( this includes closing the window). Where is the problem?

user5274714
  • 25
  • 1
  • 10

0 Answers0