processing-rs icon indicating copy to clipboard operation
processing-rs copied to clipboard

Rect mode question

Open Luminoth opened this issue 3 years ago • 5 comments

Heyo, it's me again :) Digging into chapter 3 of Nature of Code and I've got this test app:

    let mut angle = 0.0;

    loop {
        screen.background(1.0, 1.0, 1.0, 1.0);

        screen.stroke(&[0.], &[0.], &[0.], &[1.]);
        screen.fill(&[0.7], &[0.7], &[0.7], &[0.7]);

        screen.push_matrix();

        screen.translate(0.0, 0.0, 0.0);
        screen.rotate_z(angle);

        screen.rect_mode("CENTER");

        let rect = Rect::new(&screen, &[0.0], &[0.0], &[0.0], &[0.1], &[0.1])?;
        screen.draw(&rect)?;

        screen.pop_matrix();

        screen.reveal()?;

        angle += 0.01;
    }

I would expect that to rotate the rectangle given that I've set the rect mode to CENTER (I believe I'm doing this correctly from looking at the source), but it instead looks to be rotating around the upper left corner as though I hadn't set the rect mode. Is there something I've missed here to get that working correctly?

Luminoth avatar May 01 '21 20:05 Luminoth

This is the p5js version of that example:

angle = 0.0;

function draw() {
  background(220);
  
  stroke(0);
  fill(200);
  
  push();
  
  translate(width / 2.0, height / 2.0);
  rotate(angle);
  
  rectMode(CENTER);
  
  rect(0.0, 0.0, 16.0, 16.0);
  
  pop();
  
  angle += 0.1;
}

to give an idea of what I'd expect to happen.

Luminoth avatar May 01 '21 20:05 Luminoth

Hey there :-)

What do you get if you put the rect creation line (Rect::new) just before the loop, instead of inside it? If you wanted, you can also move the stroke and fill lines to just before the loop, too.

I do not really think that this is the solution, but just to be sure.

  • Rob

rennis250 avatar May 05 '21 13:05 rennis250

This gives me the same results as before:

    let mut angle = 0.0;

    let rect = Rect::new(&screen, &[0.0], &[0.0], &[0.0], &[0.1], &[0.1])?;

    loop {
        screen.background(1.0, 1.0, 1.0, 1.0);

        screen.stroke(&[0.], &[0.], &[0.], &[1.]);
        screen.fill(&[0.7], &[0.7], &[0.7], &[0.7]);

        screen.push_matrix();

        screen.translate(0.0, 0.0, 0.0);
        screen.rotate_z(angle);

        screen.rect_mode("CENTER");

        screen.draw(&rect)?;

        screen.pop_matrix();

        screen.reveal()?;

        angle += 0.01;
    }

Rotating around the upper left corner of the rect rather than the center of it. I also tried moving the screen.rect_mode() call up before creating the rect outside of the loop, but no change there either.

Luminoth avatar May 05 '21 14:05 Luminoth

Okay, that strikes me as really quite odd. I will take a look into it.

rennis250 avatar May 05 '21 14:05 rennis250

I appreciate it. I tried digging down into what it might be, but everything honestly looks like it should be working to my untrained eye.

Luminoth avatar May 05 '21 14:05 Luminoth