This is a question about the minimum distance from the Python dot.

Asked 2 years ago, Updated 2 years ago, 16 views

I want to draw a random dot using random and turtlen, but I want to set the minimum interval between the dots. What should I do?

python

2022-09-20 22:28

2 Answers

Assume that it is two-dimensional.

When we get random x, y coordinates, Comparative operations should be included for each point already marked.

If you compare the coordinates of the point you're trying to draw with the distance of each coordinate that's already drawn (Pass), you're going to draw the point again and again.

The distance between points is not a problem because it is easily obtained by the great Pythagorean theorem.

If there are not many points to be stamped, it can be implemented simply by brute-force method. No problem.

from random import randint
from math import sqrt

N = 100 #Number of points to be obtained
w, h = 512, 512 # size of viewport
min_dist = 5 # Minimum distance
points = [] # Results

def dist(p1, p2):
    return sqrt(((p1[0] - p2[0]) ** 2) + ((p1[1] - p2[1]) ** 2))

while len(points) < N:
    candidate = (randint(0, w), randint(0, h))

    dists = map(lambda point: dist(point, candidate), points)
    passed = map(lambda d: d > min_dist, dists)

    if all(passed):
        points.append(candidate)

print(points)

However, is a problem when you have to draw a large number of dots beyond a certain level.

A comparative operation should be included for the points already taken each time.

Assuming that you perform with N randomly sampled points,

The comparative computation is

(n-1) + (n-2) + ... + + 3 + 2 + 1 = O(n^2)

Therefore, time complexity is high.

In the above case, only N was compared regardless of whether it was taken or not.

It's serious if you have to draw N dots unconditionally. This is because there is no guarantee that random points will always pass.

It's also a problem that the more moles there are, the less room there are for new things. It's going to be a lot slower in this case, probabilistically. (In some cases, you may not be able to get it.)

To solve this problem, you need to use algorithms or devise special strategies.

A tree structure for spatial coordinates can be used to properly sample the number of points to perform comparative operations I think we can use a variation of spatial division algorithms (R-tree, KD-tree, etc.).

If you want to find more evenly distributed points, We can also set up a strategy to divide the viewport area for N points in advance.

You can think of a way to divide the area evenly by considering the size of the viewport in consideration of the minimum buffer, and then position it randomly only inside the buffer.

Divided conquest could be a way.

Anyway, if you want to draw a lot of dots, you'll have to think about it in many ways.


2022-09-20 22:28

random.uniform(a, b)

When you use the , a random value is generated between a and b

I think you can transfer the minimum interval to the value of a.


2022-09-20 22:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.