SRL Archive - Development

TPA from Line


mc_teo

2010-10-17 11:24:34

Right so, this procedure returns a TPA, containing a the points for a nice straight line joining any two TPoints.

As you can see, it is quite mathematical, and therefore slow if you want to update it constantly, to say, draw a line from a constant co-ordinate to the mouses position.

I originally made it to directly manipulate the pixels of bitmaps, but i think it can be added to simba easier this way, to give it a much needed speed boost.

if the name doesnt suit or conflicts feel free to change it.


function PointToPoint(start, finish: TPoint): TPointArray;
var
steep: boolean;
deltax, deltay, error, ystep, x1, y1, len: integer;
TPA: TPointArray;
begin
steep := (iAbs(finish.Y - start.Y) > iAbs(finish.X - start.X));
if steep then
begin
swap(start.X, start.Y);
swap(finish.X, finish.Y);
end;
if start.X > finish.X then
begin
swap(start.X, finish.X);
swap(start.Y, finish.Y);
end;
deltax := (finish.X - start.X);
deltay := iAbs(finish.Y - start.Y);
error := (deltax / 2);
y1 := start.Y
if (start.Y < finish.Y) then
ystep := 1
else
ystep := -1;
for x1 := start.X to finish.X do
begin
if steep then
begin
len := Length(TPA);
SetArrayLength(TPA, len+1);
TPA[len] := Point(y1, x1);
end else
begin
len := Length(TPA);
SetArrayLength(TPA, len+1);
TPA[len] := Point(x1, y1);
end;
error := error - deltay;
if error < 0 then
begin
y1 := y1 + ystep;
error := error + deltax;
end;
end;
Result := TPA;
end;

~mc_teo

marpis

2010-10-17 12:56:39

I wrote this to my area recorder which never got completed

function MakeLine(P1, P2: TPoint): TPointArray;
var
sina, cosa, hypo: extended;
R, maxR: integer;
begin
maxR := distance(P1.X, P1.Y, P2.X, P2.Y);
hypo := hypot(P2.X - P1.X, P2.Y - P1.Y);
sina := (P2.Y - P1.Y) div hypo;
cosa := (P2.x - P1.x) div hypo;
SetLength(Result, maxR + 1);

for R := 0 to maxR do
Result[R] := Point(P1.X + Round(cosa*R),
P1.Y + Round(sina*R));

//ClearDoubleTPA(Result);
end;