///////////////////////////////////////////////////////////////////////////// /// Autor: Joan Puigcerver Pérez /// Contact: gunman_corporation@msn.com || gunmancorp@gmail.com /// Description: This program make the Triangle of Sierpinski until /// 10 interaccions. /// Copyright (C) 2006, Joan Puigcerver Pérez /// /// This program is free software; you can redistribute it and/or modify /// it under the terms of the GNU General Public License as published by /// the Free Software Foundation; either version 2 of the License, or /// (at your option) any later version. /// /// This program is distributed in the hope that it will be useful, /// but WITHOUT ANY WARRANTY; without even the implied warranty of /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the /// GNU General Public License for more details. /// /// You should have received a copy of the GNU General Public License /// along with this program; if not, write to the Free Software /// Foundation, Inc., 51 Franklin St, Fifth Floor, /// Boston, MA 02110-1301 USA ///////////////////////////////////////////////////////////////////////////// unit Sierpinski; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Math, JvExControls, JvComponent, JvZoom, ExtCtrls, Mask, JvExMask, JvSpin; type TSierpinskiFrm = class(TForm) Button1: TButton; Label2: TLabel; Image1: TImage; Panel1: TPanel; Label1: TLabel; Label3: TLabel; INTER: TJvSpinEdit; AX: TJvSpinEdit; AY: TJvSpinEdit; BX: TJvSpinEdit; BY: TJvSpinEdit; CY: TJvSpinEdit; CX: TJvSpinEdit; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Label8: TLabel; Label9: TLabel; Label10: TLabel; Label11: TLabel; Label12: TLabel; JvZoom1: TJvZoom; Zoom: TJvSpinButton; procedure FormCreate(Sender: TObject); procedure ZoomBottomClick(Sender: TObject); procedure ZoomTopClick(Sender: TObject); procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure Button1Click(Sender: TObject); private { Private declarations } procedure Dibuixa(const A, B, C: TPoint; N: Integer); public { Public declarations } end; var SierpinskiFrm: TSierpinskiFrm; implementation {$R *.dfm} {Procediment per a saber el punt mig d'un vector, el vector que representa el costat de cada triangle} function Dividir(const P1, P2: TPoint): TPoint; begin Result.X := Trunc((P1.X+P2.X)/2); Result.Y := Trunc((P1.Y+P2.Y)/2); end; {Procediment per a dibuixar el Triangle de Sierpinski, els arguments corresponen als punts del triangle (A,B,C) i a la interacció (N) actuals. Si la interacció és 0, dibuixa el triangle mínim; si no és 0 aleshores calcula els punts mitjos de cada costat del triángle actual i crida la funció Dibuixa per a calcular els triángles que hi haurà dins del triàngle actual.} procedure TSierpinskiFrm.Dibuixa(const A, B, C: TPoint; N: Integer); var AB, AC, BC: TPoint; begin if N = 0 then Image1.Canvas.Polygon([A,B,C]) else begin AB := Dividir(A,B); BC := Dividir(B,C); AC := Dividir(C,A); Dibuixa(A, AB, AC, N-1 ); Dibuixa(AB, B, BC, N-1 ); Dibuixa(AC, BC, C, N-1 ); end; end; {Al obrir el programa executa el codi del botó de 'Generar'} procedure TSierpinskiFrm.FormCreate(Sender: TObject); begin Button1.Click; end; {Codi del botó 'Generar'. Elimina el fons de la imatge, obtén els punts inicials del triangle de les caixes de text del programa (AX,AY,BX...), finalment fa que el fons del triángle siga transparent i executa el procediment Dibuixa amb les dades del primer triangle i amb el nombre d'interaccions indicades per l'usuari} procedure TSierpinskiFrm.Button1Click(Sender: TObject); var A,B,C: TPoint; begin Image1.Canvas.FillRect(Image1.ClientRect); A.X := Trunc(AX.Value); A.Y := Trunc(AY.Value); B.X := Trunc(BX.Value); B.Y := Trunc(BY.Value); C.X := Trunc(CX.Value); C.Y := Trunc(BY.Value); Image1.Transparent := True; Dibuixa(A,B,C,Trunc(INTER.Value)); end; procedure TSierpinskiFrm.Image1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin Label2.Caption := 'X: '+IntToStr(X)+' || Y:'+IntToStr(Y); end; procedure TSierpinskiFrm.ZoomBottomClick(Sender: TObject); begin if JvZoom1.ZoomPercentage > 0 then JvZoom1.ZoomPercentage := JvZoom1.ZoomPercentage-10; end; procedure TSierpinskiFrm.ZoomTopClick(Sender: TObject); begin if JvZoom1.ZoomPercentage < 1000 then JvZoom1.ZoomPercentage := JvZoom1.ZoomPercentage+10; end; end.