0

I have four point p0 , p1 ,p1' , p2' , each defined by x,y,z component and all lay on one line as in the figure

enter image description here

I want to get the line segment (the dashed part) result from the intersection between the four points

any suggestion , or sample code using C#

Random Dev
  • 51,810
  • 9
  • 92
  • 119
AMH
  • 6,363
  • 27
  • 84
  • 135

2 Answers2

1

This is more or less the same answer as Howard gave but pressed into C# ... I hope this helps with your code-base.

This code snippet should do the trick (finding the mid-points from your 4, but only if all are colinear) - also note I don't check for real intersection, you can do this easily youself by inspecting the answer and your points. I did not take the time and implement the Vector3D struct in a sensible manner (operators, ...) - you can do this easily too. Also note that this will work for not only 4 points but keep your diagram in mind.

private struct Vector3D
{
    public double X { get; set; }
    public double Y { get; set; }
    public double Z { get; set; }
}
static class Vectors
{
    static public double ScalProd(Vector3D v1, Vector3D v2)
    {
        return v1.X*v2.X + v1.Y*v2.Y + v1.Z*v2.Z;
    }

    static public Vector3D Minus(Vector3D v1, Vector3D v2)
    {
        return new Vector3D {X = v1.X - v2.X, Y = v1.Y - v2.Y, Z = v1.Z - v2.Z};
    }

    static public Vector3D Normalize(Vector3D v)
    {
        var len = Math.Sqrt(ScalProd(v, v));
        return new Vector3D {X = v.X/len, Y = v.Y/len, Z = v.Z/len};
    }
}

private Vector3D[]  FindIntersectionOnCoLinearVectors(params Vector3D[] input)
{
    if (input.Length < 2) throw new Exception("you need a minimum of two vectors");
    var v0 = input[0];
    var direction = Vectors.Normalize(Vectors.Minus(input[1], v0));
    Func<Vector3D, double> projectOntoLineStartingAtv0 =
        v => Vectors.ScalProd(direction, Vectors.Minus(v, v0));
    var mapped = input.OrderBy(projectOntoLineStartingAtv0).ToArray();
    return new Vector3D[] {mapped[1], mapped[2] };
}
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • thanks a lot I need a help in finding the intersection of two rectangles in 3D , could u help me please or chat – AMH Aug 28 '11 at 09:13
  • I will try and look at it - I have to run in a few minutes. Maybe I can find the time for a chat later today – Random Dev Aug 28 '11 at 09:15
  • BTW: I think you can strop the normalization in there - it's just a constant scale ... – Random Dev Aug 28 '11 at 09:17
  • sorry - no skype (or no cam/mic on my home pc :) ... there is a rather good text chat here though) – Random Dev Aug 28 '11 at 09:18
  • Ok no problem hope u have time to chat , I wished if I can send u a code that contain the problem – AMH Aug 28 '11 at 09:21
  • Tuple not recognized, I use c# 2008 – AMH Aug 28 '11 at 09:22
  • could u help me in my question http://stackoverflow.com/questions/7055673/intersection-between-two-rectangles-in-3d – AMH Aug 28 '11 at 09:28
0

You may proceed as follows:

Step 1: Transformation into a 1D-problem

  • define t(P) = (P-P0).(P1-P0) / (P1-P0).(P1-P0) where the dot denotes the scalar product
  • t is a linear measure on the line through P1 and P0
  • thus we have t(P0)=0, t(P1)=1

Step 2: Solve the problem in 1D

  • We assume t(P0') <= t(P1') (otherwise swap P0' and P1' in the following lines)
  • Now the following cases are possible
    • t(P1') < 0 => no intersection
    • 1 < t(P0') => no intersection
    • t(P0') <= 0 <= t(P1') <= 1 => intersection is segment (P0,P1')
    • t(P0') <= 0 < 1 < t(P1') => intersection is segment (P0,P1)
    • 0 <= t(P0') <= t(P1') <= 1 => intersection is segment (P0',P1')
    • 0 <= t(P0') <= 1 < t(P1') => intersection is segment (P0',P1)
  • alternatively if you are only interested in the t-values, the intersection is given by the line segment between t0 = max(0, t(P0')) and t1 = min(1, t(P1')) iff t0 <= t1
Howard
  • 38,639
  • 9
  • 64
  • 83
  • is there any mathematical example ,forgive me but I don't have any mathematical background let we have the following points p0 (X: 236.5 Y: -160 Z: -21.5) , p1 (X: -235.5 Y: -160 Z: -21.5) ,p0' (X: -39.25781 Y: 0 Z: -21.5) and p1'(X: 148 Y: 0 Z: -21.5). I couldn't understand the algorithm , and how to apply it programatically – AMH Aug 28 '11 at 09:01
  • how come, I am trying to get the line segment of intersection between two rectangles , u can see my previous question http://stackoverflow.com/questions/7055673/intersection-between-two-rectangles-in-3d , so I got the line direction by cross product of the two planes, then get points on it , I htink I miss something – AMH Aug 28 '11 at 09:09
  • @AMH first two are on the line (Y=-160;Z=-21.5) while the second two are one the line (Y=0;Z=-21.5). So they are clearly not all on the same line. – Howard Aug 28 '11 at 09:13