I have got a four 2d vertices A B C D of rotated rectangle, I need to rasterize/draw it (efficiently) in pixelbufer with setpixel(x,y,color)
how to do it?
i was trying with some code like
// convertilg a b c d do up down left right,
// calculating some dx_left dx_right on y--
// etc (frustrating on special cases when there are 2 up_y vertices in same line etc)
for(;;)
{
drawhorizontalline(y, xstart, xend, color);
if(y==downy) break;
y--;
xstart+=dxstart;
xend+=dxend;
if(y==lefty) dxstart = dxright;
if(y==righty) dxend = dxleft;
}
but it is most frustrating (terribly bug prone and most frustrating) i am really tired of debuging this all day yesterday and i need to find maybe some working code rather than to try to debug this
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
-resolutionimplement any draw line algorithm
but instead of draw to screen just store
x
coordinate of pixel to buffer.setpixel(x,y,color);
do:buf_x?[y]=x;
.Which buffer is the destination depends on the line
Y
directiondy<0
then fillbuff_x0
dy>0
then fillbuff_x1
if
dy==0
thenbuf_x0[y]=min(x)
andbuf_x1[y]=max(x)
after this the buffers contains start and end
x
-positions of your horizontal linesfill the rectangle on screen
Image for clarity (taken from my lectures on low level computer graphics)
image description:
buf_x0[],buf_x1[]
buf_x0[y] <= buf_x1[y]
so draw of horizontal line colapses to singlefor
loopAlso here simple C++ example of mine for this