if you are bored and want a simple coding challenge. #74

Closed
opened 2019-04-03 17:38:31 +01:00 by kieran-boyle · 3 comments
kieran-boyle commented 2019-04-03 17:38:31 +01:00 (Migrated from github.com)

Right so i have got the logic down for what i want to achieve, and it works beautifully.
Only issue is juicy nested if statement.
Any ideas on pruning this down?
Absolutely not a problem if you cant be arsed though hahaha.

static int pos1,pos2,pos3,pos4,posR,shapeNumber;
int numberOfShapes = 4;
EVERY_N_MILLISECONDS(1000){
	shapeNumber=random8(0,2);//square or circle +1 cus ? but works.
	colorIndex=random8(1,255);//color of shape
	pos1=random8(1,MATRIX_WIDTH);//starting x coord
	pos2=random8(2,MATRIX_HEIGHT);//starting y coord
	pos3=random8(1,MATRIX_WIDTH);//ending x coord
	pos4=random8(2,MATRIX_HEIGHT);//ending y coord
	posR=random8(1,(MATRIX_HEIGHT));//radius for circle
	if (shapeNumber==0){//purpose i want there to be at least 1 line of blank pixles in each rectangle
//draw, so i check if the difference in coordinates allows this, and if not edit to do so.
		if (pos1>=pos3){
			if((pos1-pos3)<2){
				if ((pos1-pos3)==1){
					pos1=pos1+1;
				}
				else{
					pos1=pos1+2;
				}	
				
			}
			
		}
		if (pos3>=pos1){
			if((pos3-pos1)<2){
				if ((pos3-pos1)==1){
					pos3=pos3+1;
				}
				else{
					pos3=pos3+2;
				}	
				
			}
			
		}
		if (pos2>=pos4){
			if((pos2-pos4)<2){
				if ((pos2-pos4)==1){
					pos2=pos2+1;
				}
				else{
					pos2=pos2+2;
				}	
				
			}
			
		}
		if (pos4>=pos2){
			if((pos4-pos2)<2){
				if ((pos4-pos2)==1){
					pos4=pos4+1;
				}
				else{
					pos4=pos4+2;
				}	
				
			}
			
		}
	}
	switch (shapeNumber){//draw the shape.
		case 0:
			matrix.DrawRectangle(pos1,pos2,pos3,pos4,ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending));
			break;
		case 1:
			matrix.DrawCircle(pos2,pos2,posR,ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending));  
			break;
	}
}
EVERY_N_SECONDS(14){//wipe the screen
	turnOff();
	setLEDs();
}
FastLED.show();   
FastLED.delay(20);

}

Right so i have got the logic down for what i want to achieve, and it works beautifully. Only issue is juicy nested if statement. Any ideas on pruning this down? Absolutely not a problem if you cant be arsed though hahaha. static int pos1,pos2,pos3,pos4,posR,shapeNumber; int numberOfShapes = 4; EVERY_N_MILLISECONDS(1000){ shapeNumber=random8(0,2);//square or circle +1 cus ? but works. colorIndex=random8(1,255);//color of shape pos1=random8(1,MATRIX_WIDTH);//starting x coord pos2=random8(2,MATRIX_HEIGHT);//starting y coord pos3=random8(1,MATRIX_WIDTH);//ending x coord pos4=random8(2,MATRIX_HEIGHT);//ending y coord posR=random8(1,(MATRIX_HEIGHT));//radius for circle if (shapeNumber==0){//purpose i want there to be at least 1 line of blank pixles in each rectangle //draw, so i check if the difference in coordinates allows this, and if not edit to do so. if (pos1>=pos3){ if((pos1-pos3)<2){ if ((pos1-pos3)==1){ pos1=pos1+1; } else{ pos1=pos1+2; } } } if (pos3>=pos1){ if((pos3-pos1)<2){ if ((pos3-pos1)==1){ pos3=pos3+1; } else{ pos3=pos3+2; } } } if (pos2>=pos4){ if((pos2-pos4)<2){ if ((pos2-pos4)==1){ pos2=pos2+1; } else{ pos2=pos2+2; } } } if (pos4>=pos2){ if((pos4-pos2)<2){ if ((pos4-pos2)==1){ pos4=pos4+1; } else{ pos4=pos4+2; } } } } switch (shapeNumber){//draw the shape. case 0: matrix.DrawRectangle(pos1,pos2,pos3,pos4,ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending)); break; case 1: matrix.DrawCircle(pos2,pos2,posR,ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending)); break; } } EVERY_N_SECONDS(14){//wipe the screen turnOff(); setLEDs(); } FastLED.show(); FastLED.delay(20); }
Riolaurenti commented 2019-04-07 17:38:01 +01:00 (Migrated from github.com)

Try this.

EVERY_N_SECONDS(1){
for(int i=0,i<5;i++){
   if(i%2)pos[i]=random8(i+1,MATRIX_WIDTH);
   else pos[i]=random8(i,MATRIX_WIDTH);
  }
  if(shapeNumber==0){
    findPos(1,3);
    findPos(3,1);
    findPos(2,4);
    findPos(4,2);
    makeShape();
  }
}
void findPos(int a, int b){
  if (a >= b){
    if((a-b)<2){
      if((a+b)==1){
        a=a+1;
      }
      else a=a+2;
    }
  }
} // would be fairly easy to make a=b and b=a if a flag was turned, but it'd only save 2 lines of code.. 

void makeShape(){
  if (shapeNumber) matrix.DrawCircle(pos[2],pos[2],pos[5],ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending));
    if(!shapeNumber) matrix.DrawRectangle(pos[1],pos[2],pos[3],pos[4],ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending));
}
//might not be the best solution.

Have Fun

Sent from my Galaxy J6+ using FastHub

Try this. ``` int pos[] = {1,2,3,4,5}; EVERY_N_SECONDS(1){ for(int i=0,i<5;i++){ if(i%2)pos[i]=random8(i+1,MATRIX_WIDTH); else pos[i]=random8(i,MATRIX_WIDTH); } if(shapeNumber==0){ findPos(1,3); findPos(3,1); findPos(2,4); findPos(4,2); makeShape(); } } void findPos(int a, int b){ if (a >= b){ if((a-b)<2){ if((a+b)==1){ a=a+1; } else a=a+2; } } } // would be fairly easy to make a=b and b=a if a flag was turned, but it'd only save 2 lines of code.. void makeShape(){ if (shapeNumber) matrix.DrawCircle(pos[2],pos[2],pos[5],ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending)); if(!shapeNumber) matrix.DrawRectangle(pos[1],pos[2],pos[3],pos[4],ColorFromPalette( currentPalette, colorIndex, brightness, currentBlending)); } //might not be the best solution. ``` Have Fun _Sent from my Galaxy J6+ using [FastHub](https://play.google.com/store/apps/details?id=com.fastaccess.github)_
Riolaurenti commented 2019-04-08 09:55:02 +01:00 (Migrated from github.com)

Pfft, random8(1+1) not i.
Whatever you get the idea i'm sure. was a pretty lazy attempt.
also prob fine just if((a>=b) & ((a-b)<2))) { ...

Pfft, random8(1+1) not i. Whatever you get the idea i'm sure. was a pretty lazy attempt. also prob fine just if((a>=b) & ((a-b)<2))) { ...
kieran-boyle commented 2019-04-08 19:38:52 +01:00 (Migrated from github.com)

Closed reference above commit messages.

Closed reference above commit messages.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
squatnet/ArduinoStuff#74
No description provided.