We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › 2 Basic Programs
Page Index Toggle Pages: 1
2 Basic Programs (Read 760 times)
2 Basic Programs
May 3rd, 2009, 3:19pm
 
Hiya guys, I am currently working on 2 basic sketches, I say basic but for sure I'm not able to implement them Cry
My buddies keep saying it is easy but I wish they would stop it already as I'm new to this completely.
Any help appreciated



Re: 2 Basic Programs
Reply #1 - May 3rd, 2009, 3:27pm
 
try to upload it somewhere. there are severel easy file hoster like http://www.file-upload.net/
Re: 2 Basic Programs
Reply #2 - May 4th, 2009, 2:27am
 
I wouldn't be keen on downloading and running some random exe file...
It might be more useful to explain what you are trying to do, and perhaps provide some images.
Re: 2 Basic Programs
Reply #3 - May 4th, 2009, 5:40am
 
I guess hes right. Dont trust anybody Smiley
Some screenshots and a good explanation will maybe do the job
Re: 2 Basic Programs
Reply #4 - May 7th, 2009, 5:21pm
 
hey y'all I have miraculously been able to get some of my coding done for one of these programs so was briefly shouting yipee, but that soon changed to god darn it!  Angry I have become stuck so would like to see if any of y'all can help.
I will paste in my code & you will see a sketch producing circles with random colours. I want to add a mouse pressed event, so that a circle disapears wherever I click the mouse. Is it something to do with boolean/mouse X mouse Y? I really suck at this!

int Rank = 12;
int SqSide = 20;
int Side = SqSide * Rank;
color White = color(255, 255, 255),
     Red = color(255, 0, 0),
     Green = color(0, 255, 0),
     Blue = color(0, 0, 255),
     Yellow = color(255, 255, 0),
     Black = color(0, 0, 0);
color BG = Yellow;

void ranCol() {
 float co;  
 co = random(5);
 int n;
 n = (int) co;
 if (n == 0) {
  fill (Green);
 } else {
     if (n == 1) {
       fill (Blue);
     } else {
       if (n == 2) {
         fill (Red);
       } else {
         if (n == 3) {
           fill (Yellow);
         } else {
           if (n == 4) {
             fill (White);
           } else {
             if (n == 5) {
               fill (Black);
        }
      }
     }
     }
     }
}
 
}

//color RandomColor() {
// color Result;
 
 //Result = color((int) random(256), (int) random(256), (int) random(256));
// return(Result);
//}

void setup() {
 size(Side, Side);
 smooth();
 background(BG);  
}

void draw() {

 int a, b;
 float x,
       y;
 int L,
     T;
 int u,
     v;
 x = random(Side)/SqSide;
 y = random(Side)/SqSide;
 L = (int) x;
 T = (int) y;
 u = SqSide * L;
 v = SqSide * T;

 for (a=0; a< Rank;a++) {
  for ( b=0;  b<Rank;b++) {
//fill(RandomColor());
ranCol();
ellipse(a * SqSide + SqSide/2, b*SqSide + SqSide /2, SqSide, SqSide);  
}
 }
}
Re: 2 Basic Programs
Reply #5 - May 7th, 2009, 6:03pm
 
the best way to do this would be with classes. I try to work on your code.
it already took me a while to understand what you didt and i still dont understand your randomcolors but i keep them just the way they are.
Just curious. what is this? homework to rebuild these programms ?
Re: 2 Basic Programs
Reply #6 - May 7th, 2009, 7:14pm
 
alright, here it is. You really should take a look at what ive done. and maybe check the OOP tutorial.
there are also some little things you should change about your programming language like naming variables ( use camelCase)
and your for loops for example.

anyway, here it is:



int Rank = 12;
int SqSide = 20;
Circles[] circle = new Circles[Rank*Rank];
int Side = SqSide * Rank;
color White = color(255, 255, 255),
Red = color(255, 0, 0),
Green = color(0, 255, 0),
Blue = color(0, 0, 255),
Yellow = color(255, 255, 0),
Black = color(0, 0, 0);
color BG = Yellow;


void setup() {
 size(Side, Side);
 smooth();
 background(BG);  
 for (int i=0; i< circle.length;i++) {
   circle[i] = new Circles();
 }
}

void draw() {

 background(BG);

 for (int i=0; i< Rank;i++) {
   for (int j=0; j< Rank;j++) {
     circle[i+(j*Rank)].check();
     circle[i+(j*Rank)].show(i * SqSide + SqSide/2, j*SqSide + SqSide /2);


   }
 }

}

class Circles{
 float x;
 float y;
 boolean circleON;

 Circles()  {
   circleON = true;
 }

 void ranCol() {


 }

 void check(){
   if (mousePressed == true) {

     if(dist(mouseX,mouseY,x,y)<=SqSide/2)circleON =  false;
   }
 }
 void show(float _x, float _y){
   x= _x;
   y= _y;

   float co;  
   co = random(5);
   int n;
   n = (int) co;
   if (n == 0) {
     fill (Green);
   }
   else {
     if (n == 1) {
       fill (Blue);
     }
     else {
       if (n == 2) {
         fill (Red);
       }
       else {
         if (n == 3) {
           fill (Yellow);
         }
         else {
           if (n == 4) {
             fill (White);
           }
           else {
             if (n == 5) {
               fill (Black);
             }
           }
         }
       }
     }
   }


   if(circleON){

     ellipse(x,y,SqSide,SqSide);
   }
 }
}




Re: 2 Basic Programs
Reply #7 - May 8th, 2009, 1:15am
 
Two remarks about the cascading ifs:

- You can write:

if (n == 0) {
 fill(Green);
} else if (n == 1) {
 fill(Blue);
} else if (n == 2) {


and so on. Much simpler and only one brace to close at the end!
(some languages even introduce the elseif keyword!).

- In such case, it might be simpler and slightly more efficient to use switch().
Re: 2 Basic Programs
Reply #8 - May 8th, 2009, 5:03am
 
Wouldn't the cascading ifs be even clearer as a switch statement?
(Oops! just noticed PhiLho suggested this!  Here's the code anyway...)

Code:
n = (int) co;
switch(n) {
 case 0:
    fill (Green);
   break;
 case 1:
    fill (Blue);
   break;
 case 2:
    fill (Red);
   break;
 case 3:
    fill (Yellow);
   break;
 case 4:
    fill (White);
   break;
 default:
    fill (Black);
   break;
}
Re: 2 Basic Programs
Reply #9 - May 8th, 2009, 10:51am
 
oh my god this is so awesome!  Grin I can't wait until the day I'm as good as you are! I kinda understand the additional coding too, very logical, thanks cedric, u rock!
To answer your question about what this is for? It is an optional exercise after class to help us to start to think like programmers.
Although it's optional, it won't shed you in a good light if you don't do it, after all why'd you take the class if not to learn!
Thanks to philho for the reccomendation, good point, much neater!
In fact, thanks to all you guys (listen to me, like I've just won an Oscar!)
Re: 2 Basic Programs
Reply #10 - May 8th, 2009, 11:03am
 
I made some aditional changes by skipping all this if and else or switches and simply added a color array where you randomly pick one of the colors. Much shorter and easy to add more colors if you want...


Code:
int Rank = 12;
int SqSide = 20;
Circles[] circle = new Circles[Rank*Rank];
int Side = SqSide * Rank;
color[] colors = {#ff0000,#ffff00,#000000,#00ff00,#0000ff,#ffffff};

void setup() {
size(Side, Side);
smooth();
background(#ffff00);  
for (int i=0; i< circle.length;i++) {
  circle[i] = new Circles();
}
}

void draw() {

background(#ffff00);
for (int i=0; i< Rank;i++) {
  for (int j=0; j< Rank;j++) {
    circle[i+(j*Rank)].check();
    circle[i+(j*Rank)].show(i * SqSide + SqSide/2, j*SqSide + SqSide /2);
 }
}
}

class Circles{
float x;
float y;
boolean circleON;

Circles()  {
  circleON = true;
}


void check(){
  if (mousePressed == true) {

    if(dist(mouseX,mouseY,x,y)<=SqSide/2)circleON =  false;
  }
}
void show(float _x, float _y){
  x= _x;
  y= _y;

  fill(colors[int(random((colors.length)))]);

  if(circleON){

    ellipse(x,y,SqSide,SqSide);
  }
}
}
Page Index Toggle Pages: 1