unit grafica;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Graphics;
type pointf=record
x,y:double;
valido:boolean;
end;
type tSegmento=object
inizio,fine:pointf;
function trapezio:double;
function distanza:double;
end;
type Ttavola=class
private
foglio:TCanvas;
bassoSinistra,scale:pointf;
procedure trasla(var p:pointf);
procedure setColore(colore:qword);
function getColore:qword;
procedure setSpessore(spessore:integer);
function getSpessore:integer;
public
constructor crea(posto:TCanvas);
constructor crea(posto:TCanvas; x0,y0,xf:double);
constructor crea(posto:TCanvas; x0,y0,xf,yf:double);
procedure cancella(colore:qword);
procedure assi(colore:qword);
procedure linea(tratto:tSegmento);
procedure linea(x0,y0,xf,yf:double);
procedure cerchio(xo,yo,raggio:double);
procedure punto(coordinate:pointf);
property tinta:qword read getColore write setColore;
property spessore:integer read getSpessore write setSpessore;
end;
implementation
constructor Ttavola.crea(posto:TCanvas);
begin
crea(posto, -10.0, -5.0, 10.0, 5.0);
end;
constructor Ttavola.crea(posto:TCanvas; x0,y0,xf:double);
begin
foglio:=posto;
cancella($101010);
foglio.Pen.Color:=clRed;
bassoSinistra.x:=x0;
bassoSinistra.y:=y0;
scale.x:=(xf - x0) / posto.Width;
if scale.x = 0 then scale.x:=1;
scale.y:=scale.x;
end;
constructor Ttavola.crea(posto:TCanvas; x0,y0,xf,yf:double);
begin
foglio:=posto;
cancella($101010);
foglio.Pen.Color:=clRed;
bassoSinistra.x:=x0;
bassoSinistra.y:=y0;
scale.x:=(xf - x0) / posto.Width;
scale.y:=(yf - y0) / posto.Height;
if scale.x=0 then scale.x:=1;
if scale.y=0 then scale.y:=1;
end;
procedure Ttavola.cancella(colore:qword);
var x,y:integer;
begin
foglio.Brush.Color:=colore mod clWhite;
x:=foglio.Width;
y:=foglio.Height;
foglio.FillRect(0, 0, x, y);
end;
procedure Ttavola.trasla(var p:pointf);
begin
p.x:=(p.x - bassoSinistra.x) / scale.x;
p.y:=foglio.Height + ((bassoSinistra.y - p.y) / scale.y);
if p.x < 0 then p.x:=-1;
if p.x > foglio.Width then p.x:=foglio.Width + 1;
if p.y < 0 then p.y:=-1;
if p.y > foglio.Height then p.y:=foglio.Height + 1;
end;
procedure Ttavola.assi(colore:qword);
var vecchio:qword;
origine:pointf;
begin
origine.x:=0.0; origine.y:=0.0;
vecchio:=tinta;
tinta:=colore;
trasla(origine);
foglio.Line(0, round(origine.y), foglio.Width, round(origine.y));
foglio.Line(round(origine.x), 0, round(origine.x), foglio.Height);
tinta:=vecchio;
end;
procedure Ttavola.linea(tratto:tSegmento);
begin
trasla(tratto.inizio);
trasla(tratto.fine);
foglio.Line(round(tratto.inizio.x), round(tratto.inizio.y), round(tratto.fine.x), round(tratto.fine.y));
end;
procedure Ttavola.linea(x0,y0,xf,yf:double);
var tratto:tSegmento;
begin
tratto.inizio.x:=x0;
tratto.inizio.y:=y0;
tratto.fine.x:=xf;
tratto.fine.y:=yf;
linea(tratto);
end;
procedure Ttavola.punto(coordinate:pointf);
begin
trasla(coordinate);
foglio.Pixels[round(coordinate.x), round(coordinate.y)]:=foglio.Pen.Color;
end;
procedure Ttavola.cerchio(xo,yo,raggio:double);
var tratto:tSegmento;
alfa,passo:double;
begin
tratto.inizio.x:=xo + raggio;
tratto.inizio.y:=yo;
passo:=0.05;
alfa:=passo;
while alfa <= 6.3 do
begin
tratto.fine.x:=xo + raggio * cos(alfa);
tratto.fine.y:=yo + raggio * sin(alfa);
linea(tratto);
tratto.inizio:=tratto.fine;
alfa:=alfa + passo;
end;
end;
procedure Ttavola.setColore(colore:qword);
begin
foglio.Pen.Color:=colore mod clWhite;
end;
function Ttavola.getColore:qword;
begin
getColore:=foglio.Pen.Color;
end;
procedure Ttavola.setSpessore(spessore:integer);
begin
foglio.Pen.Width:=abs(spessore) mod 50;
end;
function Ttavola.getSpessore:integer;
begin
getSpessore:=foglio.Pen.Width;
end;
function tSegmento.trapezio:double;
var base,altezza:double;
begin
base:=fine.x - inizio.x;
altezza:=fine.y + inizio.y;
trapezio:=0.5 * base * altezza;
end;
function tSegmento.distanza:double;
var dx,dy:double;
begin
dx:=fine.x-inizio.x;
dy:=fine.y-inizio.y;
distanza:=sqrt(dx*dx + dy*dy);
end;
end.