Created
October 20, 2011 03:23
-
-
Save elithompson/1300343 to your computer and use it in GitHub Desktop.
DimensionResizer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package dimensionresizer; | |
import java.awt.Point; | |
/** | |
* | |
* @author dev | |
*/ | |
public class DimensionResizer { | |
/** | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) { | |
// TODO code application logic here | |
} | |
private int _maxWidth; | |
public int getMaxWidth() | |
{ | |
return _maxWidth; | |
} | |
public void setMaxWidth(int width) | |
{ | |
_maxWidth = width; | |
} | |
public Point Resize(Point p) | |
{ | |
double ratio = p.x / (double)_maxWidth; | |
if (ratio <= 1) | |
return p; | |
int x = (int)Math.floor(p.x / ratio); | |
int y = (int)Math.floor(p.y / ratio); | |
return new Point(x, y); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this template, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package dimensionresizer; | |
import java.awt.Point; | |
import org.hamcrest.core.Is; | |
import org.junit.After; | |
import org.junit.AfterClass; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
import static org.junit.Assert.*; | |
/** | |
* | |
* @author dev | |
*/ | |
public class DimensionResizerTest { | |
public DimensionResizerTest() { | |
} | |
@BeforeClass | |
public static void setUpClass() throws Exception { | |
} | |
@AfterClass | |
public static void tearDownClass() throws Exception { | |
} | |
@Before | |
public void setUp() { | |
} | |
@After | |
public void tearDown() { | |
} | |
@Test | |
public void CanSetAndGetMaxWidth() | |
{ | |
DimensionResizer resizer = new DimensionResizer(); | |
int maxWidth = 1000; | |
resizer.setMaxWidth(maxWidth); | |
assertEquals(maxWidth, resizer.getMaxWidth()); | |
} | |
@Test | |
public void CanReduceToMaxWidth() { | |
// too small | |
CheckTheResize(500, 499, 500, 499, 500); | |
// really tall | |
CheckTheResize(500, 500, 2500, 500, 2500); | |
// just right | |
CheckTheResize(500, 500, 500, 500, 500); | |
// just a tad too large | |
CheckTheResize(500, 501, 1002, 500, 1000); | |
// too large | |
CheckTheResize(500, 1000, 1000, 500, 500); | |
// high precision | |
CheckTheResize(500, 666, 333, 500, 250); | |
// something silly | |
CheckTheResize(1, 2, 2, 1, 1); | |
} | |
private void CheckTheResize(int maxWidth, int inputX, int inputY, int expectedX, int expectedY) | |
{ | |
// arrange | |
DimensionResizer instance = new DimensionResizer(); | |
instance.setMaxWidth(maxWidth); | |
Point p = new Point(inputX, inputY); | |
// act | |
Point result = instance.Resize(p); | |
// assert | |
assertEquals(new Point(expectedX, expectedY), result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment