Python Forum
[Geometry] Finding centers of each grid in pointgrid
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Geometry] Finding centers of each grid in pointgrid
#1
[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 :)
Reply
#2
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.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#3
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])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  finding point in m grid jenya56 0 827 Feb-06-2022, 09:00 PM
Last Post: jenya56
  Import Text, output curve geometry Alyner 0 1,992 Feb-03-2020, 03:05 AM
Last Post: Alyner
  How do I apply a Gaussian blur to a particular edge of geometry in Matplotlib? hbolandi 0 2,016 Feb-02-2020, 06:08 PM
Last Post: hbolandi
  terminal/console geometry Skaperen 12 9,549 Feb-02-2017, 02:09 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020