summaryrefslogtreecommitdiff
path: root/extras/contributed/nicegrid/fpg_types.pas
blob: 96d5cbc260997d868b6edfe166cc41b468c2f30b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
unit fpg_types;
{$mode objfpc}{$H+}

interface

uses SysUtils, Classes, fpg_base;

function EqualfpgRect(const r1,r2 : TfpgRect) : Boolean;
function PtInfpgRect(const ARect : TfpgRect; const p : TPoint) : Boolean;
function IntersectfpgRect(out ARect : TfpgRect; const R1,R2 : TfpgRect) : Boolean;
function UnionfpgRect(out ARect : TfpgRect; const R1,R2 : TfpgRect) : Boolean;
function IsfpgRectEmpty(const ARect : TfpgRect) : Boolean;
function OffsetfpgRect(var ARect : TfpgRect;DX : Integer;DY : Integer) : Boolean;
function InflatefpgRect(var ARect: TfpgRect; dx: Integer; dy: Integer): Boolean;
function CopyfpgRect(out Ds: TfpgRect;const Sc : TfpgRect): Boolean;

implementation


function EqualfpgRect(const r1,r2 : TfpgRect) : Boolean;
begin
  Result:= (r1.Left=r2.Left) and (r1.Right=r2.Right) and (r1.Width=r2.Width) and (r1.Height=r2.Height);
end;

function PtInfpgRect(const ARect : TfpgRect;const p : TPoint) : Boolean;
begin
  Result:=(p.y >= ARect.Top) and
            (p.y  <= ARect.Bottom) and
            (p.x  >= ARect.Left) and
            (p.x  <= ARect.Right);
end;

function IsfpgRectEmpty(const ARect : TfpgRect) : Boolean;
begin
  Result:=(ARect.Width <= 0) or (ARect.Height <= 0);
end;

function IntersectfpgRect(out ARect : TfpgRect;const R1,R2 : TfpgRect) : Boolean;
begin
  ARect:=R1;
  with R2 do
  begin
    if Left > R1.Left then
      ARect.Left:=Left;
    if Top > R1.Top then
      ARect.Top:=Top;
    if Right < R1.Right then
      ARect.Width:= ARect.Left + Right; 
    if Bottom < R1.Bottom then
      ARect.Height:= ARect.Top + Bottom; 
    end;
  if IsfpgRectEmpty(ARect) then
  begin
    FillChar(ARect,SizeOf(ARect),0);
    Result:=false;
  end
  else
    Result:=true;
end;

function UnionfpgRect(out ARect : TfpgRect;const R1,R2 : TfpgRect) : Boolean;
begin
  ARect:=R1;
  with R2 do
  begin
    if Left < R1.Left then
      ARect.Left:=Left;
    if Top < R1.Top then
      ARect.Top:=Top;
    if Right > R1.Right then
      ARect.Width:=  ARect.Left + Right; 
    if Bottom>R1.Bottom then
      ARect.Height:= ARect.Top + Bottom; 
    end;
  if IsfpgRectEmpty(ARect) then
  begin
    FillChar(ARect,SizeOf(ARect),0);
    Result:=false;
  end
  else
    Result:=true;
end;



function OffsetfpgRect(var ARect : TfpgRect;DX : Integer;DY : Integer) : Boolean;
begin
  if assigned(@ARect) then
    begin
    with ARect do
    begin
      inc(Left,dx);
      inc(Top,dy);
    end;
    Result:=true;
    end
  else
    Result:=false;
end;

function InflatefpgRect(var ARect: TfpgRect; dx: Integer; dy: Integer): Boolean;
begin
  if Assigned(@ARect) then
  begin
    with ARect do
    begin
      dec(Left, dx);
      dec(Top, dy);
      inc(Width, dx*2);
      inc(Height, dy*2);
    end;
    Result := True;
  end
  else
    Result := False;
end;
  
function CopyfpgRect(out Ds: TfpgRect;const Sc : TfpgRect): Boolean;
begin
  Ds:=Sc;
  if IsfpgRectEmpty(Ds) then
  begin	  
    FillChar(Ds,SizeOf(Ds),0);
    Result:=false;
  end
  else
    Result:=true;
end;  


end.