I write in C on arduino uno. The idea is to display a pyramid from a topographic representation, so the variables that I have are "seedP" - the average value, the size of the "pyramid" pyramid - the number of layers and the number of letters in a row - "layer".
A pyramid with these values ββin the code seed P = 6, sizeP = 3 will look like this:
44444 45554 45654 45554 44444
Here is the code:
void setup() { Serial.begin(9600); int seedP = 6; //Centre value int sizeP = 3; //Amount of layers int layer = (sizeP*2)-1; for(int i=0; i<layer; i++){ for(int j=0; j<layer; j++){ Serial.print(seedP); } Serial.println(); } }
I still canβt fully understand the concept of the algorithm, which I could check for the first and last layer, and then display the value full β4β in this example, but I'm not sure how to handle the other layers. The code right now only displays β6β in 5 columns and 5 in a row.
Edit: updated code and below - grid
void setup() { Serial.begin(9600); int sizeP = 3; //Amount of layers int layer = (sizeP*2)-1; int seedP = 6; //Centre value int seedX = sizeP-1; int seedY = sizeP-1; for(int i=0; i<layer; i++){ for(int j=0; j<layer; j++){ //i,j = x,y grid int distanceX, distanceY, distance; //Distance between a cell and center value distanceX = abs(seedX - j); distanceY = abs(seedY - i); distance = distanceX + distanceY; } Serial.println(); } }
Values ββfrom each cell to the center
(0,0)=4, (0,1)=3, (0,2)=2, (0,3)=3, (0,4)=4 (1,0)=3, (1,1)=2, (1,2)=1, (1,3)=2, (1,4)=3 (2,0)=2, (2,1)=1, (2,2)=0, (2,3)=1, (2,4)=2 (3,0)=3, (3,1)=2, (3,2)=1, (3,3)=2, (3,4)=3 (4,0)=4, (4,1)=3, (4,2)=2, (4,3)=3, (4,4)=4