[vtkusers] Angle between two vectors

Harold boogiedoll at yahoo.com
Thu Jan 2 22:31:59 EST 2014


Hi,
I'm trying to find the angle between two vectors from three points, like in the vtkAngleWidget. I've checked vtkAngleRepresentation2D.cxx and made the functions below. Is there any simpler way to get the angle?

 void get_angle_old(double p1[3], double  c[3], double p2[3])
 {
    double theta1 = atan2(p1[1]-c[1],p1[0]-c[0]);
    double theta2 = atan2(p2[1]-c[1],p2[0]-c[0]);
    if ( p1[0]-c[0] == 0.0 || p2[0]-c[0] == 0.0 )
         {
         return;
         }
    if ( (theta1 >= 0.0 && theta1 <= vtkMath::Pi() &&
            theta2 >= 0.0 && theta2 <= vtkMath::Pi()) ||
           (theta1 <= 0.0 && theta1 >= -vtkMath::Pi() &&
            theta2 <= 0.0 && theta2 >= -vtkMath::Pi()) )
        {
        ; //do nothin angles are fine
        }
    else if ( theta1 >= 0.0 && theta2 <= 0.0 )
        {
        if ( (theta1 - theta2) >= vtkMath::Pi() )
          {
          theta2 = theta2 + 2.0*vtkMath::Pi();
          }
        }
    else //if ( theta1 <= 0.0 && theta2 >= 0.0 )
        {
        if ( (theta2 - theta1) >= vtkMath::Pi() )
          {
          theta1 = theta1 + 2.0*vtkMath::Pi();
          }
        }
   std::cout << "Angle = " << vtkMath::DegreesFromRadians(abs(theta1-theta2)) << std::endl;

 }


 void get_angle_new(double p1[3], double c[3], double p2[3])
 {
    double vector2[3], vector1[3];
    vector1[0] = p1[0] - c[0];

    vector1[1] = p1[1] - c[1];
    vector1[2] = p1[2] - c[2];
    vector2[0] = p2[0] - c[0];
    vector2[1] = p2[1] - c[1];
    vector2[2] = p2[2] - c[2];
    vtkMath::Normalize( vector1 );
    vtkMath::Normalize( vector2 );
    double angle = acos( vtkMath::Dot( vector1, vector2 ) );
    std::cout << "angle: " << vtkMath::DegreesFromRadians(angle) << std::endl;

 }
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.vtk.org/pipermail/vtkusers/attachments/20140102/8b28c34b/attachment.htm>


More information about the vtkusers mailing list