I would like to create a filled rectangle from four points using Bresenham's line-algorithm or similar to get smooth sides. I came across this answer on Stack Overflow [link] that works perfectly for creating a line between two points. How can I use/modify this function to create a filled rectangle? Is there any other algorithm that might fit my needs better?
Asked
Active
Viewed 1,867 times
-1
-
A rectangle will always fit in pixels, you don't need to smooth anithing, may be you mean a quadrilateral? – Gusman Jul 26 '17 at 21:50
-
1@Gus : A rotated Rectangle is still a rectangle. – TaW Jul 26 '17 at 22:01
-
You could first create the rectangle and then do a floodfill. It really depends on which kind of primitives you are targetting.. – TaW Jul 26 '17 at 22:03
-
@TaW Yes, you're right... that's the problem of uising always AABB XD, you forget about rotation. – Gusman Jul 26 '17 at 22:05
-
For a simple floodfill routine see [here](https://stackoverflow.com/questions/45290317/how-can-i-fill-part-of-image-with-color/45299760#45299760)! – TaW Jul 27 '17 at 05:33
-
using floodfill for this is slow (but simple) In the old days we used convex polygon filling like in the linked duplicate answer. You create boundary intervals with any line rasterisation and then just render horizontal lines ... – Spektre Jul 27 '17 at 05:54
1 Answers
0
For arbitrary rotated rectangles:
Note: this description follows wiki Wu's implementation with for loop along OX axis, but it is more effective to draw edges along OY axis and fill horizontal lines.
Sort vertices by X-coordinate
Make intervals on horizontal axis separated by vertice
For every interval select upper and lower edge
Simultaneously execute Wu's antaliasing drawing for upper and lower edge
For upper edge draw upper semi-transparent pixel
draw: plot(ipart(intery) , x, rfpart(intery))
and ignore bottom pixel, use its position as start of fill line
LineFrom(ipart(intery)+1, x)
For lower edge draw bottom semi-transparent pixel and use upper as end of fill line
Reinitialize edge drawing when corner is reached.

MBo
- 77,366
- 5
- 53
- 86