Method of random point in region

Source: Internet
Author: User

This paper studies how to randomly distribute points in the region, and derives from the visualization of the < algorithm visualizing Algorithms>, which is of great significance in CAE and graphic science.
http://www.bilibili.com/video/av2182749/

For example, in the area of 3*1 random Point 1000, if each point is the coordinates are randomly given, the result may not be uniform.
As demonstrated by RANDOMPOINTS.M.

1 function randompoints () 2 xmax = 3; 3 ymax = 1; 4 N = 500; 5 x = Xmax*rand (n,1); 6 y = Ymax*rand (n,1); 7 figure (1); 8 on; 9 title (' Random sampling '); ten axis equal;  Axis ([0,xmax,0,ymax]);  A Set (GCA, ' XTick ', [], ' ytick ', []);  - End
RANDOMPOINTS.M

The next step is to introduce a method called Best-candidate sampling, and the basic idea is that no point needs to be found in a number of (numcandidates) points (such as 10) and the closest distance to the known point.

The pseudocode for each additional point is as follows:

Bestcandidate= 0;  = 0; Loop i=1: numcandidates= [rand (), Rand ()];  = Distance (Findclosest (samples,c), c); % Gets the nearest distance in C and known points if (d > Bestdistance)  = D;  = c; % update best points}}

This method can obtain a more uniform distribution, but when the number of points is relatively long, a lot of computational amount is needed. In addition, the larger the numcandidates, the more evenly distributed. But the same amount of computational growth is not negligible. The calculation results can be referenced in code BESTCANDIDATE.M

1function Bestcandidate ()2Xmax = 3;3Ymax = 1;4N = 500;5x = zeros (n,1); y = zeros (n,1);6X (1) = rand (*xmax);% initial placement of a point7Y (1) = rand (*ymax);% may be able to decorate multiple points on the border Oh8Numcandidates = 10;9  forII = 2: NTenXcand = rand (numcandidates,1) *xmax; % randomly selected Numcand.a candidate. OneYcand = rand (numcandidates,1) *ymax; Abestdistance = 0; -      forJJ = 1: Numcandidates  -Dists = sqrt ((x (1: II-1)-xcand (JJ)). ^2 + (Y (1: II-1)-ycand (JJ)). ^2);% known points and the distance of the JJ number candidate points thedist = min (dists);% get the smallest distance -         if(Dist > Bestdistance)% if the distance of C point is greater than any point -Bestdistance = dist;% update Max distance -Bestcandidate = JJ;% update Best points +         End -     End +X (ii) = Xcand (bestcandidate); Y (ii) = Ycand (bestcandidate);% Insert a point A End atFigure (1); -Plot (x, Y, '. ', ' markersize ', 5, ' Markeredgecolor ', ' R '); Hold on; -Title (' Best-candidate sampling '); -Axis equal; -Axis ([0,xmax,0,ymax]); - Set(GCA, ' XTick ', [], ' ytick ', []); in End
bestcandidate

The results are as follows:

The last introduced method is called Poisson disk sampling Poisson disk sampling, which was presented 06-07 years ago. This method works better because it excludes two points with a radius less than a fixed value of R. The basic ideas are as follows:


1. Set the random initial point and background grid. To ensure that there is at most one point in each grid, the grid size is R/2PI and R is the minimum distance between any two points.
2. Loop until there are no selectable points
A) Select one from a known selectable point and create a ring outside it (inner diameter r, outside diameter 2r)
b) Select a candidate point from the ring (typically, k=30). Detects the minimum distance between a candidate and a known point (which can be reduced by using a background mesh) if the distance is less than the Set value R, the candidate is discarded; Otherwise, add a new point.
c) If you cannot increase the candidate, you can turn the center point to not selectable.

The Poisson Disk method has many improvements and extensions, its computation is already O (n lg N), and even has a fast algorithm to O (n); But for the code demo, we don't consider optimizations or background grids. As demonstrated by POISSONDISK.M.

1function Poissondisk ()2Close All;clear ALL;CLC;3Xmax = 3;4Ymax = 1;5N = 5000; % a relatively large number,sufficient to store points6R = sqrt (3*1/500/pi*2); % minimum radius, so the design is about 500a point7x = zeros (n,1); y = zeros (n,1);8Ava = zeros (n,1);9X (1) = rand (*xmax);% initial placement of a pointTenY (1) = rand (*ymax);% may be able to decorate multiple points on the border Oh OneAva (1) = 1; CNT = 1; ANumcandidates = 30; -while (SUM (AVA) ~=0)% can be determined by the point -Cava =Find(Ava (1: CNT));% find available points theCnum = Cava (Floor (rand () *length (Cava) +1));% Choose from a selectable -Rcand = (rand (numcandidates,1) +1) *r; % randomly selected Numcand.a candidate. -Thetacand = rand (numcandidates,1) *2*pi; -Xcand = Rcand.*cos (Thetacand) + x (cnum); +Ycand = Rcand.*sin (thetacand) + y (cnum); - % Find out if there is a suitable point +Flag = 0; A      forJJ = 1: Numcandidates  atDists = sqrt ((x (1: CNT)-xcand (JJ)). ^2 + (Y (1: CNT)-ycand (JJ)). ^2);% known points and the distance of the JJ number candidate points -dist = min (dists);% get the smallest distance -         if(Dist > R && xcand (JJ) <= xmax && Ycand (JJ) <= ymax ... -&& Xcand (JJ) >= 0 && Ycand (JJ) >= 0)% if the distance of C point is greater than any point -X (cnt+1) = Xcand (JJ); Y (cnt+1) = Ycand (JJ); -Ava (cnt+1) = 1; inCNT = cnt + 1; -flag = 1; to              Break; +         End -     End the% The following code can run to indicate that there are no available in the Numcandidates candidate.Therefore, the point cnum becomes not selectable *     ifFlag = = 0 $Ava (Cnum) = 0;Panax Notoginseng     End - End theFigure (1); +Plot (x, Y, '. ', ' markersize ', 5, ' Markeredgecolor ', ' R '); Hold on; ATitle (' Poisson Disk sampling '); theAxis equal; +Axis ([0,xmax,0,ymax]); - Set(GCA, ' XTick ', [], ' ytick ', []); $ End
POISSONDISK.M


Extended reading:
Jittered Grid: Divides the area into blocks and then fills the blocks with arbitrary points. The same effect is not very good.
http://devmag.org.za/2009/05/03/poisson-disk-sampling/

Method of random point in region

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.