Python Forum
[Geometry] Finding centers of each grid in pointgrid - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [Geometry] Finding centers of each grid in pointgrid (/thread-4511.html)



[Geometry] Finding centers of each grid in pointgrid - andre - Aug-22-2017

[Geometry] How to find centerpoints in a grid of independent (but symmetrically spread) points?

I am using Python in Dynamo which is an extension to the Autodesk modelling software Revit.
This means (as far as i know) that I am locked to IronPython, and thus cannot use external modules.
(I have not really read up too much on this, but its the conclusion I have drawn from working with IronPython so far)

My problem is that I have a list of points which represents a few grids of points, and I want to find the centerpoint for each grid.

How do I proceed to do that?

Image from Dynamo (Its a list of lists with points. The points are grouped in list per horizontal line as you can see in the image below)
i.imgur.com/1RVvknl.png

This is basically just an array in the python code.
Any suggestions for a solution?
I am not very experienced with Python, but I get around.

Thanks for your response :)


RE: [Geometry] Finding centers of each grid in pointgrid - sparkz_alot - Aug-22-2017

First off, please do not post images of code, copy and paste the code between the appropriate tags. Refer to the Help document on BBCode for instructions. Secondly, please post the Python code you have so far, again using the code tags.


RE: [Geometry] Finding centers of each grid in pointgrid - andre - Aug-22-2017

Hello and thanks for your answer.
I am sorry for the horrible question, hope this is a better representation of it:

The Image function does not work, so I am unable to visually represent what I have so far. (The points and lines in 3D space)

The code so far gets input a list of lines, which create a few grids of lines, and the code finds the lines intersectionspoints to create a pointgrid.

I cannot wrap my head around how to find the centerpoints of each grid (see picture).

The
dataEnteringNode = IN
and the
OUT
are needed for the "Node" to read information into the pythonscript, and output the result back into the Dynamo program.

Dynamo is a visual programming software used for modeling, which has an implemented IronPython node.

Do you have any clue?

Here is the code:
import clr
clr.AddReference('ProtoGeometry')
clr.AddReference('DSCoreNodes') 
import DSCore
from DSCore import *
from Autodesk.DesignScript.Geometry import *
#The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
OUT = []

def line_intersects(p1sp,p1ep,p2sp,p2ep):
	xdiff = (p1sp.X - p1ep.X, p2sp.X - p2ep.X)
	ydiff = (p1sp.Y - p1ep.Y, p2sp.Y - p2ep.Y)
	
	def det(a1,a2,b1,b2):
		return a1 * b2 - a2 * b1
	
	div = det(xdiff[0],xdiff[1],ydiff[0],ydiff[1])
	if div <> 0:
		d = (det(p1sp.X,p1sp.Y,p1ep.X,p1ep.Y), det(p2sp.X,p2sp.Y,p2ep.X,p2ep.Y))
		x = det(d[0],d[1], xdiff[0],xdiff[1]) / div
		y = det(d[0],d[1], ydiff[0],ydiff[1]) / div
		if x > p1sp.X and x < p1ep.X and y > p2sp.Y and y < p2ep.Y:
			return Point.ByCoordinates(x,y)
			
for l1 in IN[0]:
	p1sp = l1.StartPoint 
	p1ep = l1.EndPoint
	data = []
	for l2 in IN[0]:
		if l1 <> l2:
			p2sp = l2.StartPoint
			p2ep = l2.EndPoint
			if  p1sp.X < p2sp.X and p1ep.X > p2ep.X  or p1sp.Y > p2sp.Y and p1ep.Y < p2ep.Y:
				if not (line_intersects(p1sp,p1ep,p2sp,p2ep) is None):
					data.append(line_intersects(p1sp,p1ep,p2sp,p2ep))
	if data.Count > 0:
		OUT.append([l1,data])