FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   help! with if(){----}
« Previous topic | Next topic »

Pages: 1 2 
   Author  Topic: help! with if(){----}  (Read 1600 times)
proce55ing23

WWW Email
help! with if(){----}
« on: Jun 27th, 2003, 2:25am »

me and my friend are working on an applet in which each key does something different. A makes a rectangle over mouse, B makes a circle under mouse,C makes a triangle left of the mouse and so on and so forth getting more complicated as they go. An example of the code is shown here:
void setup(){
---
---
---
---
}
void loop(){
if(key=='a'||key=='A'){
rect(coordinates);
}
if(key=='b'||key=='B'){
ellipse(coordinates);
}
if(key=='c'||key=='C'){
triangle(coordinates);
}
//So on...
}
 
 
does anybody know how to make it so that if we hold down the A and C keys, it will show the rect and the triangle only, and if we hold down the B and C keys it will make the circle and the triangle, and so on and so forth...
somebody please help...
thank you...
David
 
arielm

WWW
Re: help! with if(){----}
« Reply #1 on: Jun 27th, 2003, 12:34pm »

hi david,
 
try the following:
 
Code:

boolean AisPressed;
boolean BisPressed;
boolean CisPressed;
 
void setup()
{
  size(300, 300);
 
  // not really need, because a boolean is always "false" by default...
  AisPressed = false;
  BisPressed = false;
  CisPressed = false;
}
 
void keyPressed()
{
  if (key == 'a' || key == 'A')
  {
    AisPressed = true;
  }
 
  if (key == 'b' || key == 'B')
  {
    BisPressed = true;
  }
 
  if (key == 'c' || key == 'C')
  {
    CisPressed = true;
  }
}
 
void keyReleased()
{
  if (key == 'a' || key == 'A')
  {
    AisPressed = false;
  }
 
  if (key == 'b' || key == 'B')
  {
    BisPressed = false;
  }
 
  if (key == 'c' || key == 'C')
  {
    CisPressed = false;
  }
}
 
void loop()
{
  if (AisPressed)
  {
    rect(100, 80, 30, 40);
  }
 
  if (BisPressed)
  {
    ellipse(200, 130, 40, 40);
  }
 
  if (CisPressed)
  {
    triangle(100, 100, 100, 200, 200, 200);
  }
}

 
hope it helps,
 
ariel (you have a special authorization to call me bagel papa poule...)
 

Ariel Malka | www.chronotext.org
benelek

35160983516098 WWW Email
Re: help! with if(){----}
« Reply #2 on: Jun 27th, 2003, 2:34pm »

mm, good thinking monsigor bagel papa poule (hehe), but it can be done that way with an array, saving huge amounts of code space (and making for fun practice with numeric letters ) :
 
Code:

boolean[] alphabet = new boolean[26];
int numa=int('a'); //97
int numz=int('z'); //122
int numA=int('A'); //65
int numZ=int('Z'); //90
 
void setup()  
{  
  size(300, 300);  
 
  for(int i=0; i<alphabet.length; i++) {
    alphabet[i]=false;
  }
}  
 
void keyPressed() {  
 
  int index=0;
  int bigLetterNum=numA;
  for(int i=numa; i<=numz; i++) {
    int theLetter=int(key);
    if (i==theLetter || bigLetterNum==theLetter) {  
 alphabet[index] = true;  
    }  
    index++;
    bigLetterNum++;
  }
   
}  
 
void keyReleased() {  
 
  int index=0;
  int bigLetterNum=numA;
  for(int i=numa; i<=numz; i++) {
    int theLetter=int(key);
    if (i==theLetter || bigLetterNum==theLetter) {  
 alphabet[index] = false;  
    }  
    index++;
    bigLetterNum++;
  }
   
}  
 
 
void loop() {  
 
  if (alphabet[0]==true) //a  
  {  
    rect(100, 80, 30, 40);  
  }  
 
  if (alphabet[1]==true) //b
  {  
    ellipse(200, 130, 40, 40);  
  }  
 
  if (alphabet[2]==true) //c  
  {  
    triangle(100, 100, 100, 200, 200, 200);  
  }  
   
}  
 
 
arielm

WWW
Re: help! with if(){----}
« Reply #3 on: Jun 27th, 2003, 2:47pm »

well, cher monsieur jacob, the goal was to build a very simple piece of code, simplified as possible and totally focused on the question asked by david...
 
but if you want to run a contest of the best obfuscated, self-modifying, anamorphic, genetically algorithmed piece of code, then we could fix an opening date
 

Ariel Malka | www.chronotext.org
benelek

35160983516098 WWW Email
Re: help! with if(){----}
« Reply #4 on: Jun 27th, 2003, 3:11pm »

hehe, you think you would be up to the challenge do ya?
 
arielm

WWW
Re: help! with if(){----}
« Reply #5 on: Jun 27th, 2003, 3:49pm »

okay, i propose the following scenario:
 
1) consider a 3x3 grid centered on screen (must be visible, made with thin lines)
2) each cell can be either "on or offed" by one of the keys between 1 and 9
3) note that it should be possible to press more than one key at a time (to stick with david's problem)
4) for each cell:
- "on" means to have the corresponding cell number drawn inside it (not using fonts, but rather made of little crosses).
- "off" means the same, but instead of crosses, let's use circles
 
if the "crosses" thing was not clear, here is "2" made of crosses:
 
Code:
xxx
  x
xxx
x
xxx

 
the name of the game is to produce the *shortest* piece of standalone code (i.e. importing bitmaps is forbidden) for this scenario!
 
if you accept the rules, let's say that the results should be posted here, but not before, say, sunday, 12:00 AM, GMT time...
 
(the goal is to let everyone complete his piece of code without being influenced by the other's)
 
 
and everybody is invited to the party of course!
« Last Edit: Jun 27th, 2003, 3:54pm by arielm »  

Ariel Malka | www.chronotext.org
fry


WWW
Re: help! with if(){----}
« Reply #6 on: Jun 27th, 2003, 9:35pm »

a simpler way to do this.. (the original question, not this genetomorpho saber rattling)
 
Code:
boolean alphabet[] = new boolean[26];
 
int letter(int key) {
  if ((key >= 'a') && (key <= 'z')) {
    return key - 'a';
 
  } else if ((key >= 'A') && (key <= 'Z')) {
    return key - 'A';
 
  } else {
    return -1;
  }
}
 
void keyPressed() {
  int which = letter(key);  
  if (which != -1) alphabet[which] = true;
}
 
void keyReleased() {
  int which = letter(key);  
  if (which != -1) alphabet[which] = false;
}
 
flight404

WWW Email
Re: help! with if(){----}
« Reply #7 on: Jun 27th, 2003, 10:08pm »

To Arielm and Benelek...you are both pretty.
 
 
proce55ing23

WWW Email
Re: help! with if(){----}
« Reply #8 on: Jun 27th, 2003, 11:13pm »

thank you for all of your great suggestions but I need all of the code suggestions by a deadline of 8 Am Eastern time on Saturday the 28th... Thank you for all of your wonderful suggestions...
 
proce55ing23
 
PS As an extra challenge, try to do it without using a boolean and try to do it as simply as you can.
 
arielm

WWW
Re: help! with if(){----}
« Reply #9 on: Jun 28th, 2003, 12:12am »

on Jun 27th, 2003, 11:13pm, proce55ing23 wrote:
thank you for all of your great suggestions but I need all of the code suggestions by a deadline of 8 Am Eastern time on Saturday the 28th... Thank you for all of your wonderful suggestions...
 
proce55ing23
 
PS As an extra challenge, try to do it without using a boolean and try to do it as simply as you can.

 
i don't know if it's me, the late hour here in tel-aviv or too much nicotine today, but this last post sounds totally surealistic too me!
 
suddenly, i had this vision that proce55ing23 alias david was in fact ben fry that is using pseudonyms to test the level of activity of the processing community
 
 
cheers to flight404, benelek, fry and proce55ing23, whoever you are...
 
 
p.s: the original "contest" rules are still valid for me, unless benelek has something to add on the subject...
 

Ariel Malka | www.chronotext.org
arielm

WWW
Re: help! with if(){----}
« Reply #10 on: Jun 28th, 2003, 2:22am »

hmm... there might be some problems (on windows at least) when pressing more than 3 or 4 keys together (depending on the combinations): the computer is starting to sound beeps and keyPressed() / keyReleased is not functionning as intended then!
 
so either someone has a clue about that or we may just ignore this problem for now
 
guten nacht...
 

Ariel Malka | www.chronotext.org
benelek

35160983516098 WWW Email
Re: help! with if(){----}
« Reply #11 on: Jun 28th, 2003, 6:48am »

lol, sounds fair. let's ignore the little problem arielm described.
 
by the shortest code, we mean the fewest number of characters in the code, not counting a single line designated for the designer's signature.
 
there has to be a thin-line border around each number, and the numbers have to be laid out from top to bottom, left to right. ie:
 
Code:
1 2 3
4 5 6
7 8 9

 
post as soon as possible after 12am sunday EST.
 
extra cudos to those pieces of code that flight404 deems as extra-pretty (the code itself, not the compiled applet). you've got to link to the code in the sketch's .pde file posted somewhere for downloading, and you have to link to the running applet posted on some website for viewing.
 
just to clarify things, the applet has to be able to run correctly on at least one version of one well-known operating system
 
fry


WWW
Re: help! with if(){----}
« Reply #12 on: Jun 28th, 2003, 1:08pm »

doing this without a boolean, as suggested by proce55ing23..
 
uses a single int with packed bits instead of an array of 26 booleans.
 
Code:
int alphabet;
 
int letter(int key) {  
  if ((key >= 'a') && (key <= 'z')) {  
    return 1 << (key - 'a');  
 
  } else if ((key >= 'A') && (key <= 'Z')) {  
    return 1 << (key - 'A');  
  }  
  return (1 << 26); // ignored
}
 
void keyPressed() {  
  alphabet |= letter(key);
}  
 
void keyReleased() {  
  alphabet = alphabet & (~letter(key));
}

 
then draw looks something like:
Code:
void draw() {
  if ((alphabet & (1<<0)) != 0) {  // a
    // something fancy
  }
  if ((alphabet & (1<<1)) != 0) {  // b
    // something fancier
  }
  if ((alphabet & (1<<2)) != 0) {  // c
    // you get the idea
  }
 
  // or a second method that may be easier to read..
  if ((alphabet & letter('d')) != 0) {
  }
}
 
fry


WWW
Re: help! with if(){----}
« Reply #13 on: Jun 28th, 2003, 1:14pm »

on Jun 28th, 2003, 12:12am, arielm wrote:
suddenly, i had this vision that proce55ing23 alias david was in fact ben fry that is using pseudonyms to test the level of activity of the processing community

indeed, there are times when i carouse about the bboard feigning to be a 10-year-old programmer, reliving the heady days of my youth when i was trying to figure out programming myself.
 
however, this wasn't one of those times.
 
benelek

35160983516098 WWW Email
Re: help! with if(){----}
« Reply #14 on: Jun 28th, 2003, 2:44pm »

that packed bits method is excelent, Fry. very clever
 
Pages: 1 2 

« Previous topic | Next topic »