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

8 visualizaciones (últimos 30 días)
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])

Respuesta aceptada

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

Más respuestas (1)

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

Categorías

Más información sobre Graphics Object Properties en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by