# Measure the vertical distance from a point to a line segment

**URL:** <https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093>\
**Category:** Support\
**Created:** [May 11, 2024, 6:33pm UTC](https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093 "2024-05-11T18:33:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivia](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@Olivia](https://discourse.slicer.org/u/Olivia)\
**Post date:** [May 11, 2024, 6:33pm UTC](https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093/1 "2024-05-11T18:33:31Z")

</div>

Hi, I am new to this software. I need to measure the minimum distance from a point to a line segment (aka perpendicular distance) in my measurements, but I haven’t found a suitable program that can do this, do you have any code that can help?  
Sincerely to the development team!  
Olivia

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [May 12, 2024, 5:01pm UTC](https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093/2 "2024-05-12T17:01:09Z")

</div>

If you copy this code in the python interactor of Slicer, it will create a random point and a random line and calculate their distance. This assumes that the line is infinitely long, the two points are just there to define it.

```auto
import numpy as np

# Make sure point markup exists
fiducialNode = slicer.mrmlScene.GetFirstNodeByName("F")
if fiducialNode is None:
  fiducialNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsFiducialNode", "F")

# Make sure point markup has at least one point
if fiducialNode.GetNumberOfControlPoints() < 1:
  pointCoordinates = np.random.rand(3) * 200 - 100
  fiducialNode.AddControlPoint(pointCoordinates[0], pointCoordinates[1], pointCoordinates[2])

# Make sure line markup exists
lineNode = slicer.mrmlScene.GetFirstNodeByName("L")
if lineNode is None:
  lineNode = slicer.mrmlScene.AddNewNodeByClass("vtkMRMLMarkupsLineNode", "L")

# Make sure line markup has at least two points
if lineNode.GetNumberOfControlPoints() < 2:
  pointCoordinates = np.random.rand(3) * 200 - 100
  lineNode.AddControlPoint(pointCoordinates[0], pointCoordinates[1], pointCoordinates[2])
  pointCoordinates = np.random.rand(3) * 200 - 100
  lineNode.AddControlPoint(pointCoordinates[0], pointCoordinates[1], pointCoordinates[2])

# Get point coordinates from markups
point = np.array(fiducialNode.GetNthControlPointPositionWorld(0))
line_p1 = np.array(lineNode.GetNthControlPointPositionWorld(0))
line_p2 = np.array(lineNode.GetNthControlPointPositionWorld(1))

# Calculate distance
lineVector = line_p2 - line_p1
target = np.dot(point - line_p1, lineVector) / np.dot(lineVector, lineVector)
projectionPoint = target * lineVector + line_p1
distance = np.linalg.norm(point - projectionPoint)

print(f"Distance from point to line = {distance}")

```

---

<div class="post-metadata">

**Author:** ![Olivia](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@Olivia](https://discourse.slicer.org/u/Olivia)\
**Post date:** [May 15, 2024, 2:07pm UTC](https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093/3 "2024-05-15T14:07:13Z")

</div>

Thank you so much for your help, I tried this and it worked. But there is a little extra request, since the points and line segments I need to measure are defined by myself on the model, is it possible to change the code so that I can substitute the points and line segments I need to measure? (Sorry for needing your help as I am very bad with computers) Sincerely thank you again!

---

<div class="post-metadata">

**Author:** ![ungi](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/ungi/32/78573_2.png) [@ungi](https://discourse.slicer.org/u/ungi)\
**Post date:** [May 15, 2024, 2:31pm UTC](https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093/4 "2024-05-15T14:31:21Z")

</div>

If you change the name of the point list “F” and the name of the line “L” in the code, then it should work with any point and line.

---

<div class="post-metadata">

**Author:** ![Olivia](https://avatars.discourse-cdn.com/v4/letter/o/b9e5f3/32.png) [@Olivia](https://discourse.slicer.org/u/Olivia)\
**Post date:** [May 15, 2024, 3:18pm UTC](https://discourse.slicer.org/t/measure-the-vertical-distance-from-a-point-to-a-line-segment/36093/5 "2024-05-15T15:18:58Z")

</div>

This did work and I believe it solved my problem, thank you very much to you and the rest of the development team people.
