Java 填充多边形算法
我正在尝试填充我用点击和线条绘制的一些多边形。我的代码在上面:Java 填充多边形算法,java,fill,polygons,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, Po
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;
}
}
}
}
静态类Pineda{
专用静态矢量2D法线(终点p0,终点p1){
返回新的向量2d(p1.y-p0.y,-(p1.x-p0.x));
}
公共静态双距离(p0点、p1点、p点){
向量2D V_P0=新向量2D(P0.x,P0.y);
向量2D V_n=正常(p0,p1);
向量2D V_Pos=新向量2D(p.x,p.y);
返回(V_n.scalarProd(V_Pos)-V_n.scalarProd(V_P0))/V_n.abs();
}
公共静态空填充多边形(){
int x0=0;
int y0=0;
点p0=新点(x0,y0);
整数宽度=400;
内部高度=400;
国际注册号,nr1;
int[]数据=新的int[1];
数据[0]=0;
对于(int x=x0,y=y0;x为什么不创建java.awt.Polygon类型的多边形,并使用Graphics2D.fill(Shape)来填充它们?我必须使用我收到的代码:)您复制了x=x0,y=y0两次,因此这是一个无限循环;x0永远不会超过x0+1