001package armyc2.c2sd.renderer.utilities;
002
003import android.graphics.Rect;
004import android.graphics.RectF;
005
006import armyc2.c2sd.graphics2d.Rectangle;
007
008public class RectUtilities {
009        
010        public static Rect makeRect(int x, int y, int w, int h)
011        {
012                return new Rect(x, y, x + w, y + h);
013        }
014        
015        public static Rect makeRect(float x, float y, float w, float h)
016        {
017                return new Rect((int)x, (int)y, (int)(w+0.5f), (int)(h+0.5f));
018        }
019        
020        public static RectF makeRectF(float x, float y, float w, float h)
021        {
022                return new RectF(x, y, x + w, y + h);
023        }
024        
025        public static Rect makeRectFromRectF(RectF rect)
026        {
027                return new Rect((int)rect.left, (int)rect.top, (int)(rect.right+0.5), (int)(rect.bottom+0.5));
028        }
029        
030        public static void grow(Rect rect, int size)
031        {
032                rect.set(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size);
033                //return new Rect(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size);
034        }
035        
036        public static void grow(RectF rect, int size)
037        {
038                rect.set(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size);
039        }
040
041        public static void grow(Rectangle rect, int size) {
042                rect.setRect(new Rectangle(rect.getX() - size, rect.getY() - size, rect.getWidth() + (size*2), rect.getHeight() + (size*2)));
043                //return new Rectangle2D.Double(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size);
044        }
045        
046        public static void shift(Rect rect, int x, int y)
047        {
048                rect.offset(x, y);
049        }
050        
051        public static void shift(RectF rect, int x, int y)
052        {
053                rect.offset(x, y);
054        }
055        
056        public static int getCenterX(Rect rect)
057        {
058                return Math.round(rect.left + (rect.right - rect.left)/2);
059        }
060        public static float getCenterX(RectF rect)
061        {
062                return (rect.left + (rect.right - rect.left)/2);
063        }
064        
065        public static int getCenterY(Rect rect)
066        {
067                return Math.round(rect.top + (rect.bottom - rect.top)/2);
068        }
069        public static float getCenterY(RectF rect)
070        {
071                return (rect.top + (rect.bottom - rect.top)/2);
072        }
073        
074        public static void shiftBR(Rect rect, int x, int y)
075        {
076                rect.set(rect.left, rect.top, rect.right + x, rect.bottom + y);
077                //return new Rect(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size);
078        }
079        
080        public static void shiftBR(RectF rect, int x, int y)
081        {
082                rect.set(rect.left, rect.top, rect.right + x, rect.bottom + y);
083        }
084
085}