- #1
Edi
- 177
- 1
First year learning programming here.
I am trying to write a line drawing algorithm that would be, perhaps, faster than algorithms used today (that would be nice, if I would turn out to be smart and accomplish something nice) so I wrote this piece of code (currently it only draws lines where difference in y is larger than diff in x, but that's just a manner of toppling everything to its side, so to speak :) ):
The idea behind this code is this:
I simply divide difference in y-axis with diff x and I get a number of pixels to draw in y-axis per one iteration of x-axis (as a real value that is rounded). And it works.
But there are some things bothering me:
First I tried running it without the .lineTo, but with the commented-out loop of canvas.pixels and it was about as fast as Brezenhem.
Then i commented the loop out and added the .lineTo part (to only draw the vertical line) - now it works something like 6 times faster then Brezenhem (depending at the angle of the line) - that's quite interesting.
As someone explained to me - the .lineto function is optimized at assembler level, that's why it is faster.. is that right?
If I optimize my simple canvas.pixels vertical line loop at assembler level, it would work even faster than now, because there would only be that simple loop, not the whole, optimized, whatever is hiding under .lineTo, wouldn't it? ( But I have no idea how to do that yet)What is the fastest line algorithm used in games and visual programs? (every 3D model is made from triangles which are drawn using line algorithms)
If i get the fastest algorithm and write it in a code, it still would need to be optimized in assembler lever to really get its speed.. or what?
The optimization in assembler really confused me.
I am trying to write a line drawing algorithm that would be, perhaps, faster than algorithms used today (that would be nice, if I would turn out to be smart and accomplish something nice) so I wrote this piece of code (currently it only draws lines where difference in y is larger than diff in x, but that's just a manner of toppling everything to its side, so to speak :) ):
Code:
procedure TForm1.Brezenhem(x1, y1, x2, y2: integer);
var Pn, dx, dy, xn, yn, xi, yi,i,j, yprev, sign:integer;
var dyddx:single;
var start, stop, elap: cardinal;
begin
start:=GetTickCount; i:=0;
while (i<200) do
begin
image1.canvas.Pen.color:=RGB(150,0,255); // mmm, Lilac - such a great color :)
if (x2-x1<0) then Pn:=-1 else Pn:=1;
dx:=x2-x1+Pn;
dy:=y2-y1;
if abs(dy)>abs(dx) then
begin
xi:=x1;
dyddx:=dy/ dx;
if (y2>y1) then sign:=1 else sign:=-1;
yprev:=y1-sign;
while(xi<>x1+dx) do // main loop starts here. "x1" (and y1) is the center of the canvas.
begin
yn:=y1+round(dyddx*(xi+Pn-x1));
{
j:=0;
while (j<abs(yprev-yn)) do
begin
image1.Canvas.pixels[xi, yn-j*sign]:=RGB(255,200,0);
j:=j+1;
end;
}
j:=abs(yprev-yn);
image1.canvas.MoveTo(xi,yn);
image1.canvas.LineTo(xi,yn-j*sign);
xi:=xi+Pn;
yprev:=yn;
end;
end;
i:=i+1;
end;
stop:=GetTickCount;
elap:=stop-start;
edit1.Text:=IntToStr(elap);
end;
The idea behind this code is this:
I simply divide difference in y-axis with diff x and I get a number of pixels to draw in y-axis per one iteration of x-axis (as a real value that is rounded). And it works.
But there are some things bothering me:
First I tried running it without the .lineTo, but with the commented-out loop of canvas.pixels and it was about as fast as Brezenhem.
Then i commented the loop out and added the .lineTo part (to only draw the vertical line) - now it works something like 6 times faster then Brezenhem (depending at the angle of the line) - that's quite interesting.
As someone explained to me - the .lineto function is optimized at assembler level, that's why it is faster.. is that right?
If I optimize my simple canvas.pixels vertical line loop at assembler level, it would work even faster than now, because there would only be that simple loop, not the whole, optimized, whatever is hiding under .lineTo, wouldn't it? ( But I have no idea how to do that yet)What is the fastest line algorithm used in games and visual programs? (every 3D model is made from triangles which are drawn using line algorithms)
If i get the fastest algorithm and write it in a code, it still would need to be optimized in assembler lever to really get its speed.. or what?
The optimization in assembler really confused me.
Last edited: