Search code examples
c++cloopsreal-timepoint-in-polygon

Special polygonial for loop in two dimensional array


This is a bit tricky question for you computer scientists. Let's say that I have a two dimensional array/matrix of 100 by 100 entries, arr[i][j]. Where i and j goes from 0-99. This can be envisioned as a square of dots with each dot corresponding to a data value.

Now, If I define a 4 point polygon and know the indicies of the 4 points: Is it possible (is there an clever algorithm) to loop through only those entries in the matrix that lies inside of the 4-point polygon? That is, every value of i and j in the loop laps correspond to a value in arr[i][j] that is interesting (i and j is inside the 4-point poly).

Is this clear? I understand if it is difficult to understand.

Sincerely Yours


Solution

  • Sounds similar to triangle rasterization.

    There are a number of articles/tutorials you can find on it, such as this one:

    http://joshbeam.com/articles/triangle_rasterization/

    or this:

    http://sol.gfxile.net/tri/index.html

    With a 4-point poly just split it into 2 triangles.