For Loops and Rotation

I have the following piece of code in my draw function. I want the line segments all to point towards the centre of the circle they create, rather than pointing directly downwards.

I've only been able to make the lines all rotate to the same degree. So, can someone explain how to make each line rotate +6 degrees each time? Thank you.

  for(int i = 0; i < 60; i++ ) {
    float xPos = 230 * sin(TWO_PI/60 * i);
    float yPos = 230 * cos(TWO_PI/60 * i);
    line(clockPos + xPos, clockPos + yPos, clockPos + xPos, clockPos + yPos + 10);
  }
Tagged:

Answers

  • It's easier if the two points have the same angle but different radii.

  • Post a complete, working example. To fix it in a code snippet, I'd have to guess at and write everything else you didn't post just to see what's going wrong, and do all that before I can even think about what's wrong and how to fix it. Effort required? No help for you.

    To fix it in a complete, working example, I can run it, see what is wrong, and then make the change needed. Easy. Enjoy being helped.

    Always post working examples from now on.

  • edited July 2015

    Here's the rest.

    float clockPos;
    
    void setup() {
      size(700, 700);
      clockPos = 350;
    }
    
    void draw() {
      for(int i = 0; i < 60; i++ ) {
        float xPos = 230 * sin(TWO_PI/60 * i);
        float yPos = 230 * cos(TWO_PI/60 * i);
        line(clockPos + xPos, clockPos + yPos, clockPos + xPos, clockPos + yPos + 10);
      }
    }
    
  • Answer ✓
    for(int i = 0; i < 60; i++ ) {
      float angle = TWO_PI / 60 * i;
      float xPos1 = 210 * sin(angle);
      float yPos1 = 210 * cos(angle);
      float xPos2 = 230 * sin(angle);
      float yPos2 = 230 * cos(angle);
      line(clockPos + xPos1, clockPos + yPos1, clockPos + xPos2, clockPos + yPos2);
     }
    

    like i said, same angle, two different radii.

  • Thanks, I actually ended up doing this:

      for (int i = 0; i < 60 ; i++) {
        float xPos = diam/2 * sin(TWO_PI/60 * i);
        float yPos = diam/2 * cos(TWO_PI/60 * i);
          if (i % 5 == 0) {
            strokeWeight(3);
          } else {
            strokeWeight(1);
          }
        pushMatrix();
          translate(clockPos + xPos, clockPos + yPos + lineLength);
          rotate(radians(-deg * i));
          stroke(0);
          line(0, -lineLength, 0, lineLength);
        popMatrix();
      } 
    
Sign In or Register to comment.