Checking if points are contained within a shape

조회 수: 5 (최근 30일)
Raazi Rizvi
Raazi Rizvi 2016년 2월 1일
답변: Kelly Kearney 2016년 2월 2일
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
Image Analyst
Image Analyst 2016년 2월 1일
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 2016년 2월 2일
I am defining it as diametrically opposed for exampled for point (1,1,1) the opposite would be at (-1,-1,-1).

댓글을 달려면 로그인하십시오.

답변 (1개)

Kelly Kearney
Kelly Kearney 2016년 2월 2일
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.

카테고리

Help CenterFile Exchange에서 General Applications에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by