Convenient way to define a boundary

조회 수: 1 (최근 30일)
Joppy
Joppy 2017년 6월 21일
댓글: Paul White 2018년 1월 9일
Hi.
Question: Is there any convenient way to define a collection of points that may act as a MatlabFunction?
Context and more detail:
I am fiddling around with some billiard style problems. Basically i need a boundary (a box or space that particles exist in), that can be treated as a function which i can test for collisions with.
Currently i have created my boundaries by defining straight lines (y = mx + c), and then used polyfit on these lines so that i can treat them as a Matlab Function.
This works for the most part as i can successfully test when a particle has collided with a wall, but it is a bit clunky. Namely, some of the straight lines intersect other parts of the boundary, which causes problems in collision tests.
I am open for any ideas, suggestions or recommendations. Thank you!
Note:
I am developing original code for these 'simulations'. I am aware of some of the toolboxes available for this sort of thing, but again, trying to build a small simulation from scratch.
  댓글 수: 2
KSSV
KSSV 2017년 6월 21일
Question is not clear....you have a group of points..you want to draw a boundary enclosing all the points?
Joppy
Joppy 2017년 6월 21일
Yeah, basically that. In addition to this, i need to be able to check if a 'particle' collides with that boundary.
How would you code up a closed 2D shape?

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

채택된 답변

KSSV
KSSV 2017년 6월 21일
YOu can know whether the given point intersecting/ lying on the boundary using inpolygon. You can draw boundary enclosing the points in two ways.
  1. Exact boundary using the boundary function. If boundary doesn't exist (Introduced in R2014b) you can use convhull .
  2. You can draw a rectangle aka bounding box.
Check the below codes for each case.
N = 20 ;
x = rand(N,1) ;
y = rand(N,1) ;
%%Exact boundary
idx = boundary(x,y) ;
% idx = convhull(x,y) ;
% Check whether points intersecting boundary
[in,on] = inpolygon(x,y,x(idx),y(idx)) ;
figure
hold on
plot(x,y,'.b')
plot(x(idx),y(idx),'r')
plot(x(on),y(on),'Ok')
%%closed box
x0 = min(x) ; x1 = max(x) ;
y0 = min(y) ; y1 = max(y) ;
L = abs(x1-x0) ;
B = abs(y1-y0) ;
%
R = [x0 y0 ; x1 y0; x1 y1 ; x0 y1 ; x0 y0] ;
% Check whether points intersecting boundary
[in,on] = inpolygon(x,y,R(:,1),R(:,2)) ;
figure
hold on
plot(x,y,'.b')
plot(R(:,1),R(:,2),'r')
plot(x(on),y(on),'Ok')
  댓글 수: 1
Paul White
Paul White 2018년 1월 9일
I am using the Kalman Filter to 2D track a single moving object. I have used the rectangle() function to make a boundary around the video. Can I use inpolygon to detect when this randomly moving object touches the rectangle boundary?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Stochastic Differential Equation (SDE) Models에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by