Advertisement

Distance between Point A and Point B?

Started by August 21, 2002 05:32 AM
2 comments, last by alexjascott 22 years, 6 months ago
I need to program a function that will give me a distance value between the viewpoint, and either a polygon or a point. I need this for a LOD system I am working on, the further the distance, the lower the number of polygons I will draw. However I freely admit I am poor at math (bad English education system), and need a simple function to do it. I have read lots of complex math papers on the www - but all are too complicated for me. Please help, Alex.
xPOINT pointA,pointB,AtoB;

pointA.x = 0;
pointA.y = 5;
pointA.z = 3;

pointB.x = 0;
pointB.y = 2;
pointB.z = 0;

// Generate direction vector from pointA to pointB
AtoB.x = pointA.x - pointB.x;
AtoB.y = pointA.y - pointB.y;
AtoB.z = pointA.z - pointB.z;

// Find length of this direction vector
distance = sqrt((AtoB.x * AtoB.x)+(AtoB.y * AtoB.y)+(AtoB.z * AtoB.z));
Advertisement
squarerr ((v2[0]-v1[0])*(v2[0]-v1[0])+(v2[1]-v1[1])*(v2[1]-v1[1])+(v2[2]-v1[2])*(v2[2]-v1[2])))
where xPOINT is defined as:

typedef struct
{
float x,y,z;
} xPOINT;

and that should be sqrtf, not sqrt.

distance is a float value of course.

This topic is closed to new replies.

Advertisement