To fill your rectangle handle it as closed convex polygon (almost the same as triangle filling)
-
order your points to match winding rule
so there are lines AB BC CD DA or reverse
-
create left and right buffer
address is
y
-coordinate, its an array ofx
-positions and if needed also array ofcolor,texture coordinates,...
. for starters:`int buf_x0[ys],buf_x1[ys];`
where
ys
is screeny
-resolution -
implement any draw line algorithm
but instead of draw to screen just store
x
coordinate of pixel to buffer.- instead of:
setpixel(x,y,color);
do:buf_x?[y]=x;
.
Which buffer is the destination depends on the line
Y
direction- if
dy<0
then fillbuff_x0
- if
dy>0
then fillbuff_x1
- if
dy==0
thenbuf_x0[y]=min(x)
andbuf_x1[y]=max(x)
Beware you have to sort the line endpoints by x coordinate before rasterizing to avoid seams/holes in meshes caused by different pixels produced for reversed endpoints lines.
- instead of:
-
apply this line algorithm to all border lines of polygon (AB,BC,CD,DA)
after this the buffers contains start and end
x
-positions of your horizontal lines -
fill the rectangle on screen
for (y=min(Ay,By,Cy,Dy);y<=max(Ay,By,Cy,Dy);y++) draw_horizontal_line(y,buf_x0[y],buf_x1[y],color);
Image for clarity (taken from my lectures on low level computer graphics)
image description:
- vertical rectangles represents the border buffers
buf_x0[],buf_x1[]
- clockwise winding rule ensures the destination buffer. If its coded properly than
buf_x0[y] <= buf_x1[y]
so draw of horizontal line colapses to singlefor
loop
Also here simple C++ example of mine for this