0
\$\begingroup\$

I'm developing an application in Unity3D where a user can define 4 pixel points on screen, and what I would like to do is fill this shape with pixels using Unity's SetPixel.

While I can do this easily for square or rectangular shapes, I was wondering if there is any efficient code or algorithm to do this for more irregular four-sided shapes, such as trapezoids etc.

Note : Filling the shape with a mesh is not an option.

So far the best option I can think of is to draw the outline, then draw a line from each pixel point of every outline, to every other outline point, but I'm not sure that's very efficient.

\$\endgroup\$
1
\$\begingroup\$

You can use the even-odd algorithm. In summary it does the following.

  1. Loop thru each scan line
  2. Intersect the scan line with each polygon edge
  3. Sort the intersection points by x value
  4. Fill interior pixels between intersection points using the even-odd rule to determine interior points

Note, you have to guard against degenerate cases like intersecting with vertices and horizontal lines.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.