我对huizhang的函数进行了测试,结果无论是顺时针
还是逆时针都不对。不知是否程序有问题,或者是算
法用错了。程序如下:
unit Test;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics,
Controls, Forms, Dialogs,ExtCtrls, Menus;
type
TForm1 = class(TForm)
Panel1: TPanel;
MainMenu1: TMainMenu;
File1: TMenuItem;
Test1: TMenuItem;
N1: TMenuItem;
Exit1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
Image1: TImage;
procedure Test1Click(Sender: TObject);
procedure Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Exit1Click(Sender: TObject);
procedure Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
function PointInFence(p: TPoint; Fence: Array of TPoint): boolean;
var
Form1: TForm1;
implementation
var
PtNos:Integer;
MyPtArray:Array[0..15] of TPoint;
{$R *.DFM}
procedure TForm1.Test1Click(Sender: TObject);
begin
PtNos:=0;
Form1.Refresh;
Panel1.Caption:='LeftButton=Define points RightButton=Finished Esc=Exit';
Image1.OnMouseDown:=Form1.Panel1MouseDown;
end;
procedure TForm1.Panel1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
IF Button=mbLeft then
begin
MyPtArray[PtNos].x:=x;
MyPtArray[PtNos].y:=y;
if PtNos=0 then
Form1.Canvas.MoveTo(x,y);
if PtNos>High(MyPtArray) then
ShowMessage('The largest number of points '+InttoStr(High(MyPtArray))+' is surpassed!')
else
begin
Form1.Canvas.LineTo(x,y);
PtNos:=PtNos+1;
end;
end
else if Button=mbRight then
begin
if PtNos>0 then
begin
Form1.Canvas.MoveTo(MyPtArray[PtNos-1].x,MyPtArray[PtNos-1].y);
Form1.Canvas.LineTo(MyPtArray[0].x,MyPtArray[0].y);
Form1.Image1.OnMouseMove:=Form1.Panel1MouseMove;
end
else
MessageDlg('You must define wild area firstly!',mtconfirmation,[mbOK],0);
end;
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
Close;
end;
function PointInFence(p: TPoint; Fence: Array of TPoint): boolean;
var
p1: TPoint;
ar: longint;
i: integer;
begin
ar := 0;
for i:=0 to PtNos-1 do
begin
p1 := Fence
;
Ar:=(p1.x*p.y)-(p1.y*p.x)+ar;
end;
Result:= Ar > 0;
end;
procedure TForm1.Panel1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
InFlag:Boolean;
MiddlePt:TPoint;
begin
Inflag:=PointInFence(Point(x,y),MyPtArray);
Panel1.Caption:='('+InttoStr(x)+','+InttoStr+')';
if Inflag then
Panel1.Caption:=Panel1.Caption+'In the fence'
else
Panel1.Caption:=Panel1.Caption+'Out of fence';
end;
end.