Creating points inside polygon boundary?

Hello. I am trying to create a set number of points inside the boundary of an input polygon. i want to add it as a condition to my while loop to check if each point is within the polygon, if not then it should continues until it finds a point that is within, till i get my (n) desired number of points. My polygon is composed of (xb) vector contains x coordinates and and (yb) containts y coordinates which serve as vectors of the polygon points. Can anyone help me and suggest a solution to generate these points only inside this following polygon? Here is the script which i work on it
w=14.7 ; h=12.5 ; ds=0.3 ; n=500 ; THT=240;
% Biomimetic pattern parameters
a=3.7; b=0.67; phi = (sqrt(5)+1)/2;
% Constraints
Dm=(sqrt(w^2 + h^2)) + ds ; D1 = 0.75*THT;
xb=[-1186.53,1245.2,1232.51,1237.01,1002.74,946.118,954.654,1039.93,1047.05,901.728,905.519,873.292,700.736,464.325,380.736,334.014,258.286,199.453,-20.7446,-352.379,-587.244,-715.189,-867.852,-991.447,-1024.5,-930.733,-994.878,-1092.17,-1141.61,-1101.29,-1123.56,-1219.41,-1186.53];
yb=[-1085.44,-1056.98,-404.032,-76.0963,133.888,217.392,395.52,579.397,640.168,815.22,1270.41,1397.06,1539.57,1531.35,1522.04,1540.16,1562.23,1573.83,1571.67,1529.67,1488.86,1413.44,1320.06,1223.01,533.811,357.565,126.811,-11.116,-103.213,-288.39,-347.769,-435.997,-1085.44];
% Generation of points coordinates
r = zeros(1, n);
teta = zeros(1, n);
x = zeros(1, n);
y = zeros(1, n);
k = 1;
curpoint = 1;
while curpoint <= n
r(curpoint) = a*k^b;
%Minimum radial distance
if r(curpoint) > D1
teta(curpoint) = 2*pi*(phi^-2)*k;
[x(curpoint), y(curpoint)] = pol2cart(teta(curpoint), r(curpoint));
%Only add this point if it is far enough away from all others
if curpoint == 1 || all(hypot(x(curpoint) - x(1:curpoint-1), y(curpoint) - y(1:curpoint-1)) >= Dm)
curpoint = curpoint + 1;
end
end
k = k + 1;
end

2 Comments

Your dataset looks like this (except red dot).
How do you decide which point is inside or outside? Is red marker inside?
I want that generated points by (r) and (teta) equations should be created inside the above polygon defined by xb and yb vectors not outside thanks

Sign in to comment.

Answers (1)

I would use the rejection method. Define a square that encloses the entire polygon (e.g. by using the largest/smallest x & y values). Then generate random points inside that square. To see if each point lies within your polygon, use the built-in inpolygon function. Reject those points that are not within the polygon, and use what is left.

7 Comments

But i am generating points according to fermat spiral equations (r and teta) instead of random.point, could you add this to my previous code ?
Then when you do your spiral equations, before accepting a point, check to see if inpolygon() considers it to be inside the object.
Yes exactly, before add a point i want to check if it's inside my polygon, if it is add it, if not ignore it till i get the (n) points i want
inpolygon(x(curpoint), y(curpoint), xb, yb)
Could you please sir Roberson modify my code and add this statement in order to accept only points inside the polygon and thanks. Alot
if curpoint == 1 || (all(hypot(x(curpoint) - x(1:curpoint-1), y(curpoint) - y(1:curpoint-1)) >= Dm) && inpolygon(x(curpoint), y(curpoint), xb, yb))
thanks for you help

Sign in to comment.

Categories

Find more on Computational Geometry in Help Center and File Exchange

Tags

Asked:

on 18 May 2018

Commented:

on 18 May 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!