检查点是否在多边形内

Checking whether a point is inside a polygon

我制作了一个对象 class 并在其上设置了 mouseEnter 事件。 Object描述了一个使用opengl创建圆的过程(这里是绘图过程函数):

public void DrawCicrle()
        {
            GL.Begin(PrimitiveType.TriangleFan);
            GL.Color4(Color_);
            GL.Vertex2(X_, Y_);
            for (int i = 0; i < 360; i++)
            {
                GL.Vertex2(X_ + Math.Cos(i) * Radius_, Y_ + Math.Sin(i) * Radius_);

            }
            GL.End();

        }

然后我做了一个mouseEvent,但是无法正确获取光标的坐标。在圆弧中获取点的正确条件是什么? 此函数returns a点,但在左侧有一点偏移(X_和Y_为圆心(double类型)Radius_也是double类型):

public Point CursorLocation
        {
            get
            {
                return CursorLocation_;
            }
            set
            {
                this.CursorLocation_ = value;
                for (int i = 0; i < 360; i++)
                {
                    if (CursorLocation_.X <= X_ + Math.Cos(i) * Radius_ && CursorLocation_.Y<= Y_ + Math.Sin(i) * Radius_ && CursorLocation_.Y>=Y_-Radius_)
                    {
                        Enter(new Point(CursorLocation_.X, CursorLocation_.Y));
                        break;
                    }
                }
            }
        }

条件为dx * dx + dy * dy < R * R,其中dx=Math.Abs(x-cx) and dy=Math.Abs(y-cy)

(x,y)为光标位置,(cx,cy)为圆心

所以你检查圆心和某个点之间的欧几里得距离。为避免平方根计算,您只需对比较的两边进行平方

如果您还想计算圆圈边界,请使用 <=