Cornelius henry's Weblog

April 15, 2010

algoritma garis DDA

Filed under: grafika komputer — cornelius henry @ 5:09 am

digitaal diferensial analyser adalah pembentukan garis berdasarkan perhitungan dx maupun dy, menggunakan rumus dy = m . dx

garis yang dibuat menggunakan endpoint, yaitu titik awal dan titik akhir, setiap koordinat titik yang membentuk garis diperoleh dari perhitungan, kemudian di konversikan menjadi nilai-nilai integer.

dengan source code

unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
ExtCtrls, StdCtrls, Grids, Buttons;
type
{ TForm1 }
TForm1 = class(TForm)
btntutup: TBitBtn;
btnproses: TBitBtn;
Image1: TImage;
rgagar: TRadioGroup;
StringGrid1: TStringGrid;
procedure btnprosesClick(Sender: TObject);
procedure btntutupClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure DDA(xa, ya, xb, yb : Integer);
procedure Image1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
X1,X2,Y1,Y2:Integer;
tergambar:Boolean;
implementation
{ TForm1 }
procedure TForm1.DDA(xa, ya, xb, yb: Integer);
var
dx, dy, step, k: Integer;
xincrement, Yincrement, x, y:Real;
begin
xa:=10;
ya:=10;
xb:=17;
yb:=16;
dx:=xb-xa;
dy:=yb-ya;
x:=round(xa);
y:=round(ya);
if (abs(dx)>abs(dy)) then
step := (abs(dx))
else
step := (abs(dy));
Xincrement:=(dx/step);
Yincrement:=(dy/step);
Image1.Canvas.Pixels[trunc(x),trunc(y)]:=clBlack;
for k:=0 to step do
begin
x+=xincrement;
y+=Yincrement;
StringGrid1.RowCount:=StringGrid1.RowCount+1;
StringGrid1.Cells[0,stringGrid1.RowCount-1]:=IntToStr(k);
StringGrid1.Cells[1,stringGrid1.RowCount-1]:=FloatToStr(x);
StringGrid1.Cells[2,stringGrid1.RowCount-1]:=FloatToStr(y);
StringGrid1.Cells[3,stringGrid1.RowCount-1]:=FloatToStr(round(x));
StringGrid1.Cells[4,stringGrid1.RowCount-1]:=FloatToStr(round(y));
Image1.Canvas.Pixels[trunc(x),trunc(y)]:=clBlack;
end;
end;
procedure TForm1.Image1Click(Sender: TObject);
begin
end;
procedure TForm1.btntutupClick(Sender: TObject);
begin
Close();
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:=’K';
StringGrid1.Cells[1,0]:=’X';
StringGrid1.Cells[2,0]:=’Y';
StringGrid1.Cells[3,0]:=’X(Bulat)’;
StringGrid1.Cells[4,0]:=’Y(Bulat)’;
StringGrid1.Cells[1,1]:=”;
StringGrid1.Cells[2,1]:=”;
StringGrid1.Cells[3,1]:=”;
StringGrid1.Cells[4,1]:=”;
Image1.Canvas.Rectangle(0,0,Image1.Width,Image1.Height);
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
tergambar:=true;
X1:=X;
Y1:=Y;
end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
tergambar:=False;
btnprosesClick(Sender);
X2:=X;
Y2:=Y;
if rgagar.ItemIndex=0 then
DDA(X1,Y1,X2,Y2);
begin
Image1.Canvas.MoveTo(X1,Y1);
Image1.Canvas.LineTo(X2,Y2);
end;
end;
procedure TForm1.btnprosesClick(Sender: TObject);
begin
tergambar:=False;
Image1.Canvas.Rectangle(0,0,Image1.Width, Image1.Height);
end;
initialization
{$I unit1.lrs}
end.

unit Unit1;
{$mode objfpc}{$H+}
interface
uses  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,  ExtCtrls, StdCtrls, Grids, Buttons;
type
{ TForm1 }
TForm1 = class(TForm)    btntutup: TBitBtn;    btnproses: TBitBtn;    Image1: TImage;    rgagar: TRadioGroup;    StringGrid1: TStringGrid;    procedure btnprosesClick(Sender: TObject);    procedure btntutupClick(Sender: TObject);    procedure FormCreate(Sender: TObject);    procedure DDA(xa, ya, xb, yb : Integer);    procedure Image1Click(Sender: TObject);    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;      Shift: TShiftState; X, Y: Integer);  private    { private declarations }  public    { public declarations }  end;
var  Form1: TForm1;   X1,X2,Y1,Y2:Integer;  tergambar:Boolean;implementation
{ TForm1 }
procedure TForm1.DDA(xa, ya, xb, yb: Integer);var   dx, dy, step, k: Integer;   xincrement, Yincrement, x, y:Real;begin     xa:=10;     ya:=10;     xb:=17;     yb:=16;     dx:=xb-xa;     dy:=yb-ya;     x:=round(xa);     y:=round(ya);     if (abs(dx)>abs(dy)) then        step := (abs(dx))     else         step := (abs(dy));     Xincrement:=(dx/step);     Yincrement:=(dy/step);     Image1.Canvas.Pixels[trunc(x),trunc(y)]:=clBlack;     for k:=0 to step do         begin         x+=xincrement;         y+=Yincrement;         StringGrid1.RowCount:=StringGrid1.RowCount+1;         StringGrid1.Cells[0,stringGrid1.RowCount-1]:=IntToStr(k);         StringGrid1.Cells[1,stringGrid1.RowCount-1]:=FloatToStr(x);         StringGrid1.Cells[2,stringGrid1.RowCount-1]:=FloatToStr(y);         StringGrid1.Cells[3,stringGrid1.RowCount-1]:=FloatToStr(round(x));         StringGrid1.Cells[4,stringGrid1.RowCount-1]:=FloatToStr(round(y));         Image1.Canvas.Pixels[trunc(x),trunc(y)]:=clBlack;     end;end;
procedure TForm1.Image1Click(Sender: TObject);begin
end;

procedure TForm1.btntutupClick(Sender: TObject);begin  Close();end;
procedure TForm1.FormCreate(Sender: TObject);begin  StringGrid1.Cells[0,0]:=’K';  StringGrid1.Cells[1,0]:=’X';  StringGrid1.Cells[2,0]:=’Y';  StringGrid1.Cells[3,0]:=’X(Bulat)’;  StringGrid1.Cells[4,0]:=’Y(Bulat)’;  StringGrid1.Cells[1,1]:=”;  StringGrid1.Cells[2,1]:=”;  StringGrid1.Cells[3,1]:=”;  StringGrid1.Cells[4,1]:=”;  Image1.Canvas.Rectangle(0,0,Image1.Width,Image1.Height);end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  tergambar:=true;  X1:=X;  Y1:=Y;end;
procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;  Shift: TShiftState; X, Y: Integer);begin  tergambar:=False;  btnprosesClick(Sender);  X2:=X;  Y2:=Y;
if rgagar.ItemIndex=0 then     DDA(X1,Y1,X2,Y2);     begin          Image1.Canvas.MoveTo(X1,Y1);          Image1.Canvas.LineTo(X2,Y2);     end;end;
procedure TForm1.btnprosesClick(Sender: TObject);begin     tergambar:=False;     Image1.Canvas.Rectangle(0,0,Image1.Width, Image1.Height);end;
initialization  {$I unit1.lrs}
end.

cornelius henry

123070237

plug_8

Advertisement

1 Comment »

  1. [...] algoritma garis DDA [...]

    Pingback by Tugas Grafika Komputer | Rafisanctuary's Blog — April 22, 2011 @ 2:23 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.