I have a simple object that allows you to assign three properties (x,y,z) (lets call this object a “point”, because that is what it is). I then have a second object with a method that accepts two instances of the first object, and returns the distance between the two “points” in three dimensional space. I also need a method that will accept two “points”
and a double, representing distance traveled (from the first “point” parameter used) that returns a “point” object with its x,y,z coordinates.
I’m ok with everything except the calculation of the point coordinates that are on the original line between the two points supplied, that is at a certain distance from the first point.
“point” object:
public class POR
{
private double PORX;
private double PORY;
private double PORZ;
public double X
{
get { return PORX; }
set { PORX = value; }
}
public double Y
{
get { return PORY; }
set { PORY = value; }
}
public double Z
{
get { return PORZ; }
set { PORZ = value; }
}
public POR(double X, double Y, double Z)
{
PORX = X;
PORY = Y;
PORZ = Z;
}
I’m then using :
public double PorDistance(POR por1, POR por2)
{
return Math.Round(Math.Sqrt( Math.Pow((por1.X - por2.X),2) + Math.Pow((por1.Y - por2.Y),2) + Math.Pow((por1.Z - por2.Z),2)),2);
}
to return the distance between those two points I need something like
public POR IntersectPOR (POR por1, POR por2, double distance)
{
}
where distance is the distance traveled from por1 towards por2.
This can be done with a bit of help from vectors.
Let’s say your starting point is called P, and the other point is Q, and the distance is d. You want to find the point on the line PQ at a distance d from P towards Q.
First you need to find the direction of travel. That’s done by finding Q – P
Now you need to find the unit vector in that direction, so
Now you need to find the vector representing the distance traveled:
To find the final position, add your traveled vector to your starting point: