필터 지우기
필터 지우기

Determine if a point lies between two lines

조회 수: 11 (최근 30일)
Dave
Dave 2016년 5월 10일
편집: Chad Greene 2016년 5월 11일
Assume there are two lines and each line is defined by two points.(e.g. (x1a,y1a) and (x2a,y2a) for line 1, (x1b,y1b) and (x2b,y2b) for line 2.). Consider also a square centered at the origin therefore these points are inside the square. How can we test whether a point inside the square (xu,yu) lies between these lines? Lines can be either parallel or intersecting (in such case, lines intersect outside the square).
%line 1
x1a=-2.5;
y1a=-1.8;
x2a=0.8;
y2a=-2.5;
%line2
x1b=-2.5;
y1b=- 1.4;
x2b=2.5;
y2b=1.4;
%square
xs=[-2.5 -2.5 2.5 2.5 -2.5] ;
ys=[-2.5 2.5 2.5 -2.5 -2.5];
figure;
plot([x1a x2a],[y1a y2a])
hold on
plot([x1b x2b],[y1b y2b])
hold on
plot(xs,ys,'-k','Linewidth',2)
axis([-3 3 -3 3])
Assume the lines and square created above. How can we exactly test if (2,-2) is between lines inside the square but (-1,1) is not?
  댓글 수: 3
Roger Stafford
Roger Stafford 2016년 5월 11일
If the lines intersect, how do you define 'between'? The lines, infinitely extended, divide two-dimensional space into four sections and the point must lie within one of these.
Dave
Dave 2016년 5월 11일
Question is revised.

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

채택된 답변

Chad Greene
Chad Greene 2016년 5월 11일
You can use inpolygon.
  댓글 수: 2
Dave
Dave 2016년 5월 11일
How can we define the region illustrated in the problem above using inpolygon?
Chad Greene
Chad Greene 2016년 5월 11일
편집: Chad Greene 2016년 5월 11일
Try this:
polygonx = [x1a x2a x1b x2b];
polygony = [y1a y2a y1b y2b];
k = convhull(polygonx,polygony);
plot(polygonx(k),polygony(k),'r-','linewidth',3)
% 100 random points:
randx = randn(100,1);
randy = randn(100,1)-1;
plot(randx,randy,'bx')
% Find random points inside the polygon:
in = inpolygon(randx,randy,polygonx(k),polygony(k));
plot(randx(in),randy(in),'ro')

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Hypothesis Tests에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by