Tangent vector in 3D

Hello, I try to found the vector direction of tangent in 3D, but I'm locked. I find a path on internet but no one give me the solution. http://stackoverflow.com/questions/26164521/how-is-calculating-the-unit-tangent-vector-of-a-normal-vector-in-3d http://stackoverflow.com/questions/5928490/how-to-calculate-tangents-of-a-circle-in-3d http://blog.db-in.com/calculating-normals-and-tangent-space/

I find a story about the cross product but I don't find the other vector to put in cross(Vec 1, vec2) :(

PVector dir, tan ;
void setup() {
  size(400,400,P3D) ;

  dir = new PVector(random(-1,1),random(-1,1),random(-1,1)) ;

  tan = new PVector() ;
  tan = dir.cross(new PVector(-dir.y, dir.x,0)) ;
}


void draw() {
  background(0) ;
  noFill() ;
  stroke(255) ;
  strokeWeight(1) ;
  int radius = 50 ;
  PVector final_pos = new PVector(width/2,height/2, 0) ;

  PVector pos_projection = new PVector() ;
  PVector dir_temp = dir.copy() ;
  pos_projection = dir_temp.mult(radius).add(final_pos).copy() ;

  PVector pos_tan = new PVector() ;
  PVector tan_temp = tan.copy() ;
  pos_tan = tan_temp.mult(radius/2).add(final_pos).copy() ;

  //display projection
  line(final_pos.x,      final_pos.y,       final_pos.z,
       pos_projection.x, pos_projection.y,  pos_projection.z ) ;
  // display tangent   
  line(pos_projection.x, pos_projection.y,  pos_projection.z,
        pos_tan.x, pos_tan.y,  pos_tan.z ) ;

  println(final_pos.x,      final_pos.y,       final_pos.z,
       pos_projection.x, pos_projection.y,  pos_projection.z ) ;

  println(pos_projection.x, pos_projection.y,  pos_projection.z,
        pos_tan.x, pos_tan.y,  pos_tan.z ) ;

}

Answers

  • Answer ✓

    There is no tangent vector in 3D instead there is a tangent plane.

    In 2D is is simple to calculate the tangent line to a curve. If that curve is in 3D space there is no single tangent line rather a 'surface' that is at right angles to the curve.

    In 3D if you have 2 distinct vectors then they define a plane (flat surface), the cross-product of the 2 vectors give you vector that is normal (at right angles) to the plane.

  • hmmm, you mean I must make a code like that :

      dir = new PVector(random(-1,1),random(-1,1),random(-1,1)) ;
      vector = new PVector(random(-1,1),random(-1,1),random(-1,1)) ;
    
      tan = new PVector() ;
      tan = dir.cross(vector) ;
    
  • edited January 2016

    Final code, I think that's work now thanks for the piece of advice quark !

    void setup() {
      size(400,400,P3D) ;
      create_tangent() ;
    }
    
    void draw() {
      background(0) ;
      noFill() ;
      stroke(255) ;
      strokeWeight(1) ;
      if(mousePressed) create_tangent() ;
    
      //display projection
      line(start_pos.x, start_pos.y, start_pos.z,
           pos_projection.x, pos_projection.y,  pos_projection.z ) ;
      // display tangent   
      line(pos_projection.x, pos_projection.y,  pos_projection.z,
            pos_tan.x, pos_tan.y,  pos_tan.z ) ;
    
      strokeWeight(5) ;
      point(start_pos.x, start_pos.y, start_pos.z) ;
      strokeWeight(5 +(pos_projection.z /30)) ;
      point(pos_projection.x, pos_projection.y,  pos_projection.z) ;
      strokeWeight(5 +(pos_tan.z /30)) ;
      point(pos_tan.x, pos_tan.y,  pos_tan.z) ;
    }
    
    PVector dir, dir_tan, vector ;
    PVector start_pos, pos_projection, pos_tan ;
    void create_tangent() {
      dir = new PVector(random(-1,1),random(-1,1),random(-1,1)) ;
      vector = new PVector(random(-1,1),random(-1,1),random(-1,1)) ;
      dir_tan = new PVector() ;
      dir_tan = dir.cross(vector) ;
      int radius = 100 ;
      start_pos = new PVector(width/2,height/2, 0) ;
    
      pos_projection = new PVector() ;
      PVector dir_temp = dir.copy() ;
      pos_projection = dir_temp.mult(radius).add(start_pos).copy() ;
    
      pos_tan = new PVector() ;
      PVector dir_tan_temp = dir_tan.copy() ;
      pos_tan = dir_tan_temp.mult(radius/4).add(pos_projection) ;
    }       
    
Sign In or Register to comment.