Wednesday, April 1, 2015

Connect the Dots

On March 31, 2015 I had an assignment due where I was instructed to creates a series of dots that when connected would cause the picture to pop up. Although my teacher showed the class a different way to solve this problem, I decided to use classes because I felt this would be easier. I'm not sure if my hypothesis about it being easier was true but I did enjoy learning about classes because we are going to be using classes to create codes later on in the year. Below is my  code but thereare some bugs in the code so it's possible to make the house pop up without connecting all of the dots.

color colorD = color(0);
color colorO = color(0);
color colorClick = color(0);


Dot[] dots = new Dot[31];
Line[] lines = new Line[31];

// when no dot is being touched
int selDot=100, staDot = 100, endDot = 100;


int totalL = 0;


boolean draw;
boolean solve;

PImage photo;

color aColor = color(252, 13, 5);

void setup() {
 size(600, 600);
 photo = loadImage("HOUSE.png");


 draw=false;
 solve=false;

 // 31 dots
 dots[0] = new Dot(292, 126);
 dots[1] = new Dot(326, 157);
 dots[2] = new Dot(362, 193);
 dots[3] = new Dot(402, 232);
 dots[4] = new Dot(437, 268);
 dots[5] = new Dot(412, 273);
 dots[6] = new Dot(413, 310);
 dots[7] = new Dot(413, 362);
 dots[8] = new Dot(413, 397);
 dots[9] = new Dot(412, 421);
 dots[10] = new Dot(376, 421);
 dots[11] = new Dot(337, 422);
 dots[12] = new Dot(301, 422);
 dots[13] = new Dot(272, 421);
 dots[14] = new Dot(240, 421);
 dots[15] = new Dot(207, 422);
 dots[16] = new Dot(176, 421);
 dots[17] = new Dot(175, 397);
 dots[18] = new Dot(175, 361);
 dots[19] = new Dot(175, 311);
 dots[20] = new Dot(175, 273);
 dots[21] = new Dot(151, 269);
 dots[22] = new Dot(175, 239);
 dots[23] = new Dot(198, 217);
 dots[24] = new Dot(198, 188);
 dots[25] = new Dot(196, 155);
 dots[26] = new Dot(196, 133);
 dots[27] = new Dot(221, 131);
 dots[28] = new Dot(252, 131);
 dots[29] = new Dot(252, 162);
 dots[30] = new Dot(268, 147);



 for (int i=0; i<31; i++) {
   dots[i].c = colorD;
   dots[i].display(dots[i].c);
 }
 // 32 lines
 lines[0] = new Line(0, 1);
 lines[1] = new Line(0, 30);
 lines[2] = new Line(1, 2);
 lines[3] = new Line(2, 3);
 lines[4] = new Line(3, 4);
 lines[5] = new Line(4, 5);
 lines[6] = new Line(5, 6);
 lines[7] = new Line(6, 7);
 lines[8] = new Line(7, 8);
 lines[9] = new Line(8, 9);
 lines[10] = new Line(9, 10);
 lines[11] = new Line(10, 11);
 lines[12] = new Line(11, 12);
 lines[13] = new Line(12, 13);
 lines[14] = new Line(13, 14);
 lines[15] = new Line(14, 15);
 lines[16] = new Line(15, 16);
 lines[17] = new Line(16, 17);
 lines[18] = new Line(17, 18);
 lines[19] = new Line(18, 19);
 lines[20] = new Line(19, 20);
 lines[21] = new Line(20, 21);
 lines[22] = new Line(21, 22);
 lines[23] = new Line(22, 23);
 lines[24] = new Line(23, 24);
 lines[25] = new Line(24, 25);
 lines[26] = new Line(25, 26);
 lines[27] = new Line(26, 27);
 lines[28] = new Line(27, 28);
 lines[29] = new Line(28, 29);
 lines[30] = new Line(29, 30);
}

void draw() {

 background(aColor);

 for (int i=0; i< 31; i++) {
   dots[i].display(dots[i].c);
   text(i, dots[i].x + 10, dots[i].y);
 }

 // draw dots
 for (int i=0; i<31; i++) {
   dots[i].display(dots[i].c);
 }

 // draw  lines
 for (int i=0; i<31; i++) {
   if (lines[i].show==true) {
     lines[i].display();
   }
 }




 if (draw==true) {  
   line(dots[selDot].x, dots[selDot].y, mouseX, mouseY);
 }
}


void mouseReleased() {
 if (solve==false) {

   totalL++;


   if (selDot!=100) {
     dots[selDot].c=colorD;
   }


   draw=false;
   for (int i=0; i<31; i++) {
     if ((dots[i].touched()==true) && (i!=staDot)) {
       draw=true;
       dots[i].c=colorClick;
       selDot=i;
       break;
     }
   }

   if (draw==true) {
     if (staDot==100) {
       staDot=selDot;
     } else if (endDot==100) {
       endDot=selDot;
       drawLine(staDot, endDot);
       if (totalL==33) {
         checkResult();
       }
     }
     staDot=selDot;
     endDot=100;
   } else {
     staDot=100;
     endDot=100;
   }
 }
}

void mouseMoved() {
 if (solve==false) {
   for (int i=0; i<31; i++) {
     if (selDot==i) {
     } else
       if (dots[i].touched()==true) {
       dots[i].c=colorO;
     } else {
       dots[i].c=colorD;
     }
   }
 }
}



class Dot {
 int x; // dots x POSITION
 int y; // dots y POSITION
 int s; // dots SIZE
 color c; // dots COLOR
 boolean touched;

 Dot(int localX, int localY) {
   x = localX;
   y = localY;
   s = 10;
   c = color(#0DC0FA);
 }

 void display(int colorD) {
   fill(255, 235, 3);
   ellipse(x, y, s, s);
 }

 boolean touched() {

   float disX = x - mouseX;
   float disY = y - mouseY;

   if (sqrt(sq(disX) + sq(disY)) < s/2 ) {
     return true;
   } else {
     return false;
   }
 }
}

class Line {
 int beginX, beginY;
 int endX, endY;
 boolean show;

 Line(int staDot, int endDot) {
   beginX=dots[staDot].x;
   beginY=dots[staDot].y;
   endX=dots[endDot].x;
   endY=dots[endDot].y;
   show=false;
 }

 void display() {
   strokeWeight(5);
   stroke(#0DC0FA);
   line(beginX, beginY, endX, endY);
 }
}

void drawLine(int staDot, int endDot) {


 // can not be anything from from 0 to 30 */
 int a = 35;
 int b = 35;



 if (staDot < endDot) {
   a = staDot;
   b = endDot;
 } else if (staDot > endDot) {
   a = endDot;
   b = staDot;
 } else {
   draw=false;
 }

 if (draw==true) {
   if ((a==0)&&(b==1)) {
     lines[0].show=true;
   }
   if ((a==0)&&(b==30)) {
     lines[1].show=true;
   }
   if ((a==1)&&(b==2)) {
     lines[2].show=true;
   }
   if ((a==2)&&(b==3)) {
     lines[3].show=true;
   }
   if ((a==3)&&(b==4)) {
     lines[4].show=true;
   }
   if ((a==4)&&(b==5)) {
     lines[5].show=true;
   }
   if ((a==5)&&(b==6)) {
     lines[6].show=true;
   }
   if ((a==6)&&(b==7)) {
     lines[7].show=true;
   }
   if ((a==7)&&(b==8)) {
     lines[8].show=true;
   }
   if ((a==8)&&(b==9)) {
     lines[9].show=true;
   }
   if ((a==9)&&(b==10)) {
     lines[10].show=true;
   }
   if ((a==10)&&(b==11)) {
     lines[11].show=true;
   }
   if ((a==11)&&(b==12)) {
     lines[12].show=true;
   }
   if ((a==12)&&(b==13)) {
     lines[13].show=true;
   }
   if ((a==13)&&(b==14)) {
     lines[14].show=true;
   }
   if ((a==14)&&(b==15)) {
     lines[15].show=true;
   }
   if ((a==15)&&(b==16)) {
     lines[16].show=true;
   }
   if ((a==16)&&(b==17)) {
     lines[17].show=true;
   }
   if ((a==17)&&(b==18)) {
     lines[18].show=true;
   }
   if ((a==18)&&(b==19)) {
     lines[19].show=true;
   }
   if ((a==19)&&(b==20)) {
     lines[20].show=true;
   }
   if ((a==20)&&(b==21)) {
     lines[21].show=true;
   }
   if ((a==21)&&(b==22)) {
     lines[22].show=true;
   }
   if ((a==22)&&(b==23)) {
     lines[23].show=true;
   }
   if ((a==23)&&(b==24)) {
     lines[24].show=true;
   }
   if ((a==24)&&(b==25)) {
     lines[25].show=true;
   }
   if ((a==25)&&(b==26)) {
     lines[26].show=true;
   }
   if ((a==26)&&(b==27)) {
     lines[27].show=true;
   }
   if ((a==27)&&(b==28)) {
     lines[28].show=true;
   }
   if ((a==28)&&(b==29)) {
     lines[29].show=true;
   }
   if ((a==29)&&(b==30)) {
     lines[30].show=true;
   }
 }
 if ((a==0)&&(b==30)) {
   lines[30].show=true;
   photo = loadImage("HOUSE.png");
   image(photo, 150, 130);
   frameRate(0);
 }
}

void checkResult() {
 int amountLine=0;
 for (int i=0; i<31; i++) {
   if (lines[i].show==true) {
     amountLine++;
   }
 }
 if (amountLine==31) {
   for (int i=0; i<31; i++) {
   }
 }
}











No comments:

Post a Comment