
Creating points inside polygon boundary?
조회 수: 11 (최근 30일)
이전 댓글 표시
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
Ameer Hamza
2018년 5월 18일
Your dataset looks like this (except red dot).

How do you decide which point is inside or outside? Is red marker inside?
답변 (1개)
the cyclist
2018년 5월 18일
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
Walter Roberson
2018년 5월 18일
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))
참고 항목
카테고리
Help Center 및 File Exchange에서 Computational Geometry에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!