intersections between rays and 2D square grid

9 views (last 30 days)
anto
anto on 5 Jan 2023
Commented: cr on 5 Jan 2023
I'd like to calculate the intersections between a square grid and lines for a problem which looks like this:
Let's say we have 1 point for each side of the square in which the line arrives and starts. Those lines are in black in the figure below.
Let's say also that we have a square grid 3x3 with its cells are represented in red in the figure below.
So far I've coded the positions of the points of start and finish of the lines, so I got a Nx2 matrix, 'start' which represents the coordinates x,y of every starting point, and another Nx2 matrix which has all the corresponding finishing coordinates, 'finish'.
My problem requires calculating those intersections, and storing/ordering them so i can use those intersections in a system of equations.
Does anybody have any ideas on how to do this?
Any help would be greatly appreciated.

Answers (1)

cr
cr on 5 Jan 2023
Each line has an equation, and in this case several lines have simple equations like y=c (horizontal lines) or x=c (vertical lines)
For lines forming sides of square use eqn for line passing through two points (if not sure just do web search). With 4 of these equations (1 for each side) and the eqns of other lines (horizontals and verticals) simple substitution would yield XY intersection points.
e.g. eqn of bottom left side is y = 0.5-x. This intersects left vertical (line eqn x=.33) at (.33,0.5-0.33) = (.33,0.17).
Henceforth.
  2 Comments
anto
anto on 5 Jan 2023
Ok, so I will work on a code that calculates the equations of straight lines, distinguishes when they are vertical, horizontal or oblique and does the system of equations with every one of the vertical grid lines and horizontal grid lines.
And for storing and ordering the interceptions? Do you have any ideas? for example we could call the grid boxes like this, and assign every intersection to its cell
cr
cr on 5 Jan 2023
Not sure if I understood your concern, but you can store intersections xy coordinates in separate variables, say x,y. I suggest you account with lines rather than spaces between them.

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!