Sabtu, 14 Mei 2016

Tutorial Algoritma Lingkaran dan Ellipse pada Delphi 7

Assalamualaikum...


shape lingkaran dan sekitarnya diabaikan saja, karena belum ada codingannya :)

full code :


unit lingkaran;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    EditXC: TEdit;
    EditYC: TEdit;
    EditRX: TEdit;
    EditRY: TEdit;
    Button1: TButton;
    btn1way: TButton;
    btn2way: TButton;
    btn4way: TButton;
    btn8way: TButton;
    btnHapus: TButton;
    btnExit: TButton;
    Shape1: TShape;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    ScrollBox1: TScrollBox;
    Image1: TImage;
    CheckBox3: TCheckBox;
    CheckBox4: TCheckBox;
    CheckBox5: TCheckBox;
    CheckBox6: TCheckBox;
    CheckBox7: TCheckBox;
    CheckBox8: TCheckBox;
    btnAnimasi: TButton;
    procedure FormShow(Sender: TObject);
    procedure btnHapusClick(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure btnExitClick(Sender: TObject);
    procedure btn1wayClick(Sender: TObject);
    procedure btn2wayClick(Sender: TObject);
    procedure btn4wayClick(Sender: TObject);
    procedure btn8wayClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  rx, ry, xc, yc, c, x, y, sudut :  double;
  PosX1, PosY1, PosX2, PosY2, PosX3, PosY3, PosX4, PosY4, PosX5, PosY5, PosX6, PosY6, PosX7, PosY7, PosX8, PosY8 : double;

implementation
uses Math;
{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
begin
  Image1.Canvas.Pen.Color := clRed;
  Image1.Canvas.Pen.Style := psDash;
  Image1.Canvas.MoveTo(Image1.Width div 2, 0);
  Image1.Canvas.LineTo(Image1.Width div 2,Image1.Height);
  Image1.Canvas.MoveTo(0, Image1.Height div 2);
  Image1.Canvas.LineTo(Image1.Width,Image1.Height div 2);

  Image1.Canvas.MoveTo(0,0);
  Image1.Canvas.LineTo(Image1.Width,Image1.Height);
  Image1.Canvas.MoveTo(0,Image1.Height);
  Image1.Canvas.LineTo(Image1.Width,0);
end;

procedure TForm1.btnHapusClick(Sender: TObject);
begin
  //button hapus
  Image1.Canvas.Pen.Color := clWhite;
  Image1.Canvas.Brush.Color := clWhite;
  Image1.Canvas.Brush.Style := bsSolid;
  Image1.Canvas.Pen.Style := psSolid;
  Image1.Canvas.Rectangle(0,0,Image1.Width,Image1.Height);
  FormShow(nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
   sudut := 0;
   rx := StrToFloat(EditRX.Text);
   ry := StrToFloat(EditRY.Text);
   xc := StrToFloat(EditXC.Text);
   yc := StrToFloat(EditYC.Text);

   c := 1 / max(rx,ry);

   while (sudut < 2 * pi) do
     begin
      sudut := sudut + c;
      x := rx * cos(sudut);
      y := ry * sin(sudut);

      PosX1 := xc + x;
      PosY1 := yc + y;

      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX1)),(Image1.Height div 2 - round(PosY1))] := clBlack;
    end;
end;

procedure TForm1.btnExitClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.btn1wayClick(Sender: TObject);
begin
  sudut := 0;
   rx := StrToFloat(EditRX.Text);
   ry := StrToFloat(EditRY.Text);
   xc := StrToFloat(EditXC.Text);
   yc := StrToFloat(EditYC.Text);

   c := 1 / max(rx,ry);

   while (sudut < 2 * pi) do
     begin
      sudut := sudut + c;
      x := rx * cos(sudut);
      y := ry * sin(sudut);

      PosX1 := xc + x;
      PosY1 := yc + y;

      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX1)),(Image1.Height div 2 - round(PosY1))] := clBlue;
      Image1.Repaint;
      sleep(10);
    end;
end;

procedure TForm1.btn2wayClick(Sender: TObject);
begin
  sudut := 0;
   rx := StrToFloat(EditRX.Text);
   ry := StrToFloat(EditRY.Text);
   xc := StrToFloat(EditXC.Text);
   yc := StrToFloat(EditYC.Text);

   c := 1 / max(rx,ry);

   while (sudut < pi) do
     begin
      sudut := sudut + c;
      x := rx * cos(sudut);
      y := ry * sin(sudut);

      PosX1 := xc + x;
      PosY1 := yc + y;

      PosX2 := xc - x;
      PosY2 := yc - y;

      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX1)),(Image1.Height div 2 - round(PosY1))] := clGreen;
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX2)),(Image1.Height div 2 - round(PosY2))] := clGreen;
      Image1.Repaint;
      sleep(10);
    end;
end;

procedure TForm1.btn4wayClick(Sender: TObject);
begin
  sudut := 0;
   rx := StrToFloat(EditRX.Text);
   ry := StrToFloat(EditRY.Text);
   xc := StrToFloat(EditXC.Text);
   yc := StrToFloat(EditYC.Text);

   c := 1 / max(rx,ry);

   while (sudut < pi/2) do
     begin
      sudut := sudut + c;
      x := rx * cos(sudut);
      y := ry * sin(sudut);

      PosX1 := xc + x;
      PosY1 := yc + y;

      PosX2 := xc - x;
      PosY2 := yc + y;

      PosX3 := xc - x;
      PosY3 := yc - y;

      PosX4 := xc + x;
      PosY4 := yc - y;

      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX1)),(Image1.Height div 2 - round(PosY1))] := clPurple;
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX2)),(Image1.Height div 2 - round(PosY2))] := clPurple;
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX3)),(Image1.Height div 2 - round(PosY3))] := clPurple;
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX4)),(Image1.Height div 2 - round(PosY4))] := clPurple;
      Image1.Repaint;
      sleep(10);
    end;
end;

procedure TForm1.btn8wayClick(Sender: TObject);
begin
  sudut := 0;
   rx := StrToFloat(EditRX.Text);
   ry := StrToFloat(EditRY.Text);
   xc := StrToFloat(EditXC.Text);
   yc := StrToFloat(EditYC.Text);

   c := 1 / max(rx,ry);

   while (sudut < pi/4) do
     begin
      sudut := sudut + c;
      x := rx * cos(sudut);
      y := ry * sin(sudut);

      PosX1 := xc + x;
      PosY1 := yc + y;

      PosX2 := xc + x;
      PosY2 := yc - y;

      PosX3 := xc - x;
      PosY3 := yc + y;

      PosX4 := xc - x;
      PosY4 := yc - y;

      PosX5 := xc + y;
      PosY5 := yc + x;

      PosX6 := xc + y;
      PosY6 := yc - x;

      PosX7 := xc - y;
      PosY7 := yc + x;

      PosX8 := xc - y;
      PosY8 := yc - x;

      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX1)),(Image1.Height div 2 - round(PosY1))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX2)),(Image1.Height div 2 - round(PosY2))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX3)),(Image1.Height div 2 - round(PosY3))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX4)),(Image1.Height div 2 - round(PosY4))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX5)),(Image1.Height div 2 - round(PosY5))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX6)),(Image1.Height div 2 - round(PosY6))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX7)),(Image1.Height div 2 - round(PosY7))] := RGB(0,153,153);
      Image1.Canvas.Pixels[(Image1.Width div 2 + round(PosX8)),(Image1.Height div 2 - round(PosY8))] := RGB(0,153,153);
      Image1.Repaint;
      sleep(10);
     end;
end;

end.