Rotating line code

Hi there I have a code query. I am wanting to create a illustration out of lines that at one point are cemented and the other moveable (preferably all together). I have made small alterations to an existing code but this is not quite working. I would like the arm to do a full 360 rotation. I would like to be able to multiple lines that all follow the mouse together

Any suggestion or help is greatly appreciated!

float x, y;
float angle1 = 0.0;
float segLength = 100;

void setup() {
  size(700, 700);
  strokeWeight(10);
  stroke(200, 160);

  x = width * 0.5;
  y = height * 0.5;
}

void draw() {
  background(0);

  angle1 = (mouseX/float(width) - 0.0) * -PI;


  pushMatrix();
  segment(x, y, angle1); 
  popMatrix();
}

void segment(float x, float y, float a) {
  translate(x, y);
  rotate(a);
  line(0, 0, segLength, 0);
}

Answers

  • _vk_vk
    edited October 2013

    for the 360º just replace PI by TWO_PI. Or maybe a more trigonometric approach :)

        float x, y; 
        float angle1 = 0.0; 
        float segLength = 100;
    
        void setup() { 
          size(700, 700); 
          strokeWeight(10);
          stroke(200, 160); 
    
    
          x = width * 0.5; 
          y = height * 0.5;
        }
    
        void draw() { 
          background(0);
    
          angle1 = map(mouseX, 0, width, 0, TWO_PI);
    
          float ex = x + cos(angle1)* segLength;
          float ey = y + sin(angle1)* segLength;
    
          if (!mousePressed) {
            line(x, y, ex, ey);
          }
          else { 
              // several lines... in one way...
              for (float i = 0; i < 6; i += 0.7) {
              float nx = x+i*50;
              ex = nx + cos(angle1)* segLength;
              ey = y + sin(angle1)* segLength;
              line(nx, y, ex, ey);
            }
          }
        }
    

    Not sure what you need for several lines, but click the mouse to see some. That's usually call for an OOP approach though... Also to paste code, ident 4 spaces each line, or paste/select/hit 'C' button, it will ident for you ;)

Sign In or Register to comment.