Hi all,
If any of you are interested, I have created a very simple and fast 16.16 integer based fixed-point 2d line drawer.
This means the code uses numbers with 16 bit integer part, and 16 bit fractional part.
The idea is definitely not new and I didn't invent it but here it is for anyone to use
Code:
Type
{...........................................................................}
TFixed = LongInt;
TInterpolant = Record
Value : TFixed;
Delta : TFixed;
End;
{..............................................................................}
{..............................................................................}
Function CalculateInterpolant(v1,v2,span : LongInt) : TInterpolant;
Begin
Result.Value := v1 * 65536;
Result.Delta := 0;
If span > 0 Then Result.Delta := ((v2 - v1) * 65536) Div span;
End;
{..............................................................................}
{..............................................................................}
Procedure SDLSurface_DrawLine2d(Const s : PSDL_Surface;
x1,y1,x2,y2 : Integer;
Const r,g,b,a : Byte;
Const SetPixel : TSetPixel);
Var
x,y : Integer;
dx,dy : Integer;
span : Integer;
i : Integer;
ix,iy : TInterpolant;
Begin
dx := Abs(x2 - x1);
dy := Abs(y2 - y1);
Case dx >= dy Of
False : span := dy;
True : span := dx;
End;
ix := CalculateInterpolant(x1,x2,span);
iy := CalculateInterpolant(y1,y2,span);
For i := 0 To span Do
Begin
x := ix.Value Shr 16;
y := iy.Value Shr 16;
SetPixel(s,x,y,r,g,b,a);
Inc(ix.Value,ix.Delta);
Inc(iy.Value,iy.Delta);
End;
End;
Enjoy
cheers,
Paul
Bookmarks