Path: news.mathworks.com!newsfeed-00.mathworks.com!newsfeed2.dallas1.level3.net!news.level3.com!postnews.google.com!n1g2000prb.googlegroups.com!not-for-mail
From: cpp.matlab@gmail.com
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to create vectors from position data?
Date: Wed, 5 Nov 2008 07:34:09 -0800 (PST)
Organization: http://groups.google.com
Lines: 113
Message-ID: <b35acf36-2398-4bca-8915-03472fe3b1b3@n1g2000prb.googlegroups.com>
References: <gerkmn$sdr$1@fred.mathworks.com>
NNTP-Posting-Host: 136.142.129.138
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
X-Trace: posting.google.com 1225899249 22520 127.0.0.1 (5 Nov 2008 15:34:09 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Wed, 5 Nov 2008 15:34:09 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: n1g2000prb.googlegroups.com; posting-host=136.142.129.138; 
	posting-account=o_n-RQoAAADC9xIv0-72rUXu8rLBt7Hi
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.3) 
	Gecko/2008092417 Firefox/3.0.3,gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:499101


This example will derive the smallest angle in degrees between two
lines in 3D space. The coordinates are in float form in x,y,z format.
For 2D lines, just set the z value to zero. E.g x,y,z = (23,14,0).

Written in c++ Visual Studio 2005, Win32, console application.

It works based on the dot product of two vectos. There is info
on the web about that as it applies to c++. Basically it states
that the dot product of two unit vectors yields the cosine of the
angle between them.

///////////////////////////////////////////////////////////////////////

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#pragma once

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows
headers
#include <stdio.h>
#include <tchar.h>
///////////////////////////////////////////////////////////////////////
// AngleBetweenTwoLines.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <math.h>

//Radians to degrees.
#define RTD 57.29577951 // 180/pi

typedef struct fvector_s
{float dx,dy,dz;
}fvector_t;

typedef struct fvertex_s
{ float x,y,z;
}fvertex_t;

float fdot( fvector_t *v1, fvector_t *v2 );
float fAngleDeg(fvector_t *v1, fvector_t *v2 );
float fVectorLength(fvector_t *v);
void fVectorNormalize(fvector_t *v);


//--------------------------------------------------------------

int _tmain(int argc, _TCHAR* argv[])
{

fvector_t v1,v2; //two vectors that represent our lines.
fvertex_t p1,p2,p3,p4; // 4 vertices that represent our lines.
float angle_between_lines;

// Make 4 vertices.
//For this example I will just hard code the values here.
p1.x = 0; p1.y = 0; p1.z = 0;
p2.x = 707; p2.y = 707; p2.z = 0;

p3.x = 0; p3.y = 0; p3.z = 0;
p4.x = 200; p4.y = 0; p4.z = 0;

//make two lines (vectors) from these vertices
v1.dx = p2.x - p1.x;
v1.dy = p2.y - p1.y;
v1.dz = p2.z - p1.z;

v2.dx = p4.x - p3.x;
v2.dy = p4.y - p3.y;
v2.dz = p4.z - p3.z;

//get the smallest angle between them.
angle_between_lines = fAngleDeg(&v1,&v2);

printf("The angle between these two lines is %f degrees.\r
\n",angle_between_lines);

return 0;
}


// returns the dot product of two vectors
float fdot( fvector_t *v1, fvector_t *v2 )
{return( (v1->dx*v2->dx) + (v1->dy*v2->dy) + (v1->dz*v2->dz) );}

// returns the length of a vector
float fVectorLength(fvector_t *v)
{return (float)sqrt(fdot(v,v));}

//return the angle in degrees between two lines
float fAngleDeg(fvector_t *v1, fvector_t *v2 )
{
fVectorNormalize(v1);
fVectorNormalize(v2);
return (acos(fdot(v1,v2)) * RTD);
}

// Makes a unit vector, a vector whose length is 1.
void fVectorNormalize(fvector_t *v)
{
float temp;

temp = 1 / fVectorLength(v);

v->dx = v->dx * temp;
v->dy = v->dy * temp;
v->dz = v->dz * temp;
}
///////////////////////////////////////////////////////////////////////