Share a 3D line

edited March 2014 in Share Your Work

Hello all,

I just wanted to share a work by James.

It's a 3D line.

Greetings, Chrisir

void drawLine(float x1, float y1, float z1, 
float x2, float y2, float z2, 
float weight, color strokeColour)
// drawLine was programmed by James Carruthers
// see http://processing.org/discourse/yabb2/YaBB.pl?num=1262458611/0#9
{
  PVector p1 = new PVector(x1, y1, z1);
  PVector p2 = new PVector(x2, y2, z2);
  PVector v1 = new PVector(x2-x1, y2-y1, z2-z1);
  float rho = sqrt(pow(v1.x, 2)+pow(v1.y, 2)+pow(v1.z, 2));
  float phi = acos(v1.z/rho);
  float the = atan2(v1.y, v1.x);
  v1.mult(0.5);
  pushMatrix();
  translate(x1, y1, z1);
  translate(v1.x, v1.y, v1.z);
  rotateZ(the);
  rotateY(phi);
  noStroke();
  fill(strokeColour);
  box(weight, weight, p1.dist(p2)*1.2);
  popMatrix();
}

Comments

  • Here's some see-it-in-action code.

    void setup(){
      size(400,400,P3D);
    }
    
    void draw(){
      background(0);
      translate(width/2,height/2,0);
      scale(90);
      rotateY(map(millis()%5000,0,5000,0,TWO_PI));
      drawLine(-1,-1,-1,1,1,1,.01,color(255,0,0));
      drawLine(-1,1,1,1,1,1,.01,color(255,255,0));
      drawLine(1,-1,-1,1,1,1,.01,color(255,0,255));
      noFill();
      stroke(255);
      box(2);
    }
    
  • Thanks a ton!

Sign In or Register to comment.