So at first click nothing happens than, at the second click a square appears from the line of the te two click coordinates, After this i have to fill this square, this is ok I did this.
Now how can I fill the screen like this ?
“1” is the first click, “2” is the second click.
My code so far:
boolean first=true; boolean done = false;
int x1,x2,y1,y2,xl,yl,x3,y3,x4,y4;
void setup(){
size(640,480);
}
void draw(){
background(240);
if(done){
if(x1 <= x2){
//draw left
if(y1 <=y2){
yl = y2-y1;
xl = x2-x1;
szR(x1,y1,x2,y2);
x3 = x1-yl; y3 =y1+xl;
szR(x1,y1,x3,y3);
x4 = x2-yl; y4 = y2+xl;
szR(x2,y2,x4,y4);
szR(x3,y3,x4,y4);
scanLine();
}else{
yl = y2-y1;
xl = x1-x2;
szR(x1,y1,x2,y2);
x3 = x1-yl; y3 =y1-xl;
szR(x1,y1,x3,y3);
x4 = x2-yl; y4 = y2-xl;
szR(x2,y2,x4,y4);
szR(x3,y3,x4,y4);
scanLine();
}
}else{
//draw right
if(y1 <=y2){
yl = y1-y2;
xl = x1-x2;
szR(x1,y1,x2,y2);
x3 = x1+yl; y3 =y1-xl;
szR(x1,y1,x3,y3);
x4 = x2+yl; y4 = y2-xl;
szR(x2,y2,x4,y4);
szR(x3,y3,x4,y4);
scanLine();
}else{
yl = y1-y2;
xl = x2-x1;
szR(x1,y1,x2,y2);
x3 = x1-yl; y3 =y1-xl;
szR(x1,y1,x3,y3);
x4 = x2-yl; y4 = y2-xl;
szR(x2,y2,x4,y4);
szR(x3,y3,x4,y4);
scanLine();
}
}
}
}
void mousePressed(){
if(first){
x1 = mouseX;
y1 = mouseY;
first= false;
done = false;
}else{
x2 = mouseX;
y2 = mouseY;
done = true;
first = true;
}
}
void scanLine(){
boolean start = true;
color x;
int x1 = -1,y1 = -1,x2 = -1,y2 = -1;
for(int i = 0; i < 480; i++){
for(int j = 0; j < 640;j++){
x = get(j,i);
if(x == color(30) && start){
x1 = j; y1 = i;
x2 = j; y2 = i;
start = false;
}else if(x == color(30) && !start){
x2 = j; y2 = i;
}
}
if(x1 >= 0){
szR(x1,y1,x2,y2);
}
x1 = -1;y1 = -1;x2 = -1;y2 = -1;
start = true;
}
}
void szR(int xa, int ya, int xb,int yb){
float x1 = (float) xa; float y1 = (float) ya;
float x2 = (float) xb; float y2 = (float) yb;
float x = x1; float y = y1;
float m;
m=(y1-y2)/(x1-x2);
if(abs(m)<=1)
{
if(x1<x2)
{
for(x=x1;x<=x2;x++)
{
point(x,y);
y=y+m;
}
}
if(x2<x1)
{
for(x=x1;x>=x2;x--)
{
point(x,y);
y=y-m;
}
}
}
if(abs(m)>1)
{
if(y1<y2)
{
for(y=y1;y<=y2;y++)
{
point(x,y);
x=x+(1/m);
}
}
if(y2<y1)
{
for(y=y1;y>=y2;y--)
{
point(x,y);
x=x-(1/m);
}
}
}
};