# How to creat a plane according 2 points

**URL:** <https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710>\
**Category:** Community\
**Tags:** markups-fiducials\
**Created:** [May 29, 2023, 2:36pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710 "2023-05-29T14:36:12Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![slicer365](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/slicer365/32/67549_2.png) [@slicer365](https://discourse.slicer.org/u/slicer365)\
**Post date:** [May 29, 2023, 2:36pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/1 "2023-05-29T14:36:12Z")

</div>

Dear friends

I know there are many ways in slicer to creat a plane

Then how to use two points to create a plane that passes through the middle point of these two points and is perpendicular to the line formed by the two points?

any way or script will be useful!

thankyou!

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/8/2/82f5a4989294cd80121c374f8366aebf97dd2cb9.jpeg)

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [May 29, 2023, 3:47pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/2 "2023-05-29T15:47:14Z")

</div>

> [@slicer365](#):
>
> any way or script will be useful!

It should be doable with vtkMath::Perpendiculars() with a direction vector relative to the middle point.

Calculate

- the middle point,
- a direction vector with either point relative to the middle point,
- the perpendiculars at the middle point using the direction vector.

By adding the perpendiculars to the middle point, you have the 3 points of a plane.

---

<div class="post-metadata">

**Author:** ![slicer365](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/slicer365/32/67549_2.png) [@slicer365](https://discourse.slicer.org/u/slicer365)\
**Post date:** [May 29, 2023, 4:25pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/3 "2023-05-29T16:25:54Z")

</div>

I would really appreciate it if you could help me write this slicer script

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [May 29, 2023, 5:16pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/4 "2023-05-29T17:16:40Z")

</div>

This should work when pasted in the Python console.

Add a fiducial node named ‘F’ with 2 points, and a plane named ‘P’ before.

```auto
def findMiddleCrossSectionPlane(p1, p2):
    middlePoint = ((p1[0] + p2[0]) / 2.0, (p1[1] + p2[1]) / 2.0, (p1[2] + p2[2]) / 2.0)
    directionVector = [0.0, 0.0, 0.0]
    vtk.vtkMath().Subtract(p2, middlePoint, directionVector)
    
    perpendicular1 = [0.0, 0.0, 0.0]
    perpendicular2 = [0.0, 0.0, 0.0]
    vtk.vtkMath().Perpendiculars(directionVector, perpendicular1, perpendicular2, 0.0)
    
    planePoint1 = [0.0, 0.0, 0.0]
    planePoint2 = [0.0, 0.0, 0.0]
    vtk.vtkMath().Add(middlePoint, perpendicular1, planePoint1)
    vtk.vtkMath().Add(middlePoint, perpendicular2, planePoint2)
    
    return (middlePoint, planePoint1, planePoint2)
    

# Add a fiducial node named 'F', and a plane named 'P'
f = slicer.util.getNode("F")
p1 = [0.0, 0.0, 0.0]
p2 = [0.0, 0.0, 0.0]
f.GetNthControlPointPositionWorld(0, p1)
f.GetNthControlPointPositionWorld(1, p2)

planePoints = findMiddleCrossSectionPlane(p1, p2)

# Just for visualisation.
plane = slicer.util.getNode("P")
plane.SetPlaneType(slicer.vtkMRMLMarkupsPlaneNode.PlaneType3Points)
plane.SetNthControlPointPositionWorld(0, planePoints[0])
plane.SetNthControlPointPositionWorld(1, planePoints[1])
plane.SetNthControlPointPositionWorld(2, planePoints[2])

```

---

<div class="post-metadata">

**Author:** ![slicer365](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/slicer365/32/67549_2.png) [@slicer365](https://discourse.slicer.org/u/slicer365)\
**Post date:** [May 29, 2023, 10:38pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/5 "2023-05-29T22:38:36Z")

</div>

> [@chir.set](#):
>
> `plane.SetNthControlPointPositionWorld(2, planePoints[2])`

thankyou very much! it works well!

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/9/3/938be53b7ffb4b3bc993528823105ed71bfd0e22.jpeg)

---

<div class="post-metadata">

**Author:** ![slicer365](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/slicer365/32/67549_2.png) [@slicer365](https://discourse.slicer.org/u/slicer365)\
**Post date:** [July 25, 2023, 11:13am UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/6 "2023-07-25T11:13:15Z")

</div>

Sorry to disturbe you,Mr！

In the last version slicer，the script does not work.

It shows the errors.

Would you like to help solve the problem?

 ![image](https://us1.discourse-cdn.com/flex002/uploads/slicer/original/3X/e/f/efac06cfbb1cb644c2351016860db8d561934ce3.png)

---

<div class="post-metadata">

**Author:** ![chir.set](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/chir.set/32/66982_2.png) [@chir.set](https://discourse.slicer.org/u/chir.set)\
**Post date:** [July 25, 2023, 3:48pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/7 "2023-07-25T15:48:48Z")

</div>

> [@slicer365](#):
>
> In the last version slicer，the script does not work.

Hi, I checked with 5.3.0-2023-07-23 r31895 / 98f1a5a on Linux and the script does work.

The error messages mean that the plane P is found but does not have any control point. I could reproduce the errors by adding a plane without any control point to the scene. Use one that you can see and interact with normally.

---

<div class="post-metadata">

**Author:** ![lili-yu22](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lili-yu22](https://discourse.slicer.org/u/lili-yu22)\
**Post date:** [February 18, 2025, 1:39pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/8 "2025-02-18T13:39:43Z")

</div>

can you give me a script about creating a plane through point N and perpendicular to P1,thank you very much.

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [February 18, 2025, 2:37pm UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/9 "2025-02-18T14:37:05Z")

</div>

The plane node has a mode where you can define the plane with a point and a normal, see `PlaneTypePointNormal`  
[https://apidocs.slicer.org/main/classvtkMRMLMarkupsPlaneNode.html#af83a7dac83b04668a4f2bae10245a7baa5bce04cda74677cd6404657aa0868783](https://apidocs.slicer.org/main/classvtkMRMLMarkupsPlaneNode.html#af83a7dac83b04668a4f2bae10245a7baa5bce04cda74677cd6404657aa0868783)

---

<div class="post-metadata">

**Author:** ![lili-yu22](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lili-yu22](https://discourse.slicer.org/u/lili-yu22)\
**Post date:** [February 19, 2025, 12:19am UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/10 "2025-02-19T00:19:46Z")

</div>

I’m really sorry, but I can’t understand that code and I’m not sure which part of the text I should copy. Could you please provide me with the specific text that I can copy directly? Thank you so much!

---

<div class="post-metadata">

**Author:** ![cpinter](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/cpinter/32/7995_2.png) [@cpinter](https://discourse.slicer.org/u/cpinter)\
**Post date:** [February 19, 2025, 9:55am UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/11 "2025-02-19T09:55:38Z")

</div>

I asked Copilot this question:  
“Write me a brief code snippet that creates a vtkMRMLMarkupsPlaneNode from a point and a normal vector using the PlaneTypePointNormal mode.”

It gave me the answer in a few seconds. It looks correct:

```auto
import slicer
import numpy as np

# Define the point and normal vector
point = np.array([10.0, 20.0, 30.0])
normal = np.array([0.0, 0.0, 1.0])

# Create a new vtkMRMLMarkupsPlaneNode
planeNode = slicer.mrmlScene.AddNewNodeByClass('vtkMRMLMarkupsPlaneNode')
planeNode.SetName('PlaneNode')

# Set the plane type to PlaneTypePointNormal
planeNode.SetPlaneType(slicer.vtkMRMLMarkupsPlaneNode.PlaneTypePointNormal)

# Set the point and normal vector
planeNode.SetOrigin(point)
planeNode.SetNormal(normal)

```

---

<div class="post-metadata">

**Author:** ![lili-yu22](https://avatars.discourse-cdn.com/v4/letter/l/dbc845/32.png) [@lili-yu22](https://discourse.slicer.org/u/lili-yu22)\
**Post date:** [February 20, 2025, 7:39am UTC](https://discourse.slicer.org/t/how-to-creat-a-plane-according-2-points/29710/12 "2025-02-20T07:39:49Z")

</div>

thank you very much 🌹
