I'm trying to print a diamond shape using a function that changes the size of the diamond for every new int we decide to use. First I'm printing the top half part, then I'm printing the bottom half part, though when doing so, it causes me problems. Here I leave you with the code:
int ancho;
void dibujaPiramide(int ancho){
for(int i = 1; i <= ancho; i++){
for(int j = 1; j<= ancho-i; j++){
print(" ");
}
for(int k = 0; k<2*i-1; k++){
print("*");
}
println();
}
for(int i = ancho; i >= 1; i--){
for(int j = i; j >= 1-i; j--){
print(" ");
}
for(int k = 0;k < 2*i-1;k++){
print("*");
}
println();
}
}
void setup(){
dibujaPiramide(6);
}
I want to print a full diamond that changes size depending of the entry variable of the function
One technique is to add a counter and increment it for each row of the bottom portion of the pyramid. Depending on the row number a proportional number of spaces is printed. The demo should be scalable.
int ancho;
int counter = 0;
void dibujaPiramide(int ancho) {
for (int i = 1; i <= ancho; i++) {
for (int j = 1; j<= ancho-i; j++) {
print(" ");
}
for (int k = 0; k<2*i-1; k++) {
print("*");
}
println();
}
for (int i = ancho; i >= 1; i--) {
counter++;
for(int x = 1; x < counter; x++ ){
print(" ");
}
for (int k = 0; k < 2*i-1; k++) {
print("*");
}
println();
}
}
void setup() {
surface.setVisible(false);
dibujaPiramide(10);
}