- Home /

# How to convert Python in to C#? Maths or find Shortest distance between two skew lines in 3D space.

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.

Also I found this Q/A before but it doesn't work correctly.

This is definitely not a Unity question, you should try on some C# forum...

@suribe - actually his question is how to translate the vector math functions in the Phyton code into Unity vector math functions. This seems like a reasonable Unity question since it is primarily about Unity's vector class (and maybe their matrix classes).

**Answer** by sdgd
·
Mar 10, 2014 at 04:56 PM

ok someone helped me and solved my original problem:

```
//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;
}
}
```

posting this as it probably will help others.

To give credit where credit is due, this code appears to be pulled from the Math3d class in the Unity Wiki.

Interesting, ... didn't know it was there, ... +1

could be someone that helped me was getting data from there, ...

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.