📄 rect.inc
字号:
{$IFNDEF rect}
{$DEFINE rect}
{$I SYST.INC}
{$I STREAM.INC}
{$I OBJECT.INC}
type
TCoordinate=longint;
TPosition=record
x,y:TCoordinate;
end;
PRect=^TRect;
TRect=object(TObject)
private
x,y,w,h:TCoordinate;
public
Constructor init(_x,_y,_w,_h:TCoordinate);
Constructor init0;
Function load(var f:TStream):boolean; virtual;
Function save(var f:TStream):boolean; virtual;
Procedure clear; virtual;
Procedure copyToMe(src:PRect); virtual;
Procedure moveto(_x,_y:TCoordinate);
Procedure moveby(dx,dy:TCoordinate);
Procedure grow(dw,dh:TCoordinate);
Procedure resize(_w,_h:TCoordinate); virtual;
Procedure intersect(r:PRect); virtual;
Procedure union(r:PRect); virtual;
Function contains(_x,_y:TCoordinate):boolean; virtual;
Function left:TCoordinate;
Function top:TCoordinate;
Function right:TCoordinate;
Function bottom:TCoordinate;
Function width:TCoordinate;
Function height:TCoordinate;
Function contentSize:longint; virtual;
Function equals(r:TRect):boolean; virtual;
Function empty:boolean; virtual;
destructor done; virtual;
end; {TRect}
Constructor TRect.init(_x,_y,_w,_h:TCoordinate);
begin
resize(_w,_h);
moveto(_x,_y);
end; {TRect.init}
Constructor TRect.init0;
begin
clear;
end; {TRect.init0}
Function TRect.load(var f:TStream):boolean;
begin
result:=true;
f.readOrSkip(x,sizeof(x),result);
f.readOrSkip(y,sizeof(y),result);
f.readOrSkip(w,sizeof(w),result);
f.readOrSkip(h,sizeof(h),result);
end; {TRect.load}
Function TRect.save(var f:TStream):boolean;
begin
result:=true;
f.writeOrSkip(x,sizeof(x),result);
f.writeOrSkip(y,sizeof(y),result);
f.writeOrSkip(w,sizeof(w),result);
f.writeOrSkip(h,sizeof(h),result);
end; {TRect.load}
Procedure TRect.clear;
begin
x:=0;
y:=0;
w:=0;
h:=0;
end; {TRect.clear}
Procedure TRect.copyToMe(src:PRect);
begin
clear;
if src<>nil then init(src^.x,src^.y,src^.w,src^.h);
end; {TRect.copyToMe}
Procedure TRect.moveto(_x,_y:TCoordinate);
begin
x:=_x;
y:=_y;
end; {TRect.moveto}
Procedure TRect.moveby(dx,dy:TCoordinate);
begin
inc(x,dx);
inc(y,dy);
end; {TRect.moveby}
Procedure TRect.grow(dw,dh:TCoordinate);
begin
w:=Max(w+dw,0);
h:=Max(h+dh,0);
end; {TRect.grow}
Procedure TRect.resize(_w,_h:TCoordinate);
begin
w:=_w;
h:=_h;
end; {TRect.resize}
Procedure TRect.intersect(r:PRect);
var x2,y2:TCoordinate;
begin
x2:=Min(x+w,r^.x+r^.w);
y2:=Min(y+h,r^.y+r^.h);
moveto(Max(x,r^.x),Max(y,r^.y));
w:=Max(x2-x,0);
h:=Max(y2-y,0);
end; {TRect.intersect}
Procedure TRect.union(r:PRect);
var x0,y0,x1,y1:TCoordinate;
begin
x0:=Min(x,r^.x);
y0:=Min(y,r^.y);
x1:=Min(x+w,r^.x+r^.w);
y1:=Min(y+h,r^.y+r^.h);
init(x0,y0,x1-x0,y1-y0);
end; {TRect.union}
Function TRect.left:TCoordinate;
begin
result:=x;
end; {TRect.left}
Function TRect.top:TCoordinate;
begin
result:=y;
end; {TRect.top}
Function TRect.right:TCoordinate;
begin
result:=pred(x+w);
end; {TRect.right}
Function TRect.bottom:TCoordinate;
begin
result:=pred(y+h);
end; {TRect.bottom}
Function TRect.width:TCoordinate;
begin
result:=w;
end; {TRect.width}
Function TRect.height:TCoordinate;
begin
result:=h;
end; {TRect.height}
Function TRect.contentSize:longint;
begin
result:=w*h;
end; {TRect.contentSize}
Function TRect.contains(_x,_y:TCoordinate):boolean;
begin
result:=(_x>=x) and (_x<x+w) and (_y>=y) and (_y<y+h);
end; {TRect.contains}
Function TRect.equals(r:TRect):boolean;
begin
result:=(x=r.x) and (y=r.y) and (w=r.w) and (h=r.h);
end; {TRect.equals}
Function TRect.empty:boolean;
begin
result:=(w=0) or (h=0);
end; {TRect.empty}
destructor TRect.done;
begin
end; {TRect.done}
{$ENDIF}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -