コード例 #1
0
ファイル: Find.cs プロジェクト: PtrMan/ai2
        //
        //     Zzzzz
        //     zYyyz
        //     zyXyz
        //     zyyyz
        //     zzzzz
        //
        public static Vector2<int> findNearestPositionWhereMapIs(bool value, Vector2<int> position, Map2d<bool> image, uint radius, out bool found)
        {
            Vector2<int> outwardIteratorOffsetUnbound;
            Vector2<int> borderMin;
            Vector2<int> borderMax;
            Vector2<int> one;
            Vector2<int> positionAsInt;

            found = false;

            outwardIteratorOffsetUnbound = new Vector2<int>();
            outwardIteratorOffsetUnbound.x = 0;
            outwardIteratorOffsetUnbound.y = 0;

            borderMin = new Vector2<int>();
            borderMin.x = 0;
            borderMin.y = 0;

            borderMax = new Vector2<int>();
            borderMax.x = (int)image.getWidth();
            borderMax.y = (int)image.getLength();

            positionAsInt = new Vector2<int>();
            positionAsInt.x = (int)position.x;
            positionAsInt.y = (int)position.y;

            one = new Vector2<int>();
            one.x = 1;
            one.y = 1;

            for(;;)
            {
                Vector2<int> iteratorOffsetBoundMin;
                Vector2<int> iteratorOffsetBoundMax;
                int x, y;

                if (-outwardIteratorOffsetUnbound.x > radius)
                {
                    break;
                }

                iteratorOffsetBoundMin = Vector2<int>.max(borderMin, outwardIteratorOffsetUnbound + positionAsInt, outwardIteratorOffsetUnbound + positionAsInt, outwardIteratorOffsetUnbound + positionAsInt);
                iteratorOffsetBoundMax = Vector2<int>.min(borderMax, outwardIteratorOffsetUnbound.getScaled(-1) + one + positionAsInt, borderMax, borderMax);

                for (y = (int)(iteratorOffsetBoundMin.y); y < iteratorOffsetBoundMax.y; y++ )
                {
                    for( x = (int)(iteratorOffsetBoundMin.x); x < iteratorOffsetBoundMax.x; x++ )
                    {
                        // just find at the border
                        if (y == (int)(iteratorOffsetBoundMin.y) || y == iteratorOffsetBoundMax.y - 1 || x == (int)(iteratorOffsetBoundMin.x) || x == iteratorOffsetBoundMax.x - 1)
                        {
                            bool valueAtPoint;

                            valueAtPoint = image.readAt(x, y);

                            if (valueAtPoint == value)
                            {
                                found = true;

                                Vector2<int> result;

                                result = new Vector2<int>();
                                result.x = x;
                                result.y = y;

                                return result;
                            }
                        }
                    }
                }

                outwardIteratorOffsetUnbound.x--;
                outwardIteratorOffsetUnbound.y--;
            }

            found = false;

            return new Vector2<int>();
        }
コード例 #2
0
        //
        //     Zzzzz
        //     zYyyz
        //     zyXyz
        //     zyyyz
        //     zzzzz
        //
        private static List<Vector2<int>> calculateRelativePositionsForRadius(int radius)
        {
            List<Vector2<int>> resultOffsets;

            Vector2<int> outwardIteratorOffsetUnbound;
            Vector2<int> one;

            outwardIteratorOffsetUnbound = new Vector2<int>();
            outwardIteratorOffsetUnbound.x = 0;
            outwardIteratorOffsetUnbound.y = 0;

            one = new Vector2<int>();
            one.x = 1;
            one.y = 1;

            resultOffsets = new List<Vector2<int>>();

            for (; ; )
            {
                Vector2<int> iteratorOffsetBoundMin;
                Vector2<int> iteratorOffsetBoundMax;
                int x, y;

                if (-outwardIteratorOffsetUnbound.x > radius)
                {
                    break;
                }

                iteratorOffsetBoundMin = outwardIteratorOffsetUnbound;
                iteratorOffsetBoundMax = outwardIteratorOffsetUnbound.getScaled(-1) + one;

                for (y = (int)(iteratorOffsetBoundMin.y); y < iteratorOffsetBoundMax.y; y++)
                {
                    for (x = (int)(iteratorOffsetBoundMin.x); x < iteratorOffsetBoundMax.x; x++)
                    {
                        // just add the border
                        if( y == (int)(iteratorOffsetBoundMin.y) || y == iteratorOffsetBoundMax.y - 1 || x ==  (int)(iteratorOffsetBoundMin.x) || x == iteratorOffsetBoundMax.x - 1 )
                        {
                            Vector2<int> newOffset;

                            newOffset = new Vector2<int>();
                            newOffset.x = x;
                            newOffset.y = y;

                            resultOffsets.Add(newOffset);
                        }

                    }
                }

                outwardIteratorOffsetUnbound.x--;
                outwardIteratorOffsetUnbound.y--;
            }

            return resultOffsets;
        }