Draw black and white diamonds

“In this section you should write a Processing program that creates a display window of 500 by 500 pixels,
then uses a proper control structure to create a collection of many small black and white diamonds with
the indicated dimensions in pixels.”

someone please help, Im not even close. I know I have to use nested loops but I just cant get it.
I need to make this in a window.

image

Nevermind, that thread has gone…

that page does not exist when i click the link

As I was saying…

The code needed to do this is not hard. It’s only 10 lines line.

The first line is a call to size().
The second line calculates a value.
The third line is an appropriate call to translate().
The fourth line is an appropriate call to rotate().
Lines 5 and 6 setup two for loops, with appropriate bounds.
Line 7 sets the fill() color appropriately.
Line 8 draws a rectangle with a call to rect().
Lines 9 and 10 close the two for loops.

So basically the code will take this shape:

size( ???, ??? );
??? d = ??? * sqrt( ??? );
translate( ??? / ???, 0 );
rotate( ??? / ??? );
for( ??? i = ???; i < ???; ??? ){
  for( ??? j = ???; j < ???; ??? ){
    fill( ( ??? + ??? % ??? == ??? ) ? ( ??? ) : ( ??? ) );
    rect( ???, ???, ???, d );
  }
}
2 Likes

So what would be in place of the question marks if I wanted to make it in a 500x500 window with the size of the rects to be 10x10?

Well, that’s for you to work out, isn’t it?

If you want the window size to be 500 by 500, then it’s immediately obvious what the first two sets of question marks should be… because they are the parameters to size(), which sets the window size. And you want a size of 500 by 500.

… Are you sure the rectangles you want to draw are 10 by 10 pixels? Try drawing a diagram of one of them on paper. How long is one side of a rectangle? How might you actually calculate that value?

What you have is way more than enough help. Please at least try to do your own homework now. So far you have shown us NO effort.

3 Likes

First of all, your code was incorrect as i have found the answer. Secondly, you were no help whatsoever and i spent hours trying to solve this problem. You arent even qualified to answer this simple question.

void setup ()
{
  size(500,500);
}

void draw() {
  
  
  int centerY =0;
  int centerX =250;
  for(int i = 0; i <=25; i++){
    for(int j = 0; j< i; j ++){
      int startX = centerX;
      if((i%2) ==0){
        startX +=10;
        fill(255);
        
      }else{
        fill(0);
      }
      
      drawQuad(startX - ((i/2) *20) + ( j* 20), centerY+(i*10));
    }
  }
}
void drawQuad(int x, int y){
  quad(x-10,y,x,y-10,x+10,y,x,y+10);
}

TfGuy44 is more than capable of doing your homework. But that is not in his interest. This forum is not for doing peoples homework. You are not going to learn to code if you you just copy and paste TfGuy44s work. So in order to make sure that people actually learn to code and get help with their homework is to require some effort from the person who ask the question

You had not shown any code. So no one could know that you had done any work. I am not saying you are not putting in any effort, but you have to show what you have tried.

1 Like

Indeed, my interest is in finding interesting problems and then solving them. Your problem was easily solved, and I didn’t feel like posting the code because it was clearly a homework assignment and the forums frown on that sort of thing. The hints I tried to provide were an effort to get you to do it yourself while still leading you in a direction towards a solution.

That said, now that you have posted your own code that demonstrates how to do it, I am free to post my own version that also does it:

size( 500, 500 );
float d = 10 * sqrt( 2 );
translate( width / 2.0, 0 );
rotate( HALF_PI / 2.0 );
for( int i = 0; i < 25; i++ ){
  for( int j = 0; j < 25-i; j++ ){
    fill( ( (j + i) % 2 == 0 ) ? ( 0 ) : ( 255 ) );
    rect( i*d, j*d, d, d );
  }
}

As you see, it is 10 lines long and follows the general shape described above. Even though it is short, it is packed-full of useful tricks. In addition to demonstrating the use of translate() and rotate(), it also makes use of the ?: operator, which is handy shorthand to know. I also highly suggest you reconsider your attitude.

1 Like

I think you have to understand that I am in first year general engineering which makes me extremely busy. Our prof barely teaches us anything and then expects us to be able to do these assignments with little knowledge on the topic. For example, we haven’t learned the rotate() and translate() functions yet and I simply do not have time to watch videos or read up on them. As much as I enjoy the satisfaction of programming, I will never take a computing course again because I do not want to go into software/electrical engineering. Hence, I don’t need people to get mad at me when I ask for direct answers, I simply do not have the time to complete partial answers.

If i had time i would love to spend hours on it

@engg23369 We all understand the struggles of being a student, so you could have told us more about what you have or have not learned yet in your original post. There is no need to be rude when someone is trying to help you in the learning process to help you get to the solution, which is what the forum is all about. But you have to show us your effort.

2 Likes

Hi Alex. I am also a first-year engg student at U of C. I was stuck in a similar problem as you, and I was hoping if you could help me solve the problem. I am facing similar challenges as you; prof never teaches this stuff, TA’s are useless and no help online. Id highly appreciate it if you could help with this problem.

Hello,

This is very achievable and an excellent exercise:

image

image

The diagonal took a bit of thinking!

:slight_smile:

References to get you started:

https://processing.org/tutorials/drawing/

Note:
I did not use rotate() or translate() in my code.
You know the size of the cube and can increment x and y in nested loops appropriately to place them at the x and y coordinates on the screen.
The modulo operator is also useful if you understand how to use it; I used counters inside my loop instead.

:slight_smile:

3 Likes

We all face challenges in life and we must overcome them with dignity.
Your comments about the instructors and TA’s were personal and not necessary.

You have the challenge of learning to program and if you persevere and work at it you will be successful.

:slight_smile:

1 Like