Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setup() {
- createCanvas(512,512);
- background(255);
- }
- var x0=-1;
- var y0=-1;
- var x1=-1;
- var y1=-1;
- function mousePressed() {
- x0=mouseX;
- y0=mouseY;
- }
- function mouseDragged() {
- x1=mouseX;
- y1=mouseY;
- background(255);
- noStroke();
- fill('red');
- ellipse(x0-3,y0-3,6);
- fill('green');
- ellipse(x1-3,y1-3,6);
- }
- function mouseReleased() {
- background(255);
- loadPixels();
- draw_line();
- updatePixels();
- }
- function set_pixel(x,y,c) {
- idx=(y*512+x)*4;
- pixels[idx]=c;
- pixels[idx+1]=c;
- pixels[idx+2]=c;
- pixels[idx+3]=255;
- }
- function draw_line() {
- dx=x1-x0;
- dy=y1-y0;
- //console.log("frm "+x0+"|"+y0+" to "+x1+"|"+y1);
- //console.log("init "+dx+" "+dy);
- mx=1;
- if((dx*dx)>(dy*dy)){
- mx=dx;
- }else{
- mx=dy;
- }
- if(mx<0)
- mx=-mx;
- dx=dx/mx;
- dy=dy/mx;
- //console.log("pprs "+dx+"|"+dy+"|"+mx);
- for(i=0;i<mx;i++){
- tmpx=floor(x0+dx*i);tmpy=floor(y0+dy*i);
- set_pixel(tmpx,tmpy,0);
- set(tmpx,tmpy,color(255,0,0));
- //console.log("prs "+tmpx+"|"+tmpy+"|"+i);
- }
- }
- //na 3
- function setup() {
- createCanvas(512,512);
- background(255);
- }
- var x0=-1;
- var y0=-1;
- var x1=-1;
- var y1=-1;
- function mousePressed() {
- x0=mouseX;
- y0=mouseY;
- }
- function mouseDragged() {
- x1=mouseX;
- y1=mouseY;
- background(255);
- noStroke();
- fill('red');
- ellipse(x0-3,y0-3,6);
- fill('green');
- ellipse(x1-3,y1-3,6);
- }
- function mouseReleased() {
- background(255);
- loadPixels();
- draw_line();
- updatePixels();
- }
- function set_pixel(x,y,c) {
- idx=(y*512+x)*4;
- pixels[idx]=c;
- pixels[idx+1]=c;
- pixels[idx+2]=c;
- pixels[idx+3]=255;
- }
- function draw_line() {
- if(x0<x1){
- sx0=x0;
- sy0=y0;
- sx1=x1;
- sy1=y1;
- }else{
- sx0=x1;
- sy0=y1;
- sx1=x0;
- sy1=y0;
- }
- a=(sx1-sx0)/(sy1-sy0);
- b=sy0-a*sx0;
- {
- for(y=0;y<512;y++){
- for(x=0;x<512;x++){
- d=((sy1-sy0)/(sx1-sx0))*(x-sx0)-(y-sy0);
- r=0;
- g=0;
- if(255<d) {g=255;}else
- if(d<-255) {r=255;}else
- if(0<d) {g=d; }else
- {r=-d; }
- set(x,y,color(r,g,0));
- }
- }
- }
- }
- //----
- function setup() {
- createCanvas(512,512);
- background(255);
- }
- var x0=-1;
- var y0=-1;
- var x1=-1;
- var y1=-1;
- function mousePressed() {
- x0=mouseX;
- y0=mouseY;
- }
- function mouseDragged() {
- x1=mouseX;
- y1=mouseY;
- background(255);
- noStroke();
- fill('red');
- ellipse(x0-3,y0-3,6);
- fill('green');
- ellipse(x1-3,y1-3,6);
- }
- function mouseReleased() {
- background(255);
- loadPixels();
- draw_line();
- updatePixels();
- }
- function set_pixel(x,y,c) {
- idx=(y*512+x)*4;
- pixels[idx]=c;
- pixels[idx+1]=c;
- pixels[idx+2]=c;
- pixels[idx+3]=255;
- }
- function draw_line() {
- //noprotect
- if(x0<x1){
- sx0=x0;
- sy0=y0;
- sx1=x1;
- sy1=y1;
- }else{
- sx0=x1;
- sy0=y1;
- sx1=x0;
- sy1=y0;
- }
- a=(sx1-sx0)/(sy1-sy0);
- b=sy0-a*sx0;
- {
- for(y=0;y<512;y++){
- for(x=0;x<512;x++){
- d=2*(sy1-sy0)*(x-sx0)-2*(sx1-sx0)*(y-sy0);
- r=0;
- g=0;
- d=floor(d);
- if(d!=0)
- if(0<d) {g=255; }else
- {r=255; }
- set(x,y,color(r,g,0));
- }
- }
- }
- }
- //na 3.5
- function setup() {
- createCanvas(512,512);
- background(255);
- }
- var x0=-1;
- var y0=-1;
- var x1=-1;
- var y1=-1;
- function mousePressed() {
- x0=mouseX;
- y0=mouseY;
- }
- function mouseDragged() {
- x1=mouseX;
- y1=mouseY;
- background(255);
- noStroke();
- fill('red');
- ellipse(x0-3,y0-3,6);
- fill('green');
- ellipse(x1-3,y1-3,6);
- }
- function mouseReleased() {
- background(255);
- loadPixels();
- draw_line();
- updatePixels();
- }
- function set_pixel(x,y,c) {
- idx=(y*512+x)*4;
- pixels[idx]=c;
- pixels[idx+1]=c;
- pixels[idx+2]=c;
- pixels[idx+3]=255;
- }
- function draw_line() {
- //noprotect
- if(x0<x1){
- sx0=x0;
- sy0=y0;
- sx1=x1;
- sy1=y1;
- }else{
- sx0=x1;
- sy0=y1;
- sx1=x0;
- sy1=y0;
- }
- {
- y=sy0;
- chg=1/2;
- if(sy0>sy1)chg=-chg;
- for(x=sx0;x<sx1;x++){
- dx=(sx1-sx0)
- dy=(sy1-sy0);
- // Dp=2*dy-dx;
- // Deq=2*dy;
- // Dinc=2*dy-2*dx;
- D=floor(2*dy*(x-x0)-2*dx*(y+chg-y0));
- if(0<chg){
- if(0<D)
- y=y+1;
- }else{
- if(0>D)
- y=y-1;
- }
- set(x,y,color(0));
- }
- }
- }
- //na 4
- function setup() {
- createCanvas(512,512);
- background(255);
- }
- var last_x=-1;
- var last_y=-1;
- function mouseDragged() {
- if(mouseButton != LEFT) return;
- if(last_x>0) {
- line(last_x,last_y,mouseX,mouseY);
- }
- last_x=mouseX;
- last_y=mouseY;
- }
- function mouseReleased() {
- last_x=last_y=-1;
- if(mouseButton == RIGHT) {
- loadPixels();
- flood_fill(mouseX,mouseY);
- updatePixels();
- }
- }
- function set_pixel(x,y,c) {
- idx=(y*512+x)*4;
- pixels[idx]=c;
- pixels[idx+1]=c;
- pixels[idx+2]=c;
- pixels[idx+3]=255;
- }
- function get_pixel(x,y) {
- idx=(y*512+x)*4;
- return pixels[idx];
- }
- //właściwa funkcja do wypełniania
- function flood_fill(X,Y) {
- stack=[];
- stack.push([X,Y]);
- cnt=100;
- //[X,Y]=stack.pop();
- while(stack.length>0&&cnt>0){
- cnt--;
- [x,y]=stack.pop();// zdejmij ze stosu ostatnie współrzędne
- if((0<=x&&x<=512)&&(0<=y&&y<=512)){// sprawdź poprawność współrzędnych
- // jeśli współrzędne nie mieszczą się na ekranie, zrób continue
- clr=get_pixel(x,y);
- console.log(x+" "+y+" "+clr);
- if(false){ // pobierz kolor bieżącego piksela metodą get_pixel
- // jeśli kolor ten nie jest biały (czyli taki, jaki możemy zamalowywać), zrób continue
- // zamaluj bieżący piksel (najlepiej jakimś kolorem innym niż czarny, np. 200)
- // dodaj do stosu sąsiadów bieżącego piksela
- // czyli piksel powyżej (x,y-1), poniżej (x,y+1), z lewej (x-1,y) i z prawej (x+1,y)
- }
- }
- }
- }
- //na 4.5
- function setup() {
- createCanvas(512,512);
- background(255);
- }
- var last_x=-1;
- var last_y=-1;
- function mouseDragged() {
- if(mouseButton != LEFT) return;
- if(last_x>0) {
- line(last_x,last_y,mouseX,mouseY);
- }
- last_x=mouseX;
- last_y=mouseY;
- }
- function mouseReleased() {
- last_x=last_y=-1;
- if(mouseButton == RIGHT) {
- loadPixels();
- flood_fill(mouseX,mouseY);
- updatePixels();
- }
- }
- function get_pixel(x,y) {
- idx=(y*1024+x)*4;
- return pixels[idx];
- }
- //właściwa funkcja do wypełniania
- function flood_fill(X,Y) {
- stack=[];
- stack.push([X,Y]);
- cnt=1000000;
- //[X,Y]=stack.pop();
- while(stack.length>0&&cnt>0){
- cnt--;
- [x,y]=stack.pop();// zdejmij ze stosu ostatnie współrzędne
- if((0<=x&&x<=512)&&(0<=y&&y<=512)){// sprawdź poprawność współrzędnych
- // jeśli współrzędne nie mieszczą się na ekranie, zrób continue
- clr=get_pixel(x*2,y*2); // pobierz kolor bieżącego piksela metodą get_pixel
- // if(clr!=255)
- // clr=get_pixel(x*2+1,y*2);
- // if(clr!=255)
- // clr=get_pixel(x*2,y*2+1);
- // if(clr!=255)
- // clr=get_pixel(x*2+1,y*2+1);
- //console.log(x+" "+y+" "+clr);
- if(clr==255){ // jeśli kolor ten nie jest biały (czyli taki, jaki możemy zamalowywać), zrób continue
- set(x, y, color(128)); // zamaluj bieżący piksel (najlepiej jakimś kolorem innym niż czarny, np. 200)
- stack.push([x-1,y]); // dodaj do stosu sąsiadów bieżącego piksela
- stack.push([x+1,y]); // czyli piksel powyżej (x,y-1), poniżej (x,y+1), z lewej (x-1,y) i z prawej (x+1,y)
- stack.push([x,y-1]);
- stack.push([x,y+1]);
- stack.push([x-1,y-1]);
- stack.push([x+1,y-1]);
- stack.push([x-1,y+1]);
- stack.push([x+1,y+1]);
- }
- }
- }
- }
- //na 5
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement