Пример #1
0
        /// <summary>
        /// Converts a rectangle into an equivalent polygon
        /// </summary>
        /// <param name="r"></param>
        /// <returns></returns>
        public static Polygon GetFromRectangle(Rectangle r)
        {
            Polygon tmp = new Polygon(UNIT_SQUARE(), LocationManager.getVectorFromPoint(r.Center), 0);

            tmp.Scale(r.Width, r.Height);
            return(tmp);
        }
Пример #2
0
        protected static bool[,] ScanOctant(Point Source, bool[,] World, bool[,] Ret, int pDepth, int pOctant, double pStartSlope, double pEndSlope, int range)
        {
            int visrange2 = range * range;
            int x         = 0;
            int y         = 0;

            switch (pOctant)
            {
            case 1:     //nnw // 0, -1
                // -1, 0
                y = Source.Y - pDepth;
                if (y < 0)
                {
                    return(Ret);
                }

                x = Source.X - Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (x < 0)
                {
                    x = 0;
                }

                while (GetSlope(x, y, Source.X, Source.Y, false) >= pEndSlope)
                {
                    if (LocationManager.distanceCheck(LocationManager.getVectorFromPoint(Source), new Vector2(x, y), range))
                    {
                        Ret[x, y] = true;
                        if (World[x, y])                        //current cell blocked
                        {
                            if (x - 1 >= 0 && !World[x - 1, y]) //prior cell within range AND open...
                            //...incremenet the depth, adjust the endslope and recurse
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x - 0.5, y + 0.5, Source.X, Source.Y, false), range);
                            }
                        }
                        else
                        {
                            if (x - 1 >= 0 && World[x - 1, y])     //prior cell within range AND open...
                            //..adjust the startslope
                            {
                                pStartSlope = GetSlope(x - 0.5, y - 0.5, Source.X, Source.Y, false);
                            }
                        }
                    }
                    x++;
                }
                x--;
                break;

            case 2:     //nne

                y = Source.Y - pDepth;
                if (y < 0)
                {
                    return(Ret);
                }

                x = Source.X + Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (x >= World.GetLength(0))
                {
                    x = World.GetLength(0) - 1;
                }

                while (GetSlope(x, y, Source.X, Source.Y, false) <= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (x + 1 < World.GetLength(0) && !World[x + 1, y])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x + 0.5, y + 0.5, Source.X, Source.Y, false), range);
                            }
                        }
                        else
                        {
                            if (x + 1 < World.GetLength(0) && World[x + 1, y])
                            {
                                pStartSlope = -GetSlope(x + 0.5, y - 0.5, Source.X, Source.Y, false);
                            }
                        }
                    }
                    x--;
                }
                x++;
                break;

            case 3:

                x = Source.X + pDepth;
                if (x >= World.GetLength(0))
                {
                    return(Ret);
                }

                y = Source.Y - Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (y < 0)
                {
                    y = 0;
                }

                while (GetSlope(x, y, Source.X, Source.Y, true) <= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (y - 1 >= 0 && !World[x, y - 1])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x - 0.5, y - 0.5, Source.X, Source.Y, true), range);
                            }
                        }
                        else
                        {
                            if (y - 1 >= 0 && World[x, y - 1])
                            {
                                pStartSlope = -GetSlope(x + 0.5, y - 0.5, Source.X, Source.Y, true);
                            }
                        }
                    }
                    y++;
                }
                y--;
                break;

            case 4:

                x = Source.X + pDepth;
                if (x >= World.GetLength(0))
                {
                    return(Ret);
                }

                y = Source.Y + Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (y >= World.GetLength(1))
                {
                    y = World.GetLength(1) - 1;
                }

                while (GetSlope(x, y, Source.X, Source.Y, true) >= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (y + 1 < World.GetLength(1) && !World[x, y + 1])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x - 0.5, y + 0.5, Source.X, Source.Y, true), range);
                            }
                        }
                        else
                        {
                            if (y + 1 < World.GetLength(1) && World[x, y + 1])
                            {
                                pStartSlope = GetSlope(x + 0.5, y + 0.5, Source.X, Source.Y, true);
                            }
                        }
                    }
                    y--;
                }
                y++;
                break;

            case 5:

                y = Source.Y + pDepth;
                if (y >= World.GetLength(1))
                {
                    return(Ret);
                }

                x = Source.X + Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (x >= World.GetLength(0))
                {
                    x = World.GetLength(0) - 1;
                }

                while (GetSlope(x, y, Source.X, Source.Y, false) >= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (x + 1 < World.GetLength(1) && !World[x + 1, y])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x + 0.5, y - 0.5, Source.X, Source.Y, false), range);
                            }
                        }
                        else
                        {
                            if (x + 1 < World.GetLength(1) &&
                                World[x + 1, y])
                            {
                                pStartSlope = GetSlope(x + 0.5, y + 0.5, Source.X, Source.Y, false);
                            }
                        }
                    }
                    x--;
                }
                x++;
                break;

            case 6:

                y = Source.Y + pDepth;
                if (y >= World.GetLength(1))
                {
                    return(Ret);
                }

                x = Source.X - Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (x < 0)
                {
                    x = 0;
                }

                while (GetSlope(x, y, Source.X, Source.Y, false) <= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (x - 1 >= 0 && !World[x - 1, y])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x - 0.5, y - 0.5, Source.X, Source.Y, false), range);
                            }
                        }
                        else
                        {
                            if (x - 1 >= 0 &&
                                World[x - 1, y])
                            {
                                pStartSlope = -GetSlope(x - 0.5, y + 0.5, Source.X, Source.Y, false);
                            }
                        }
                    }
                    x++;
                }
                x--;
                break;

            case 7:

                x = Source.X - pDepth;
                if (x < 0)
                {
                    return(Ret);
                }

                y = Source.Y + Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (y >= World.GetLength(1))
                {
                    y = World.GetLength(1) - 1;
                }

                while (GetSlope(x, y, Source.X, Source.Y, true) <= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (y + 1 < World.GetLength(1) && !World[x, y + 1])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x + 0.5, y + 0.5, Source.X, Source.Y, true), range);
                            }
                        }
                        else
                        {
                            if (y + 1 < World.GetLength(1) && World[x, y + 1])
                            {
                                pStartSlope = -GetSlope(x - 0.5, y + 0.5, Source.X, Source.Y, true);
                            }
                        }
                    }
                    y--;
                }
                y++;
                break;

            case 8:     //wnw

                x = Source.X - pDepth;
                if (x < 0)
                {
                    return(Ret);
                }

                y = Source.Y - Convert.ToInt32((pStartSlope * Convert.ToDouble(pDepth)));
                if (y < 0)
                {
                    y = 0;
                }

                while (GetSlope(x, y, Source.X, Source.Y, true) >= pEndSlope)
                {
                    if (GetVisDistance(x, y, Source.X, Source.Y) <= visrange2)
                    {
                        Ret[x, y] = true;
                        if (World[x, y])
                        {
                            if (y - 1 >= 0 && !World[x, y - 1])
                            {
                                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, GetSlope(x + 0.5, y - 0.5, Source.X, Source.Y, true), range);
                            }
                        }
                        else
                        {
                            if (y - 1 >= 0 && World[x, y - 1])
                            {
                                pStartSlope = GetSlope(x - 0.5, y - 0.5, Source.X, Source.Y, true);
                            }
                        }
                    }
                    y++;
                }
                y--;
                break;
            }

            if (x < 0)
            {
                x = 0;
            }
            else if (x >= World.GetLength(0))
            {
                x = World.GetLength(0) - 1;
            }

            if (y < 0)
            {
                y = 0;
            }
            else if (y >= World.GetLength(1))
            {
                y = World.GetLength(1) - 1;
            }

            if (pDepth < range & !World[x, y])
            {
                ScanOctant(Source, World, Ret, pDepth + 1, pOctant, pStartSlope, pEndSlope, range);
            }
            return(Ret);
        }