Exemplo n.º 1
0
        public bool FindIntersect(Point start, Point end, ref Point intersect, ref int index)
        {
            bool found = false;

            double A1 = end.Y - start.Y;
            double B1 = end.X - start.X;
            double C1 = A1 * start.X + B1 * start.Y;

            double path_dist = Get_Dist(start, end);
            double i_dist    = double.MaxValue;

            //find which border get intersected by our line
            //return the point of intersection and the index of the side intersected

            for (int i = 0; i < PointList.Count; i++)
            {
                Point left  = (Point)PointList[i];
                Point right = (Point)PointList[Get_Right_Point(i)];

                double A2 = right.Y - left.Y;
                double B2 = right.X - left.X;
                double C2 = A2 * left.X + B2 * left.Y;

                double det = A1 * B2 - A2 * B1;
                double c_x = (B2 * C1 - B1 * C2) / det;
                double c_y = (A1 * C2 - A2 * C1) / det;

                if ((c_x == left.X && c_y == left.Y) || (c_x == right.X && c_y == right.Y))
                {
                    //ignore collision on the end points
                }
                else
                {
                    //need to determine if this collision is between start <-> end
                    if (Util.MIN(left.X, right.X) <= c_x && c_x <= Util.MAX(left.X, right.X) &&
                        Util.MIN(left.Y, right.Y) <= c_y && c_y <= Util.MAX(left.Y, right.Y))
                    {
                        //if so... get the dist from start to the collision
                        //  set intersect and i_dist
                        double n_dist = Math.Sqrt(Math.Pow(start.X - c_x, 2) + Math.Pow(start.Y - c_y, 2));

                        if (n_dist < i_dist)
                        {
                            i_dist      = n_dist;
                            intersect.X = (float)c_x;
                            intersect.Y = (float)c_y;
                            index       = i;//left point
                            found       = true;
                        }
                    }
                    else
                    {
                        //not inside our line segment
                    }
                }
            }
            return(found);
        }
Exemplo n.º 2
0
        public bool IsPointInside(Point p)
        {
            if (PointList.Count < 3)
            {
                return(true);
            }

            int counter = 0;

            //need to run rays and check for collision,
            float xinters;
            Point p1, p2;
            int   i;

            try
            {
                p1 = (Point)PointList[0];
                for (i = 1; i <= PointList.Count; i++)
                {
                    p2 = (Point)PointList[i % PointList.Count];
                    if (p.Y > Util.MIN(p1.Y, p2.Y))
                    {
                        if (p.Y <= Util.MAX(p1.Y, p2.Y))
                        {
                            if (p.X <= Util.MAX(p1.X, p2.X))
                            {
                                if (p1.Y != p2.Y)
                                {
                                    xinters = (p.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X;
                                    if (p1.X == p2.X || p.X <= xinters)
                                    {
                                        counter++;
                                    }
                                }
                            }
                        }
                    }
                    p1 = p2;
                }

                //next we do a modulus and return out result
                if (counter % 2 == 0)
                {
                    return(false);
                }

                return(true);
            }
            catch
            {
                Globals.l2net_home.Add_Error("border check failed... get those mexicans out of here!");
                return(false);
            }
        }