⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 udisplaytsp.pas

📁 这是一道很基本的程序
💻 PAS
字号:
{ interface-based implementation of
  Dan Taylor's (dan@logicalgenetics.com)
  Evolutionary TSP Algorithm demo program
Modified: September 2002
  by Nikolai Shokhirev (nikolai@u.arizona.edu)
}
  { Display object }
unit uDisplayTSP;

interface

uses
  uUtilsEA, uITSP, classes, controls;

type

  TTSPDisplay = class(TPainter0)
  private
    fController: ITSPController;
    {  Clear the map }
//    procedure Clear;  //is in the base class
    {  Show the cities }
    procedure DrawCities;
    {  Display a route  }
    procedure DrawRoute(Individual : ITSPIndividual);
    function GetController: ITSPController;
    procedure SetController(const Value: ITSPController);
  public
    {  The constructor }
    constructor Create(aOwner: TComponent; aParent: TWinControl);
    {  The destructor }
    destructor Destroy; override;
    {  Call this to draw the map }
    procedure DrawMap;
    {  Draw the map with a route  }
    procedure DrawMapWithRoute(Individual : ITSPIndividual);
    property Controller: ITSPController read GetController write SetController;
  end;

implementation

uses
  Graphics;

{ TTSPDisplay }

constructor TTSPDisplay.Create(aOwner: TComponent; aParent: TWinControl);
begin
  inherited;
  Clear;
end;

destructor TTSPDisplay.Destroy;
begin
  inherited;
end;

procedure TTSPDisplay.DrawCities;
var
  i : Integer;
begin
    PenColor := clBlack;
    BrushColor := clRed;
    PenWidth := 1;
    with fController do
      for i := 0 to CityCount - 1 do
        Circle(Cities[i].x, Cities[i].y,0.2);
end;

procedure TTSPDisplay.DrawMap;
begin
  Clear;
  DrawCities;
end;

procedure TTSPDisplay.DrawMapWithRoute(Individual: ITSPIndividual);
begin
  Clear;
  DrawCities;
  DrawRoute(Individual);
end;

procedure TTSPDisplay.DrawRoute(Individual: ITSPIndividual);
var
  i, c : Integer;
begin
  {  Set the pen colour and stuff }
  PenColor := clBlue;
  PenWidth := 1;

  {  Start at the first city }
  with fController do
  begin
    if Individual.Steps > 1 then
    begin
      c := Individual.RouteArray[0];
      MoveTo(Cities[c].X, Cities[c].Y);
    end;

    {  Move round the other cities }
    for i := 1 to Individual.Steps - 1 do
    begin
      c := Individual.RouteArray[i];
      LineTo(Cities[c].X, Cities[c].Y);
    end;

    c := Individual.RouteArray[0];
    LineTo(Cities[c].X, Cities[c].Y);
  end;
end;

function TTSPDisplay.GetController: ITSPController;
begin
  result := fController;
end;

procedure TTSPDisplay.SetController(const Value: ITSPController);
begin
  fController := Value;
end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -