CJC Delphi 技巧馆's Archiver

cjc Posted at 2012-1-16 00:30

Question: Is a point inside a given polygon? (用WinApi判断一个点是否在多边形内)

Question:

It is of interest to determin if a point is within a given polygon. My polygon is represented as an array of TPoints.

Answer:

A quick way to do this is to use the Windows API function PtInRegion. If not already avalaible, call CreatePolygonRgn() to create a region and pass the region's handle to PtInRegion().
Don't forget to free Windows' region handle afterwards.

A neat trick is to use the Length() function to obtain the number of points in the polygon.

               
// PointInPolygon() function by Andreas Filsinger
function PointInPolygon (const x,y : integer; aPolygon: array of TPoint): boolean;
var
  PolyHandle: hRgn;
begin
  PolyHandle := CreatePolygonRgn(aPolygon[0], Length(aPolygon), Winding);
  result     := PtInRegion(PolyHandle,X,Y);
  DeleteObject(PolyHandle);
end

Page: [1]
CJC好玩背单词最新版本 - 是目前最好的背单词软件,游戏好玩,功能强大,词库众多,还有真人语音,免费下载,免费试用 !

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.