# vtkLine distanceToLine does not work as written in documentation?

**URL:** https://discourse.vtk.org/t/vtkline-distancetoline-does-not-work-as-written-in-documentation/14563
**Category:** Support
**Tags:** bug, python
**Created:** [September 12, 2024, 2:34pm UTC](https://discourse.vtk.org/t/vtkline-distancetoline-does-not-work-as-written-in-documentation/14563 "2024-09-12T14:34:23Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![ladela](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ladela/32/9204_2.png) [@ladela](https://discourse.vtk.org/u/ladela)
#### Post date: [September 12, 2024, 2:34pm UTC](https://discourse.vtk.org/t/vtkline-distancetoline-does-not-work-as-written-in-documentation/14563/1 "2024-09-12T14:34:23Z")

</div>

Hi everyone,  
I am using vtk for my project in python and I just found out the computation of distanceToLine does not work as written in the [documentation](https://vtk.org/doc/nightly/html/classvtkLine.html). (Snippet below.)

* * *

## Static Public Member Functions

static [vtkLine](https://vtk.org/doc/nightly/html/classvtkLine.html) \* [New](https://vtk.org/doc/nightly/html/classvtkLine.html#a495d3d8ae29aa2905df377ca6949dc27) ()  
…  
static double [DistanceToLine](https://vtk.org/doc/nightly/html/classvtkLine.html#a5a3a35665107c84f439c5513372b30db) (const double x[3], const double p1[3], const double p2[3], double &t, double closestPoint[3]=nullptr)  
Compute the distance of a point x to a finite line (p1,p2).

* * *

So according to the documentation, it should compute distance to a **finite** line, but in my code, it treats the line as **infinite**.

```auto
def computeDistanceToLine(point, line1, line2):
    distance_squared = vtk.vtkLine.DistanceToLine(point, line1, line2)
    return np.sqrt(distance_squared)

points = np.array([[1, 5, 3], [2, 4, 6], [7, 1, 3], [5, 9, 2], ....]) # example
distances = np.apply_along_axis(computeDistanceToLine, 1, points, line1=line1, line2=line2)
pointsThatAreFar = points[distances > 10]

```

If I run it on points in inside of meshes like on the figure below, it returns for the green line, result like that. I would expect that the blue ball will be filled with the red points (=pointsThatAreFar), but you can see that there is a part of the points missing in the direction of the green line.

 ![image](https://discourse.vtk.org/uploads/default/original/2X/9/9eb095383f08ecac9125225196b9f96a8c663eb2.jpeg)

Am I using the DistanceToLine function wrong? I can do a workaround, but it would be nice to use vtk functions, because of the speed.  
Any tips? Did I oversee something?

Thanks a lot!

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [September 12, 2024, 3:26pm UTC](https://discourse.vtk.org/t/vtkline-distancetoline-does-not-work-as-written-in-documentation/14563/2 "2024-09-12T15:26:49Z")

</div>

The different DistanceToLine() functions have different behaviors. For the one you are using, the documentation says this:

> [@](#):
>
> static double vtkLine::DistanceToLine (  
> const double _x_[3],  
> const double _p1_[3],  
> const double _p2_[3]  
> )
> 
> Determine the distance of the current vertex to the edge defined by the vertices provided.
> 
> Returns distance squared. Note: line is assumed infinite in extent.

So instead, you can try using this one, which says that it’s for finite lines:

> [@](#):
>
> static double vtkLine::DistanceToLine ( const double _x_[3],  
> const double _p1_[3],  
> const double _p2_[3],  
> double & _t_,  
> double _closestPoint_[3] = `nullptr`  
> )
> 
> Compute the distance of a point x to a finite line (p1,p2).
> 
> […] Also, the method returns the distance squared between x and the line (p1,p2).

You should be able to call it like this:

```python
    t = vtk.reference(0.0)
    distance_squared = vtk.vtkLine.DistanceToLine(point, line1, line2, t)

```
