View Full Version : creating vectors from points
Buexe 08-12-2003, 08:07 PM Hi, I have a three points a,b,c and I would like to check if point c is a point on a line between point a and b. To do this I think I need to compute the vectors of point a and c and point b and c. if both vectors are equal they should be in a straight line, right? So I have the positions of all three points but how do I convert them to vectors?
Any clues (especially if the approach is okay) are appreciated
TIA
desparate " I dunno `bout `em vectorz" buexe
|
|
Given 2 points, you can calculate a vector for them by subtracking one point from the other.
float $pointA[]={3.5, 43.44, 67.4};
float $pointB[]={12.4, 6.33, 33.8};
vector $vec1=<<$pointA[0]-$pointB[0],$pointA[1]-$pointB[1],$pointA[2]-$pointB[2]>>;
--g
Buexe
08-12-2003, 08:34 PM
Thanx GDC!!!!!
So I came up with this one:
//start
global proc float checkAngle (string $loc1, string $loc2, string $loc3)
{
float $pos1[] =`xform -q -ws -t loc1`;
float $pos2[] =`xform -q -ws -t loc2`;
float $pos3[] =`xform -q -ws -t loc3`;
vector $v1 = << $pos2[0] - $pos1[0] , $pos2[1] - $pos1[1] , $pos2[2] - $pos1[2] >>;
vector $v2 = << $pos3[0] - $pos2[0], $pos3[1] - $pos2[1] , $pos3[1] - $pos2[2] >>;
$angle1 = angle ($v1,$v2);
$deg1 = `rad_to_deg $angle1`;
print ("The angle between the three points is " + $deg1 + "\n");
return $deg1;
}
//end
"da good ol` vectorz guru" buexe ; )
Buexe
08-12-2003, 09:46 PM
Is there a way to get only the first 4 oder 5 digits behind the period? Because everything works fine until I get those scientific float values like 0.23443E+24.
TIA b
Bryan Ewert has this proc posted on his excellent MEL web pages.
This will solve your problem.
// Procedure Name: roundoff
//
// Author: Duncan Brinsmead
//
// Description:
// simple function to round float values to a particular decimal
//
// Usage: roundoff
//
// examples
//
// roundoff -1.119 2
// Result: -1.12
//
// roundoff 256.812 0
// Result: 257
//
// roundoff 128.1 -1
// Result: 130
global proc float roundoff( float $f, int $n )
{
// we divide if n < 0 to avoid numeric
// precision problems
if( $n > 0 )
{
float $roundScale = pow(10,$n);
if( $f > 0 )
return( ((float)(int)($f * $roundScale + 0.5)) /$roundScale );
else
return( ((float)(int)($f * $roundScale - 0.5)) /$roundScale );
}
else
{
float $roundScale = pow(10,-$n);
if( $f > 0 )
return( ((float)(int)($f/$roundScale + 0.5)) *$roundScale );
else
return( ((float)(int)($f/$roundScale - 0.5)) *$roundScale );
}
}
FYI - his web page is http://www.ewertb.com
--g
mark_wilkins
08-13-2003, 12:23 AM
Of course, it would be really nice if good output formatting were part of MEL... :D
-- Mark
Buexe
08-13-2003, 09:20 AM
Great thing!! I was already on Bryan`s excellent page but I missed that one. Anyway, the proc seems to work now you can give three points and an integer for the precision:
//start
//Description: calculate the angle of the two vectors between three points
global proc float checkAngle (string $loc1, string $loc2, string $loc3, int $precision)
{
float $pos1[] =`xform -q -ws -a -t $loc1`;
float $pos2[] =`xform -q -ws -a -t $loc2`;
float $pos3[] =`xform -q -ws -a -t $loc3`;
float $pos1_x =`roundoff $pos1[0] $precision`;
float $pos1_y =`roundoff $pos1[1] $precision`;
float $pos1_z =`roundoff $pos1[2] $precision`;
float $pos2_x =`roundoff $pos2[0] $precision`;
float $pos2_y =`roundoff $pos2[1] $precision`;
float $pos2_z =`roundoff $pos2[2] $precision`;
float $pos3_x =`roundoff $pos3[0] $precision`;
float $pos3_y =`roundoff $pos3[1] $precision`;
float $pos3_z =`roundoff $pos3[2] $precision`;
float $vec1_1 = $pos2_x - $pos1_x;
float $vec1_2 = $pos2_y - $pos1_y;
float $vec1_3 = $pos2_z - $pos1_z;
float $vec2_1 = $pos3_x - $pos2_x;
float $vec2_2 = $pos3_y - $pos2_y;
float $vec2_3 = $pos3_z - $pos2_z;
$v1 = << $vec1_1 , $vec1_1 , $vec1_1 >>;
$v2 = << $vec2_1 , $vec1_1 , $vec1_1 >>;
$angle1 = angle ($v2,$v1);
$deg1 = `rad_to_deg $angle1`;
print ("The angle between the two vectors is " + $deg1 + "\n");
return $deg1;
}
// Procedure Name: roundoff
//
// Author: Duncan Brinsmead
//
// Description:
// simple function to round float values to a particular decimal
//
// Usage: roundoff
//
// examples
//
// roundoff -1.119 2
// Result: -1.12
//
// roundoff 256.812 0
// Result: 257
//
// roundoff 128.1 -1
// Result: 130
global proc float roundoff( float $f, int $n )
{
// we divide if n < 0 to avoid numeric
// precision problems
if( $n > 0 )
{
float $roundScale = pow(10,$n);
if( $f > 0 )
return( ((float)(int)($f * $roundScale + 0.5)) /$roundScale );
else
return( ((float)(int)($f * $roundScale - 0.5)) /$roundScale );
}
else
{
float $roundScale = pow(10,-$n);
if( $f > 0 )
return( ((float)(int)($f/$roundScale + 0.5)) *$roundScale );
else
return( ((float)(int)($f/$roundScale - 0.5)) *$roundScale );
}
}
//end
:beer: thanx a trillion!!!
But now I gotta go and polish up my math knowledge...
buexe
CGTalk Moderation
01-15-2006, 08:00 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
vBulletin v3.0.5, Copyright ©2000-2012, Jelsoft Enterprises Ltd.