这个算法抄自Graphics 32 的 GR32_Polygons.pas ,大名鼎鼎的开源图形组件,官网地址请大家上导航里找。
function PtInPolygon(const Pt: TPoint; const Points: array of TPoint): Boolean; var I: Integer; iPt, jPt: PPoint; begin Result := False; iPt := @Points[0]; jPt := @Points[High(Points)]; for I := 0 to High(Points) do begin Result := Result xor (((Pt.Y >= iPt.Y) xor (Pt.Y >= jPt.Y)) and (Pt.X - iPt.X < MulDiv(jPt.X - iPt.X, Pt.Y - iPt.Y, jPt.Y - iPt.Y))); jPt := iPt; Inc(iPt); end; end;
具体算法的原理俺没研究,图形算法不是俺的强项,俺只负责抄一下。