unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, ComCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    CheckBox3: TCheckBox;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Memo1: TMemo;
    OpenDialog1: TOpenDialog;
    Button1: TButton;
    Shape1: TShape;
    TrackBar1: TTrackBar;
    TrackBar2: TTrackBar;
    TrackBar3: TTrackBar;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure CheckBox1Change(Sender: TObject);
    procedure CheckBox2Change(Sender: TObject);
    procedure CheckBox3Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Label1click(Sender: TObject);
    procedure Label4Click(Sender: TObject);
    procedure Label5Click(Sender: TObject);
    procedure Memo1Change(Sender: TObject);
    procedure Shape1ChangeBounds(Sender: TObject);
    procedure Shape1Paint(Sender: TObject);
    procedure TrackBar1Change(Sender: TObject);
    procedure TrackBar2Change(Sender: TObject);
    procedure TrackBar3Change(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  centro_x, centro_y, alto_imagen, ancho_imagen: Integer;
  angulo, longitud_eje : integer;
  i: integer;
  numero_puntos: integer;
  radio: Integer;
  temporal_puntos: array [1..3,1..1000] of real;
  puntos : array[1..3,1..1000] of real;
  ubicacion_x, ubicacion_y: real;
  fichero_puntos : text;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Shape1Paint(Sender: TObject);



begin
ancho_imagen:=shape1.Width;
alto_imagen:= shape1.height;
centro_x:= trunc(ancho_imagen/2);
centro_y :=trunc(alto_imagen/2);
shape1.Canvas.Pen.Style := psdot;

{linea horizontal}
shape1.Canvas.line(0,centro_y, ancho_imagen, centro_y);

{linea vertical}
shape1.canvas.line(centro_x,0, centro_x, alto_imagen);

{Estilo de lneas en los ejes}
shape1.Canvas.Pen.style := pssolid;
shape1.canvas.pen.Width := 2;

{dibujar eje Z}
shape1.canvas.line(centro_x,centro_y,
                   centro_x, centro_y-longitud_eje);


{dibujat eje X}
shape1.canvas.line(centro_x,centro_y,
                   centro_x-trunc(longitud_eje*cos(angulo*2*pi/360)),
                   centro_y+trunc(longitud_eje*sin(angulo*2*pi/360)));

{dibujar eje Y}
shape1.canvas.line(centro_x,
                   centro_y,
                   centro_x+trunc(longitud_eje*cos(angulo*2*pi/360)),
                   centro_y+trunc(longitud_eje*sin(angulo*2*pi/360)));

{Representacin de los puntos en R3}
shape1.canvas.pen.Width:=1;
for i:=1 to numero_puntos do
  begin
  ubicacion_x:= centro_x - puntos[1,i] * cos(angulo*2*pi/360) +
  puntos[2,i] * cos(angulo*2*pi/360);
  ubicacion_y:= centro_y + puntos[1,i] * sin(angulo*2*pi/360) +
  puntos[2,i] * sin(angulo*2*pi/360) - puntos[3,i];
  shape1.Canvas.pen.Color:=clblack;
  shape1.canvas.Brush.Color:=clred;
  shape1.canvas.ellipsec(trunc(ubicacion_x), trunc(ubicacion_y),
  radio, radio);
  end;
end; { fin del OnPaint}

{rotacin sobre eje X}
procedure TForm1.TrackBar1Change(Sender: TObject);
var
 angulo_giro_eje_x : real;
begin
angulo_giro_eje_x:= trackbar1.Position*2*pi/360; {el angulo debe operar
en radianes }
label1.Caption:=inttostr(trackbar1.position);
for i:=1 to numero_puntos do
 begin
 puntos[2,i]:= temporal_puntos[2,i] * cos(angulo_giro_eje_x) +
             - temporal_puntos[3,i] * sin(angulo_giro_eje_x);
 puntos[3,i]:= temporal_puntos[2,i] * sin(angulo_giro_eje_x) +
               temporal_puntos[3,i] * cos(angulo_giro_eje_x);
 end;
shape1.Invalidate;
end;

{rotacin sobre eje Y}
procedure TForm1.TrackBar2Change(Sender: TObject);
var
 angulo_giro_eje_y : real;
 begin
 angulo_giro_eje_y:= trackbar2.Position*2*pi/360; {el angulo debe operar
 en radianes }
 label2.Caption:=inttostr(trackbar2.position);
 for i:=1 to numero_puntos do
  begin
  puntos[1,i]:= temporal_puntos[1,i] * cos(angulo_giro_eje_y) +
              - temporal_puntos[3,i] * sin(angulo_giro_eje_y);
  puntos[3,i]:= temporal_puntos[1,i] * sin(angulo_giro_eje_y) +
                temporal_puntos[3,i] * cos(angulo_giro_eje_y);
  end;
 shape1.Invalidate;
 end;

{rotacin sobre eje Z}
procedure TForm1.TrackBar3Change(Sender: TObject);
var
 angulo_giro_eje_z : real;
begin
angulo_giro_eje_z:= trackbar3.Position*2*pi/360; {el angulo debe operar
en radianes }
label3.Caption:=inttostr(trackbar3.position);
for i:=1 to numero_puntos do
 begin
 puntos[1,i]:= temporal_puntos[1,i] * cos(angulo_giro_eje_z) +
             - temporal_puntos[2,i] * sin(angulo_giro_eje_z);
 puntos[2,i]:= temporal_puntos[1,i] * sin(angulo_giro_eje_z) +
               temporal_puntos[2,i] * cos(angulo_giro_eje_z);
 end;
shape1.Invalidate;
end;

procedure TForm1.Shape1ChangeBounds(Sender: TObject);
begin

end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

procedure TForm1.Label1click(Sender: TObject);
begin

end;

procedure TForm1.Label4Click(Sender: TObject);
begin

end;

procedure TForm1.Label5Click(Sender: TObject);
begin

end;

procedure TForm1.Memo1Change(Sender: TObject);
begin

end;

procedure TForm1.Button1Click(Sender: TObject); {BOTON }
var
  coordenada: real;
i: integer;
begin
if opendialog1.execute then
  begin
  i:=0;
  assignfile(fichero_puntos, opendialog1.filename);
  reset(fichero_puntos);
  while not eof(fichero_puntos) do
    begin
    i:= i + 1;
    read(fichero_puntos, coordenada);
    memo1.lines.text:= memo1.lines.text +floattostr(coordenada)+' # ';
    puntos[1, i] := coordenada;
    read(fichero_puntos, coordenada);
    memo1.lines.text:= memo1.lines.text +floattostr(coordenada)+' # ';
    puntos[2, i] := coordenada;
    read(fichero_puntos, coordenada);
    memo1.lines.text:= memo1.lines.text +floattostr(coordenada)+' # ';
    puntos[3, i] := coordenada;
    memo1.lines.text:= memo1.lines.text + #13 + #10;
    end;
  closefile(fichero_puntos);
  numero_puntos:=i;
  end;
shape1.Invalidate;
end;

{reflexion segun plano XY}
procedure TForm1.Button2Click(Sender: TObject);
begin
  for i :=1 to numero_puntos do
    begin
    puntos[3,i]:=-puntos[3,i];
    end;
shape1.invalidate;
end;

{Escalado. En funcin de factor a elegir por el usuario}
procedure TForm1.Button3Click(Sender: TObject);
var
   factor_escala : real;
begin
factor_escala:=strtofloat(edit1.text);
for i:= 1 to numero_puntos do
begin
puntos[1,i]:= puntos[1,i]*factor_escala;
puntos[2,i]:= puntos[2,i]*factor_escala;
puntos[3,i]:= puntos[3,i]*factor_escala;
end;
shape1.invalidate;
end;

{reflexion segun plano XZ}
procedure TForm1.Button4Click(Sender: TObject);
begin
for i:= 1 to numero_puntos do
  begin
  puntos[2,i]:=-puntos[2,i];
  end;
shape1.invalidate;
end;

{reflexion segun YZ}
procedure TForm1.Button5Click(Sender: TObject);
begin
  for i:= 1 to numero_puntos do
  begin
  puntos[1,i]:=-puntos[1,i];
  end;
shape1.invalidate;
end;

{Botn Fijar}
procedure TForm1.Button6Click(Sender: TObject);
begin
memo1.lines.Clear;
for i:= 1 to numero_puntos do
 begin
 temporal_puntos[1,i]:=puntos[1,i];
 temporal_puntos[2,i]:=puntos[2,i];
 temporal_puntos[3,i]:=puntos[3,i];
 memo1.lines.text:= memo1.lines.text +floattostr(puntos[1,i])+' # ';
 memo1.lines.text:= memo1.lines.text +floattostr(puntos[2,i])+' # ';
 memo1.lines.text:= memo1.lines.text +floattostr(puntos[3,i])+' # ';
 memo1.lines.text:= memo1.lines.text + #13 + #10;
 end;
trackbar1.position:=0;
trackbar2.position:=0;
trackbar3.position:=0;
end;

{boton volver}
procedure TForm1.Button7Click(Sender: TObject);
begin
for i:= 1 to numero_puntos do
 begin
 puntos[1,i]:= temporal_puntos[1,i];
 puntos[2,i]:= temporal_puntos[2,i];
 puntos[3,i]:= temporal_puntos[3,i];
 end;
trackbar1.position:=0;
trackbar2.position:=0;
trackbar3.position:=0;
end;

{proyecta segun eje X}
procedure TForm1.CheckBox1Change(Sender: TObject);
begin
if CheckBox1.Checked then
  begin
  for i:= 1 to numero_puntos do
      begin
      puntos[1,i]:=0;
      end;
 end;
shape1.invalidate;
end;

{proyecta segun eje Y}
procedure TForm1.CheckBox2Change(Sender: TObject);
begin
if CheckBox2.Checked then
  begin
  for i:= 1 to numero_puntos do
      begin
      puntos[2,i]:=0;
      end;
 end;
shape1.invalidate;
end;

{proyecta segun eje Z}
procedure TForm1.CheckBox3Change(Sender: TObject);
begin
if CheckBox3.Checked then
  begin
  for i:= 1 to numero_puntos do
      begin
      puntos[3,i]:=0;
      end;
 end;
shape1.invalidate;
end;

initialization
begin
  angulo:=30;
  longitud_eje:=600;
  radio:=3;
  numero_puntos:=0;
end;

end.
           