Create a cycle out of scattered points

2024/10/10 10:29:40

I know this sounds trivial, but my head is refusing to give an algorithm for this.

I have a bunch of points scattered on a 2-D plane and want to store them in a list such that they create a ring. The points do not belong on a cycle.

enter image description here

Start from the first point in the list (red in this pic) and sequentially add the rest based on their distance.

Since I cannot answer my question I will post here a possible answer.

This is an approach that seems to do the job. V.pos holds the positions of the nodes and distance() is just a function that returns the Euclidean distance between two points. A faster approach would also delete the next_node after appending it to the ring so that you don't have to go through the already connected points

ring = [nodes[0]]while len(ring) < len(nodes): minl=99999for i in range(len(nodes)):dist = distance(V.pos[ring[-1]],V.pos[nodes[i]])if dist<minl and nodes[i] not in ring: minl = distnext_node = nodes[i]ring.append(next_node)

Answer

Here's an idea that will give okay-ish results if your point cloud is already ring-shaped like your example:

  • Determine a centre point; this can either be the centre of gravity of all points or the centre of the bounding box.
  • Represent all points in radial coordinates (radius, angle) with reference to the centre
  • Sort by angle

This will, of course, produce jagged stars for random clouds, but it is not clear, what exactly a "ring" is. You could probably use this as a first draft and start swapping nodes if that gives you a shorter overall distance. Maybe this simple code is all you need short of implementing the minimum distance over all nodes of a graph.

Anayway, here goes:

import mathpoints = [(0, 4), (2, 2), ...]     # original points in Cartesian coords    
radial = []                        # list of tuples(index, angle)# find centre point (centre of gravity)
x0, y0 = 0, 0
for x, y in points:x0 += xy0 += yx0 = 1.0 * x0 / len(points)
y0 = 1.0 * y0 / len(points)# calculate angles
for i, p in enumerate(points):x, y = pphi = math.atan2(y - y0, x - x0)radial += [(i, phi)]# sort by angle
def rsort(a, b):"""Sorting criterion for angles"""return cmp(a[1], b[1])radial.sort(rsort)# extract indices
ring = [a[0] for a in radial]
https://en.xdnf.cn/q/118465.html

Related Q&A

Python Dictionary w/ 2 Keys?

Can I have a python dictionary with 2 same keys, but with different elements?

Tkinter throwing a KeyError when trying to change frames

Im learning tkinter off of the Sentdex tutorials and I into a problem when trying to change pages. My compiler throws something about a KeyError that it doesnt give whenever I change the button on the …

How to send messages to other clients only in the sequence of adding clients?

https://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/sever.pyhttps://github.com/kakkarotssj/ChatApplication/blob/master/GroupChat/client.pyWhen server starts, and suppose three clients …

Dictionary keeps getting overwritten in each iteration of for-loop

import randomo=[,,!,@,#,$,%,^,&,*,(,),,_,=,+,/,[] q=[1,2,3,4,5,6,7,8,9,0]for i in top_25:wordDic ={i: random.choice(o)+random.choice(q)} print(wordDic)(top_25 is an array of words, and the random.c…

Python socket communication with HP print server [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question appears to be off-topic because it lacks sufficient information to diagnose the proble…

Why are torch.version.cuda and deviceQuery reporting different versions?

I have a doubt about the CUDA version installed on my system and being effectively used by my software. I have done some research online but could not find a solution to my doubt. The issue which helpe…

How to have 2 advertisements in BLE(BlueTooth Low Energy)?

Im working on BLE advertisement. Id like to know if its possible to have 2 advertisements in BLE. I need to have both service data and manufacturer data. Im using Python. The code is based on https://g…

How to get Python script to write to existing sheet

I am writing a Python script and stuck on one of the early steps. I am opening an existing sheet and want to add two columns so I have used this:#import the writer import xlwt #import the reader impor…

Python3 - getting the sum of a particular row from all the files [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Selenium Headers in Python

how can I change my headers in Selenium like I do in requests ,I want to change my cookies and headers . from myCookie import cookie from selenium import webdriver from selenium.webdriver.common.by imp…