Checking if points are contained within a shape

11 views (last 30 days)
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:
  1. Choose a random starting point (p_0)
  2. Find the boundary point (p_b) closest to p_0. E.g. r = p_0 + t (p_b - p_0)
  3. Using the vector generated from the p_b traverse until you reach the opposite point (p_opp) on line r.
  4. 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
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.
Raazi Rizvi
Raazi Rizvi on 2 Feb 2016
I am defining it as diametrically opposed for exampled for point (1,1,1) the opposite would be at (-1,-1,-1).

Sign in to comment.

Answers (1)

Kelly Kearney
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.

Products

Community Treasure Hunt

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

Start Hunting!