tags:

views:

639

answers:

4

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.

+1  A: 

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));
}
Jason
+4  A: 

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)
    
sykora
Perfect... the only thing i'm running into is if the distance entered is greater than the actual distance....but i can easily do that check since im already calculating the distance between the two points from the get-go
Patrick
+1  A: 

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

Dennis
i thought about that.... but what tends to happen is that after the 4th or 5th iteration... (the line/ray can change direction) the accuracy drops... "4 right turns should bring you back to where you started" but that wasnt happening to well... i was coming off by about a unit and half... it would probably work in a smaller system... but when working with huge units... and hunge numbers of units... it can get messy...
Patrick
@Patrick - that's because you're rounding. Why do you?
NVRAM
A: 

problem is Pi is actually 3. the .14159 etc is just wiggle room. use 3 for Pi the univese works great; it just needs room to vibrate after all it is alive not dead like your math and your minds.