Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an efficient algorithm to generate random points in general position in the plane?

I need to generate n random points in general position in the plane, i.e. no three points can lie on a same line. Points should have coordinates that are integers and lie inside a fixed square m x m. What would be the best algorithm to solve such a problem?

Update: square is aligned with the axes.

like image 826
Danylo Mysak Avatar asked Aug 04 '11 19:08

Danylo Mysak


2 Answers

Since they're integers within a square, treat them as points in a bitmap. When you add a point after the first, use Bresenham's algorithm to paint all pixels on each of the lines going through the new point and one of the old ones. When you need to add a new point, get a random location and check if it's clear; otherwise, try again. Since each pair of pixels gives a new line, and thus excludes up to m-2 other pixels, as the number of points grows you will have several random choices rejected before you find a good one. The advantage of the approach I'm suggesting is that you only pay the cost of going through all lines when you have a good choice, while rejecting a bad one is a very quick test.

(if you want to use a different definition of line, just replace Bresenham's with the appropriate algorithm)

like image 69
LaC Avatar answered Nov 15 '22 18:11

LaC


Can't see any way around checking each point as you add it, either by (a) running through all of the possible lines it could be on, or (b) eliminating conflicting points as you go along to reduce the possible locations for the next point. Of the two, (b) seems like it could give you better performance.

like image 34
John Avatar answered Nov 15 '22 19:11

John