计算机图形学-简单种子填充算法-java实现

import javax.swing.JFrame;


public class FloodFill4 {
public static void main(String args[]){
new FloodFill4();
}
private TwoDimen env;
public FloodFill4(){
JFrame frame = new JFrame();
env = new TwoDimen();
frame.getContentPane().add(env);
frame.setBounds(100, 100, 600, 600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
int[] X = new int[] { 3, 18, 33, 15, 3 };
int[] Y = new int[] { 15, 30, 15, 3, 15 };
int i = 0;
double x, y;
double dy, dx;
int ymin, ymax;
for (i = 0; i < X.length - 1; i++) {
//System.out.println(i);
dy = Y[i + 1] - Y[i];
if (dy != 0) {
dx = (X[i + 1] - X[i]) / dy;
if (dy > 0)
x = X[i];
else
x = X[i + 1];
if (Y[i] >= Y[i + 1]) {
ymin = Y[i + 1];
ymax = Y[i];
} else {
ymin = Y[i];
ymax = Y[i + 1];
}
int k,j;
k=Math.abs(X[i+1]-X[i]);
if(Math.abs(Y[i+1]-Y[i])>k)k=Math.abs(Y[i+1]-Y[i]);
dx=(float)(X[i+1]-X[i])/k;
dy=(float)(Y[i+1]-Y[i])/k;
x=(float)(X[i]);
y=(float)(Y[i]);
for(j=0;j if (env.isFlag((int) (x+0.5), (int) (y+0.5))) {
env.setFlag((int) (x + 1.5), (int) (y+0.5));
} else {
env.setFlag((int) (x+0.5), (int) (y+0.5));
}
x=x+dx;
y=y+dy;
}
}
}
fill(15,15);
}
private void fill(int i, int j) {
if(!env.isFlag(i, j)&&!env.isFilled(i, j)){
env.drawPoint(i, j);
fill(i,j+1);
fill(i,j-1);
fill(i+1,j);
fill(i-1,j);
//fill(i+1,j+1);
//fill(i+1,j-1);
//fill(i-1,j+1);
//fill(i-1,j-1);
}
}
}

你可能感兴趣的:(计算机图形学-简单种子填充算法-java实现)