litesoft
@ 804
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.core.simpletypes; import org.litesoft.core.simpletypes.nonpublic.*; import org.litesoft.core.typeutils.*; public final class Rect extends CompareSupport<Rect> implements SimpleType { private PointXY mPointXY; private SizeXY mSizeXY; /** * @deprecated - for Serialization */ protected Rect() { } public Rect( PointXY pPointXY, SizeXY pSizeXY ) { Objects.assertNotNull( "PointXY", mPointXY = pPointXY ); Objects.assertNotNull( "SizeXY", mSizeXY = pSizeXY ); } public Rect( Line pLineX, Line pLineY ) { Objects.assertNotNull( "LineX", pLineX ); Objects.assertNotNull( "LineY", pLineY ); mPointXY = new PointXY( pLineX.getOffset(), pLineY.getOffset() ); mSizeXY = new SizeXY( pLineX.getLength(), pLineY.getLength() ); } public Rect( int pXpos, int pYpos, int pXsize, int pYsize ) { mPointXY = new PointXY( pXpos, pYpos ); mSizeXY = new SizeXY( pXsize, pYsize ); } public PointXY getPointXY() { return mPointXY; } public SizeXY getSizeXY() { return mSizeXY; } public int getXpos() { return mPointXY.getX(); } public int getYpos() { return mPointXY.getY(); } public int getXsize() { return mSizeXY.getX(); } public int getYsize() { return mSizeXY.getY(); } public int getXleft() { return getXpos(); } public int getXright() { return getXpos() + getXsize(); } public int getYtop() { return getYpos(); } public int getYbottom() { return getYpos() + getYsize(); } public PointXY getRightBottomPointXY() { return new PointXY( getXright(), getYbottom() ); } public Line getXline() { return new Line( getXpos(), getXsize() ); } public Line getYline() { return new Line( getYpos(), getYsize() ); } private boolean doesOurXlineObscure( int pX ) { return (getXleft() <= pX) && (pX <= getXright()); } private boolean doesOurYlineObscure( int pY ) { return (getYtop() <= pY) && (pY <= getYbottom()); } /** * Determine if a PointXY (pPointXY) would be obscured by (covered by / inclusively contained by) 'this'. * * @param pPointXY !null */ public boolean obscures( PointXY pPointXY ) { return doesOurXlineObscure( pPointXY.getX() ) && doesOurYlineObscure( pPointXY.getY() ); } /** * Determine if a Rect (pThem) would be completely obscured by (covered by / completely inclusively contained by) 'this'. * * @param pThem !null */ public boolean obscures( Rect pThem ) { return obscures( pThem.getPointXY() ) && obscures( pThem.getRightBottomPointXY() ); } public boolean equals( Object o ) { return (this == o) || // ((o instanceof Rect) && equals( (Rect) o )); } public boolean equals( Rect them ) { return (this == them) || // ((them != null) // && equal( this.mPointXY, them.mPointXY ) // && equal( this.mSizeXY, them.mSizeXY ) // ); } public int hashCode() { return hashCodeEm( calcHashCode( mPointXY ), // calcHashCode( mSizeXY ) ); } /** * Order by Points and then Area */ public int compareTo( Rect them ) { return compareEm( compare( this.mPointXY, them.mPointXY ), // compare( this.mSizeXY, them.mSizeXY ) ); } public String toString() { return toStringBuilder().toString(); } public StringBuilder toStringBuilder() { return appendTo( new StringBuilder() ); } public StringBuilder appendTo( StringBuilder pSB ) { return pSB.append( "pos" ).append( mPointXY ).append( " size" ).append( mSizeXY ); } /** * @return this or a new Rect if pNewPointXY is !null */ public Rect moveTo( PointXY pNewPointXY ) { return (pNewPointXY == null) ? this : new Rect( pNewPointXY, getSizeXY() ); } /** * @return this or a new Rect if pNewSizeXY is !null */ public Rect resizeTo( SizeXY pNewSizeXY ) { return (pNewSizeXY == null) ? this : new Rect( getPointXY(), pNewSizeXY ); } /** * @return this or a new Rect if size &/ position was changed */ public Rect adjustToBeObscuredBy( Rect pThem ) { if ( pThem == null ) { throw new IllegalArgumentException( "Can NOT shrink to be obscured by 'null' Rectangle" ); } if ( pThem.obscures( this ) ) { return this; } Line zNewXline = this.getXline().adjustToBeObscuredBy( pThem.getXline() ); Line zNewYline = this.getYline().adjustToBeObscuredBy( pThem.getYline() ); return new Rect( zNewXline, zNewYline ); } } |