0

demo image

As you can see, there is a 10 by 10 grids. They are stored in a variable matrix.

The matrix stores an 10 by 10 2 dimensional array. In each element, the data structure looks like:

{ "distance": distance, "topLeft": (xmin, ymin), "bottomRight": (xmax, ymax) }

We also know X1(x1,y1), X2(x2, y2).

Question:

How can I find out all grids which have the path(X1 to X2) inside?

E.g. matrix[0][7] has a part of the path, thus it should be included in the result array.

Update 1

One thing to mention that: this code will be running during a video stream on RPi, so I prefer the algorithm does need to go through each pixel to figure out whether a cell includes the path or not.

Update2:

One important thing I need to mention: the X1, X2 are the points in an 224 by 224 image.

I'm now using the Bresenham's line algorithm. The code generates X,y coordinates on my image, but what I Really need to know is which grid is crossed by the line. So how can I utilise the Bresenham's line algorithm to get the crossed grids?

Update 3

Here is the Python Code I implemented for the Bresenham's_line_algorithm

 def _find_crossed_cells(self, point0, point1, matrix) -> List(tuple):
        path_cells = []
        x0, y0 = point0
        x1, y1 = point1
        dx = abs(x1 - x0)
        sx = 1 if x0 < x1 else -1
        dy = abs(y1 - y0)
        sy = 1 if y0 < y1 else -1
        error = dx + dy

        while True:
            path_cells.append((x0, y0))
            if x0 == x1 and y0 == y1:
                break
            e2 = 2 * error

            if e2 >= dy:
                if x0 == x1:
                    break
                error = error + dy
                x0 = x0 + sx
            
            if e2 <= dx:
                if y0 == y1:
                    break
                error += dx
                y0 += sy
        
        return path_cells

As you can see this code can only find a list of x,y coordinates(224 by 224), but I need to find out a lit of crossed cells(10 by 10 coordinates). So how can I achieve this?

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
Franva
  • 6,565
  • 23
  • 79
  • 144
  • 2
    So basically a [line drawing algorithm](https://en.m.wikipedia.org/wiki/Line_drawing_algorithm)? – B Remmelzwaal Apr 20 '23 at 13:54
  • that exactly requires a line drawing (or rasterization) algorithm. – Christoph Rackwitz Apr 20 '23 at 13:59
  • @BRemmelzwaal thanks man, have had a look and they look similar, but I'd say my question still different from that. in that line drawing algorithm, it draws line to each discrete pixel, but in my case, I have grids which have area(topLeft and bottomRight). How am I going to check whether a part of the path falls in this cell? – Franva Apr 20 '23 at 14:12
  • 1
    https://en.wikipedia.org/wiki/Rasterisation also deals with calculating which pixels (pixels with area) are *intersected* by a polygon (lines are trivial polygons). – Christoph Rackwitz Apr 20 '23 at 14:20
  • thanks @ChristophRackwitz, I will have a look – Franva Apr 20 '23 at 14:21
  • 1
    Since you have the top left and bottom right coordinates of each grid, you can get all the coordinates of the grid with the step of your grid. Then refer to this answer : https://stackoverflow.com/a/3304724/17457846 – HMH1013 Apr 20 '23 at 14:22
  • https://stackoverflow.com/a/25017020/844416 – MBo Apr 20 '23 at 15:17
  • Thanks guys for all your line algorithms recommended. But there's a problem. Please see my update 2 @HMH1013 – Franva Apr 20 '23 at 15:28
  • I can only tag one in a comment. @MBo – Franva Apr 20 '23 at 15:30
  • @BRemmelzwaal very close but my question needs a further step to solve. Please see my Update 2. – Franva Apr 20 '23 at 15:32
  • @ChristophRackwitz hopefully my update 2 makes sense to you. – Franva Apr 20 '23 at 15:32
  • @Franva Sorry, I don't understand what do you mean "can only tag one in a comment" . In my mind, that algo finds `the crossed grids` - cells – MBo Apr 20 '23 at 15:35
  • @MBo oh ignore that part please. I explained what I mean in the `Update 3`. Hope it makes sense to you. – Franva Apr 21 '23 at 08:00
  • @Franva Bresenham gives **points** and draws corresponding cells to provide instant line, but misses some cells really being intersected, linked Woo algo gives **cells** touched by your line. Choose what is better for your purposes – MBo Apr 21 '23 at 08:52
  • @MBo thanks for your reply man. I guess my difficulty is not missing touched grids/cells. My difficulty is, the image has 224 * 224 resolution, and the grids/cells have 10 * 10 which cover on top of the image. This means the 10 by 10 grids has same size as my image. The Woo and Bresenham algorithms find the coordinates on image which has values between 0 to 223, but the answer I need to find is the x,y for grids which has value between 0 to 9. Hope my explanation makes sense to you. – Franva Apr 22 '23 at 09:12
  • 1
    cellx = x * 10 // 224 ? – MBo Apr 22 '23 at 09:34
  • thanks @MBo , yes now all dots are connected~!! I think your approach should work. I have had a look at your previous post and the code that post refers to, I cannot figure out the Python code from that. Appreciate if you could enlighten me with the Python code implementation. Cheers~! – Franva Apr 23 '23 at 12:45

0 Answers0