Can I find the intersection points of a line and a box?

15 次查看(过去 30 天)
I'm currently doing a university project which requires me to find the points of collision between a line and a box.
This is part of a motion prediction program. My inputs are two 2D co-ordinates of an object (two detected positions which occur one after the other) from which I plan to connect using a line. From this line, I will find the point of intersection with the box.
So far, I have written a some code that occasionally works. This is because I've been unable to represent the boundary as a box and I've just used the top and right edge of the table as limits.
My main question is, is it possible to represent the four sides of the box as a boundary and find out where the point of collision is according to the direction that the object is headed in?
Thanks
Sandy
Please note: I'm using the 2010 version of MATLAB
Here is a copy of my code
clc
clear
%Define Table
x1=0;
y1=0;
x2=200;
y2=100;
%Point 1
A_x=50;
A_y=30;
%Point 2
B_x=60;
B_y=60;
%Draw the points
x_p=[A_x,B_x];
y_p=[A_y,B_y];
%Equation of line between 2 points
d=[B_x-A_x;B_y-A_y];
%Calculate time of collision at wall in x direction
collisionx=(x2-B_x)/d(1,1);
%Calculate time of collision at wall in y direction
collisiony=(y2-B_y)/d(2,1);
%Find point of collision
if collisionx>collisiony
p=[B_x;B_y]+collisiony*d
else
p=[B_x;B_y]+collisionx*d
end
%Plot all points and table
plot(x_p,y_p,'x',p(1),p(2),'o')
axis([-20 220 -20 120])
rectangle('Position',[x1,y1,x2,y2])

采纳的回答

Sean de Wolski
Sean de Wolski 2012-9-27
I am sure a tool here FEX:query:polygon+intersect will likely help you a lot!

更多回答(1 个)

Sandy
Sandy 2012-9-28
Thank you very much for the hint - I found a function which did almost exactly what I need!
2D Polygon edges intersection:

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by