绘制了一个不规则的六边形
我想用鼠标点击的时候显示一些信息 
想判断点击的坐标是否在六边形内
请教个位大哥啦

解决方案 »

  1.   

    google 向量的叉乘积
    这个绝对可以实现你想要的
      

  2.   

    PtInRegion和PtInRect这个貌似可以实现
      

  3.   

    嗯,taxi大虾出手,果然不同凡响
      

  4.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TMyPoint = packed record
        X : double;
        Y : double;
      end;
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}
    {*------------------------------------------------------------------------------
      判断指定的经纬度坐标点是否落在指定的多边形区域内
      @param ALon   指定点的经度
      @param ALat   指定点的纬度
      @param APoints   指定多边形区域各个节点坐标
      @return True 落在范围内 False 不在范围内
    ------------------------------------------------------------------------------*}
    function IsPtInPoly(ALon, ALat: double; APoints: array of TMyPoint): Boolean;
    var
      iSum, iCount, iIndex: Integer;
      dLon1, dLon2, dLat1, dLat2, dLon: double;
    begin
      Result := False;
      if (Length(APoints) < 3) then
      begin
        Result := False;
        Exit;
      end;
      iSum := 0;
      iCount := Length(APoints);
      for iIndex :=0 to iCount - 1 do
      begin
        if (iIndex = iCount - 1) then
        begin
          dLon1 := APoints[iIndex].X;
          dLat1 := APoints[iIndex].Y;
          dLon2 := APoints[0].X;
          dLat2 := APoints[0].Y;
        end
        else
        begin
          dLon1 := APoints[iIndex].X;
          dLat1 := APoints[iIndex].Y;
          dLon2 := APoints[iIndex + 1].X;
          dLat2 := APoints[iIndex + 1].Y;
        end;
        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;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      arr: array[0..3] of TMyPoint;
    begin
      arr[0].X:= 126.64827704429626;
      arr[0].Y:= 45.62182674375908;
      arr[1].X:= 126.64976835250854;
      arr[1].Y:= 45.62228446469844;
      arr[2].X:= 126.65009021759033;
      arr[2].Y:= 45.6217967291407;
      arr[3].X:= 126.64869546890259;
      arr[3].Y:= 45.6212339520709;  if IsPtInPoly(126.64886713027954,45.62177421816639,arr) then
        showmessage('in')
      else
        showmessage('out');
    end;end.一个算经纬度坐标区域检测的楼主拿去参考
      

  5.   

    为什么不用顶点坐标创建一个Region,用PtInRegion判断?
      

  6.   

    要看顶点坐标是不是整数型 是整数的话可以用CreatePolygonRgn PtInRegion