Search code examples
androidmatrixlineintersectplane

Android: Find intersection points of a line and an image


Let's say I know two points that make up a line. I want to know the points in which this line goes through a matrix or a drawable of an image (both of which I have access to, so whichever one would be easier to use is welcome). Any ideas on how I can do this?


Solution

  • The starting point for you would be a line drawing algorithm. There are many line drawing algorithms. Check the wiki page.

    dx = x2 - x1
    dy = y2 - y1
    for x from x1 to x2 {
            y = y1 + (dy) * (x - x1)/(dx)
            plot(x, y)
    }
    

    Given 2 pointsas input to this algorithm, it will plot all the points in-between them. The points can be then mapped to the image in question.