Delphi 判断点是否在区域内

function TfrmMain.IsInRect(points: TGpsPointManager; x,
  y: Double): Boolean;
var
  iSum, iCount, iIndex: Integer;
  dLon1, dLon2, dLat1, dLat2, dLon: double;
  ALon, ALat: double;
  obj1, obj2: TGpsPoint;
begin
  ALon := x;
  ALat := y;
  Result := False;

  iCount := points.Count;

  if iCount < 3 then
  begin
    Result := False;
    Exit;
  end;

  iSum := 0;
  for iIndex :=0 to iCount - 1 do
  begin
    if (iIndex = iCount - 1) then
    begin
      obj1 := points.Items[iIndex];
      obj2 := points.Items[0];
    end
    else
    begin
      obj1 := points.Items[iIndex];
      obj2 := points.Items[iIndex + 1];
    end;

    dLon1 := obj1.x;
    dLat1 := obj1.y;
    dLon2 := obj2.x;
    dLat2 := obj2.y;

    if ((ALat >= dLat1) and (ALat < dLat2)) or ((ALat>=dLat2) and (ALat < dLat1)) then
    begin
      if (abs(dLat1 - dLat2) > 0) then
      begin
        dLon := dLon1 - ((dLon1 -dLon2) * (dLat1 -ALat)) / (dLat1 - dLat2);
        if (dLon < ALon) then
          Inc(iSum);
      end;
    end;

  end;

  if (iSum mod 2 <> 0) then
    Result := True;
end;

 

posted @ 2018-04-19 15:03  都是城市惹的祸  阅读(380)  评论(0)    收藏  举报