3
votes

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.

3
Can you post some code to show us what you have so far? This sounds a bit like homework... - Andrew Hare

3 Answers

5
votes

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.

  1. First you need to find the direction of travel. That's done by finding Q - P

    v = Point(Q.x - P.x, Q.y - P.y, Q.z - P.z)
    
  2. Now you need to find the unit vector in that direction, so

    scale = sqrt(v.x*v.x + v.y*v.y + v.z*v.z)
    unit = Point(v.x/scale, v.y/scale, v.z/scale)
    
  3. Now you need to find the vector representing the distance traveled:

    t = Point(unit.x*d, unit.y*d, unit.z*d)
    
  4. To find the final position, add your traveled vector to your starting point:

    final = Point(P.x + t.x, P.y + t.y, P.z + t.z)
    
1
votes

It looks like you want something similar to:

public class Point
{
     public double x, y, z;

     // ctors, Add(), Subtract() omitted

     public Point Normalize()
     {
         double m = Magnitude;
         if (m != 0.0) return ScaleTo(1.0/m);
         return new Point();
     }

     public double Magnitude
     {
         get { return Math.Sqrt(x * x + y * y + z * z); }
     }

     public Point ScaleTo(double s)
     {
         return new Point(x * s, y * s, z * s);
     }
}

public Point PointOnLine(Point from, Point to, double dist)
{
    return from.Add(to.Subtract(from).Normalize().ScaleTo(dist));
}
1
votes

No actual code because I think this is more of a conceptual question. This may not be the most efficient, but when I'm doing this I just apply the ratio between the total distance and the segment to the coordinate deltas. For example if I have points at 0,0,0 and 1,2,3 the total 3D distance is 3.74, if I want to place a point 1 unit from the first point the ratio is 1/3.74 so the new coordinate would be .2673 of the total distance from the first point toward the second or .267 , .534 , .802