Checking if points are contained within a shape
11 views (last 30 days)
Show older comments
Hi, I am working on an a script which aims to generate opposite points within a shape, from a random starting point (inside the shape). My strategy for generating points is as follows:
- Choose a random starting point (p_0)
- Find the boundary point (p_b) closest to p_0. E.g. r = p_0 + t (p_b - p_0)
- Using the vector generated from the p_b traverse until you reach the opposite point (p_opp) on line r.
- Choose random direction and repeat from 2. until the required number of points are generated.
My problem is that when I try to traverse the line to find the opposite point I end up exceeding the shape boundary. Is there any other way to check if a point is contained within a shape or force it to stay within the shape? I am currently using inpolygon; Also is it possible to do this in higher dimensions like R^n, n=1,2,3,...,i?
My implementation for finding opposite points on a line:
% Find the difference between the two points:
v = p1 - p2;
t = 1;
p3 = p1 + t*v;
while (inpolygon(p3(1),p3(2),b(:,1),b(:,2)))
p3 = p1 + t*v; % Extrapolate until boundary is reached
t = t + 1;
end
opp = p1 + (t-2)*v; % Find a point distance t-2 from the boundary. (To make sure its inside the shape)
2 Comments
Image Analyst
on 1 Feb 2016
How are you defining "opposite"? Two points that are on the perimeter and go through some arbitrary interior point are not necessarily on opposite sides of the shape. I think though if the interior point was the centroid, that you could claim that.
Answers (1)
Kelly Kearney
on 2 Feb 2016
Do you have the Mapping Toolbox? If so, polyxpoly may be helpful to calculate exactly where you'll hit the polygon boundary. The toolbox has several other polygon-related functions that could be useful.
0 Comments
See Also
Categories
Find more on Smoothing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!