# Python script error on sorting points in a clockwise order

**URL:** <https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394>\
**Category:** Developers\
**Created:** [December 21, 2016, 12:47pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394 "2016-12-21T12:47:16Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jcp](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jcp](https://forum.dynamobim.com/u/Jcp)\
**Post date:** [December 21, 2016, 12:47pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/1 "2016-12-21T12:47:16Z")

</div>

Hi all,

I would like to use Python to re-organise in a clock wise order all the nested lists of points within one list.

I have several nested lists of points sorted by level and I was trying to use python to sort points on each level in a clock wise order.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/6/06f975e6a1e56ffce7a0b6454ddeed63634866a6.jpg)

```
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

import math

pt = IN[0]
newTupla = []

for tupla in range (len(pt)):
	newList = []
	sortedList = []

x = 0
y = 0

# calcs the medium point
for i in pt:
	x += i[0]
	y += i[1]
x = x / len(pt)
y = y / len(pt)
med = [x,y]

#calcs the arc tangent for each point

for j in pt[tupla]:
	rads = math.atan2(-1*(j[1]-med[1]),j[0]-med[0])
	rads %= 2*math.pi
	degs = math.degrees(rads)
	newPoint = (j[0],j[1],j[2],degs)
	newList.append(newPoint)

#sorts the list using the angle
newList = sorted(newList, key = lambda x : x[3])

#removes the angle from the list
for j in newList:
	newPoint = (j[0],j[1],j[2])
	sortedList.append(newPoint)
	

OUT = sortedList   

```

Does someone have a solution for that?  
Thank you for your time !

---

<div class="post-metadata">

**Author:** ![Mostafa\_El\_Ayoubi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mostafa_el_ayoubi/32/7815_2.png) [@Mostafa\_El\_Ayoubi](https://forum.dynamobim.com/u/Mostafa_El_Ayoubi)\
**Post date:** [December 21, 2016, 7:10pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/2 "2016-12-21T19:10:53Z")

</div>

Hi @Jcp ,  
could you provide more infos about what you’re trying to achieve? Clockwise arount what axis? and what is the goal?

---

<div class="post-metadata">

**Author:** ![Jcp](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jcp](https://forum.dynamobim.com/u/Jcp)\
**Post date:** [December 22, 2016, 11:06am UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/3 "2016-12-22T11:06:32Z")

</div>

Hi @Mostafa_El_Ayoubi,

I am trying to go through a parametric design exercise to generate building envelope options by translating design constrains on a set of rules in dynamo.

The screenshot bellow shows the envelope of the building represented by their vertex and every nested list of points is the intersection of this lines with the levels defined in Revit.  
As next steep I would like to create a Polycurve.ByPoints per level and is here where I need to sort points on each level in a clock wise order.  
My goal is to place adaptive components between levels based on 4 reference points

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/4/94817344b7b30998318c04f24168aa4a5fae4e3d.jpg)

Back to the issue of sorting this set of points with python as you can see in the python code I am trying to calculate the medium point then angle of each point to the medium point and sort the list using the angle.

---

<div class="post-metadata">

**Author:** ![jostein\_olsen](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jostein_olsen/32/8570_2.png) [@jostein\_olsen](https://forum.dynamobim.com/u/jostein_olsen)\
**Post date:** [December 22, 2016, 11:57am UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/4 "2016-12-22T11:57:50Z")

</div>

The Ampersand package has a Point.SortAzimutally node, maybe that will work for you.

---

<div class="post-metadata">

**Author:** ![Jcp](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jcp](https://forum.dynamobim.com/u/Jcp)\
**Post date:** [December 22, 2016, 12:50pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/5 "2016-12-22T12:50:25Z")

</div>

Hi @jostein_olsen,

Thanks for the suggestion but for some reason Point.SortAzimutally node is not reorganising my list of points.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/d/0d2bc4f2e6db8e1fe9192d40025ccecc0683bf47.png)

---

<div class="post-metadata">

**Author:** ![Mostafa\_El\_Ayoubi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mostafa_el_ayoubi/32/7815_2.png) [@Mostafa\_El\_Ayoubi](https://forum.dynamobim.com/u/Mostafa_El_Ayoubi)\
**Post date:** [December 22, 2016, 2:01pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/6 "2016-12-22T14:01:29Z")

</div>

@Jcp  
maybe something like this?

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/5/053ce1edbdea797ac52c5e6697d69ff36f7d8b86.png)

I first get the average point, then create a “reference vector” that allows to get angles to sort the points.

That’s what it looks like before sorting :

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/9/9961a036aa4d623a35ee6d79e875461238e7ee1a.png)

---

<div class="post-metadata">

**Author:** ![Jcp](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jcp](https://forum.dynamobim.com/u/Jcp)\
**Post date:** [December 22, 2016, 2:19pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/7 "2016-12-22T14:19:31Z")

</div>

@Mostafa_El_Ayoubi

I can make it work in python in 1 list of points with the same principle you are using in dynamo. My issue is how to do it with multiple nested lists within a list.

Is it possible to make it work with Math.average?

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/5/45971373f779698d505d62a87f7d9b1c94b42f1e.png)

---

<div class="post-metadata">

**Author:** ![Mostafa\_El\_Ayoubi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mostafa_el_ayoubi/32/7815_2.png) [@Mostafa\_El\_Ayoubi](https://forum.dynamobim.com/u/Mostafa_El_Ayoubi)\
**Post date:** [December 22, 2016, 2:47pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/8 "2016-12-22T14:47:21Z")

</div>

@Jcp  
here’s an example with a list structure similar to yours :

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/9/2/9293e86b465c09eb5cee61d24d11fd4387f33076.png)

just make sure you set the lacings to “longest” as shown in the screenshot above

---

<div class="post-metadata">

**Author:** ![Jcp](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jcp](https://forum.dynamobim.com/u/Jcp)\
**Post date:** [December 22, 2016, 3:15pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/9 "2016-12-22T15:15:23Z")

</div>

I cannot flatten the list, each of my nested lists are sort by level and I need to keep this structure for the next steps.

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/b/5/b5bc5f0f9fefd243c0492165300bde5ba19c57c1.png)

---

<div class="post-metadata">

**Author:** ![Mostafa\_El\_Ayoubi](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/mostafa_el_ayoubi/32/7815_2.png) [@Mostafa\_El\_Ayoubi](https://forum.dynamobim.com/u/Mostafa_El_Ayoubi)\
**Post date:** [December 22, 2016, 3:18pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/10 "2016-12-22T15:18:57Z")

</div>

Yeah I got that . When the lacing of flatten node is set to longest , you keep your first level of lists. If you take a look at my previous screenshot, or try it yourself, you’all see that .

---

<div class="post-metadata">

**Author:** ![Jcp](https://avatars.discourse-cdn.com/v4/letter/j/51bf81/32.png) [@Jcp](https://forum.dynamobim.com/u/Jcp)\
**Post date:** [December 22, 2016, 3:33pm UTC](https://forum.dynamobim.com/t/python-script-error-on-sorting-points-in-a-clockwise-order/8394/11 "2016-12-22T15:33:07Z")

</div>

It works perfect!!

 ![](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/c/ac2a4000b76fdbd065c233759124af487e4857fd.png)

Thank you @Mostafa_El_Ayoubi
