001package armyc2.c5isr.renderer.utilities; 002 003import android.graphics.Rect; 004import android.graphics.RectF; 005 006import armyc2.c5isr.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 RectF makeRectFFromRect(Rect rect) 031 { 032 return new RectF((float)rect.left, (float)rect.top, (float)rect.right, (float)rect.bottom); 033 } 034 035 public static void grow(Rect rect, int size) 036 { 037 rect.set(rect.left - size, rect.top - size, (rect.right - size) + (size*2), (rect.bottom - size) + (size*2)); 038 //rect.set(rect.left - size, rect.top - size, rect.right + (size*2), rect.bottom + (size*2)); 039 //return new Rect(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size); 040 } 041 042 public static void grow(RectF rect, int size) 043 { 044 rect.set(rect.left - size, rect.top - size, (rect.right - size) + (size*2), (rect.bottom - size) + (size*2)); 045 //rect.set(rect.left - size, rect.top - size, rect.right + (size*2), rect.bottom + (size*2)); 046 } 047 048 public static void grow(Rectangle rect, int size) { 049 rect.setRect(new Rectangle(rect.getX() - size, rect.getY() - size, rect.getWidth() + (size*2), rect.getHeight() + (size*2))); 050 //return new Rectangle2D.Double(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size); 051 } 052 053 public static void shift(Rect rect, int x, int y) 054 { 055 rect.offset(x, y); 056 } 057 058 public static void shift(RectF rect, int x, int y) 059 { 060 rect.offset(x, y); 061 } 062 063 public static int getCenterX(Rect rect) 064 { 065 return Math.round(rect.left + (rect.right - rect.left)/2); 066 } 067 public static float getCenterX(RectF rect) 068 { 069 return (rect.left + (rect.right - rect.left)/2); 070 } 071 072 public static int getCenterY(Rect rect) 073 { 074 return Math.round(rect.top + (rect.bottom - rect.top)/2); 075 } 076 public static float getCenterY(RectF rect) 077 { 078 return (rect.top + (rect.bottom - rect.top)/2); 079 } 080 081 public static void shiftBR(Rect rect, int x, int y) 082 { 083 rect.set(rect.left, rect.top, rect.right + x, rect.bottom + y); 084 //return new Rect(rect.left - size, rect.top - size, rect.right + size, rect.bottom + size); 085 } 086 087 public static void shiftBR(RectF rect, int x, int y) 088 { 089 rect.set(rect.left, rect.top, rect.right + x, rect.bottom + y); 090 } 091 092}