开发者

What is incorrect with my Voronoi generator?

import java.awt.Color;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

import javax.imageio.ImageIO;


public class Voronoi
{
    public static void main(String args[])
    {
        Random r = new Random();

        BufferedImage buf = new BufferedImage(500, 500, BufferedImage.TYPE_3BYTE_BGR);

        Point[] points = new Point[50];

        for(int i = 0; i < points.length; i++)
        {
            points[i] = new Point(r.nextInt(500), r.nextInt(500));
        }

        int b = Color.BLUE.getRGB(开发者_如何转开发);
        int w = Color.WHITE.getRGB();
        int g = Color.GREEN.getRGB();


        for(int i = 0; i < points.length; i++)
        {
            buf.setRGB(points[i].x, points[i].y, b);
        }

        ArrayList<Point> dis = new ArrayList<Point>();
        int min = 5000;

        for(int i = 0; i < buf.getWidth(); i++)
        {
            for(int j = 0; j < buf.getHeight(); j++)
            {
                for(int k = 0; k < points.length; k++)
                {
                    if(buf.getRGB(i, j) == b)
                        continue;

                    int d = distance(i, points[k].x, j, points[k].y);

                    if(d == min)
                    {
                        dis.add(points[k]);
                    }
                    else if(d < min)
                    {
                        dis.clear();
                        dis.add(points[k]);
                        min = d;
                    }

                }

                if(dis.size() == 1)
                {
                        buf.setRGB(i, j, w);
                }
                else if(dis.size() > 1)
                {
                    Point m = midPoint(dis);

                    buf.setRGB(m.x, m.y, g);
                }

                dis.clear();
                min = 5000;
            }
        }

        try
        {
            ImageIO.write(buf, "png", new File("this.png"));
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static int distance(int x1, int y1, int x2, int y2)
    {
        return (int)Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
    }

    public static Point midPoint(ArrayList<Point> p)
    {
        int totX = 0;
        int totY = 0;

        for(Point a: p)
        {
            totX += a.x;
            totY += a.y;
        }

        totX /= p.size();
        totY /= p.size();

        return new Point(totX, totY);
    }
}

All it generates is something like this:

What is incorrect with my Voronoi generator?

What the code is supposed to be doing: Go through each pixel, one by one, and find the point(s) [The blue dots] closest to each pixel. If there is only one point, color that pixel white. However if there are multiple points, color that green.


Here is what was wrong:

Originally:

int d = distance(i, points[k].x, j, points[k].y);

The correct version:

int d = distance(i, j, points[k].x, points[k].y);

In addition to that, I was not supposed to be averaging the correct points as I was here:

Point m = midPoint(dis);

I was supposed to merely color (i,j) green.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜