Kouch曲线(简单的分形绘制)

Kouch曲线(简单的分形绘制)

// Fractal曲线,mc为画布,p1、p2起点终点,k为级数
procedure draw(mc: Tmetafilecanvas; X1, Y1, X2, Y2: INTEGER; k: INTEGER);
var
  x, y: real;
  MX1, MY1, MX2, MY2, MX3, MY3: INTEGER;
begin
  if k = 0 then
  begin
    mc.MoveTo(X1, Y1);
    mc.LineTo(X2, Y2);
    exit;
  end
  else
  begin
    x := (X2 - X1) / 3;
    y := (Y2 - Y1) / 3;
    MX1 := round(x + X1);
    MY1 := round(y + Y1);
    MX2 := round(x * 2 + X1);
    MY2 := round(y * 2 + Y1);
    MX3 := round(x * cos(pi / 3) + y * sin(pi / 3) + MX1);
    MY3 := round(y * cos(pi / 3) - x * sin(pi / 3) + MY1);
    draw(mc, X1, Y1, MX1, MY1, k - 1);
    draw(mc, MX1, MY1, MX3, MY3, k - 1);
    draw(mc, MX3, MY3, MX2, MY2, k - 1);
    draw(mc, MX2, MY2, X2, Y2, k - 1);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  m: tmetafile;
  mc: Tmetafilecanvas;
  k: INTEGER;
begin
  k := 7;
  m := tmetafile.Create;
  m.Width := 60000;
  m.Height := 50000;
  mc := Tmetafilecanvas.Create(m, 0);
  draw(mc, 10000, 15000, 50000, 15000, k);
  draw(mc, 50000, 15000, 30000, 43100, k);
  draw(mc, 30000, 43100, 10000, 15000, k);
  mc.Free;
  m.SaveToFile('d:\Fractal.emf');
  m.Free;
end;

你可能感兴趣的:(分形)