1

I think it'll be enough if I understand what

np.linalg.norm(A)
np.linalg.det([t, _A, cross])
np.cross(_A, _B)

does

Python code founded here 6. answer by @Fnord

I was looking at their doc, but I couldn't understand a thing it's even worst than in MSDN doc

EDIT:

I deleted my code as my assumptions were totally incorrect.

even if my original Question wasn't answered my original problem was solved.

Problem:

how to find 2 closest points between two skew lines in 3D

this gr8 tutorial could've helped me IF I'd understood how to learn programmatically what t and s is.

Community
  • 1
  • 1
MilitaryG
  • 75
  • 1
  • 9
  • 1
    It doesn't mean "normalized". Numpy is calculating the cross products between the two line vectors, then the matrix norms and determinants based on that. – grovesNL Mar 10 '14 at 14:59
  • can you help me than translating this methods in to Unity C# please? – MilitaryG Mar 10 '14 at 15:20

1 Answers1

1

You don't need to rewrite these methods, as there are already equivalent methods built-in.

For example, to get the distance between two 3-dimensional vectors, you can use Vector3.Distance (Unity entry)

float distance = Vector3.Distance(someVector, anotherVector);

If you want to find the two closest points, you should be able to accomplish this using other vector methods as well.

Here is an example of how to use the methods to find the closest points on two difference lines (taken from the Unity wiki). It still uses the determinants to calculate this. For an explanation of why this works, you need to understand basic linear algebra for vectors.

//Two non-parallel lines which may or may not touch each other have a point on each line which are closest
//to each other. This function finds those two points. If the lines are not parallel, the function 
//outputs true, otherwise false.
public static bool ClosestPointsOnTwoLines(out Vector3 closestPointLine1, out Vector3 closestPointLine2, Vector3 linePoint1, Vector3 lineVec1, Vector3 linePoint2, Vector3 lineVec2){

    closestPointLine1 = Vector3.zero;
    closestPointLine2 = Vector3.zero;

    float a = Vector3.Dot(lineVec1, lineVec1);
    float b = Vector3.Dot(lineVec1, lineVec2);
    float e = Vector3.Dot(lineVec2, lineVec2);

    float d = a*e - b*b;

    //lines are not parallel
    if(d != 0.0f){

        Vector3 r = linePoint1 - linePoint2;
        float c = Vector3.Dot(lineVec1, r);
        float f = Vector3.Dot(lineVec2, r);

        float s = (b*f - c*e) / d;
        float t = (a*f - c*b) / d;

        closestPointLine1 = linePoint1 + lineVec1 * s;
        closestPointLine2 = linePoint2 + lineVec2 * t;

        return true;
    }

    else{
        return false;
    }
}
grovesNL
  • 6,016
  • 2
  • 20
  • 32
  • 1
    that isn't for lines, that's for 2 points totally different things. – MilitaryG Mar 10 '14 at 15:18
  • While I don't use Unity, it states that the `Vector3` type is for 3D vectors and points. http://docs.unity3d.com/Documentation/ScriptReference/Vector3.html On that page it lists `Normalize` and `CrossProduct` which you can use here if you really feel that `Distance` is not suitable. – grovesNL Mar 10 '14 at 15:23
  • what line is: 2 vectors OR 1 Origin vector + 1 direction vector, but in any case it's always 2 vectors. Vector3.Distance only substracts 2 vectors. WHILE I'm dealing with 4 vectors, 2 for each line. – MilitaryG Mar 10 '14 at 15:51
  • @MilitaryG: I understand. I was giving an example of distance between points, if you had already calculated the closest positions. I updated my post. – grovesNL Mar 10 '14 at 16:18
  • 1
    thank you very much it works as it should be :) well for this good answer I'll change my Question. Even if I found answers for my original Q BUT non of them worked correctly OR even was in Python, I would thumbs up if I could but I can't. hope check mark will satisfy. – MilitaryG Mar 10 '14 at 16:37