Skip to content

Instantly share code, notes, and snippets.

@leoleozhu
Last active August 17, 2019 06:24
Show Gist options
  • Save leoleozhu/4f7ca9a54c87f56236292032e3a1d713 to your computer and use it in GitHub Desktop.
Save leoleozhu/4f7ca9a54c87f56236292032e3a1d713 to your computer and use it in GitHub Desktop.
Affine Transform Example
package document.transform;
import com.itextpdf.kernel.geom.AffineTransform;
import com.itextpdf.kernel.geom.Point;
import junit.framework.TestCase;
import org.junit.Test;
public class AffineTransformTests extends TestCase {
/**
* Test move a rectangle, move C to (0, 0)
* ------------------------------------
* | |
* | |
* | |
* | |
* | |
* | A(4,8) . . B(8,8) |
* | |
* | |
* | |
* | C(4,4) . . D(8,4) |
* | |
* | |
* | |
* | |
* ------------------------------------
*
* @throws Exception
*/
@Test
public void testMove() throws Exception{
Point A = new Point(4, 8);
Point B = new Point(8, 8);
Point C = new Point(4,4);
Point D = new Point(8, 4);
AffineTransform transformer = AffineTransform.getTranslateInstance(-C.x, -C.y);
Point newA = transformer.transform(A, null);
Point newB = transformer.transform(B, null);
Point newC = transformer.transform(C, null);
Point newD = transformer.transform(D, null);
assertEquals(0.0, newA.x,0.01);
assertEquals(4.0, newA.y,0.01);
assertEquals(4.0, newB.x,0.01);
assertEquals(4.0, newB.y,0.01);
assertEquals(0.0, newC.x,0.01);
assertEquals(0.0, newC.y,0.01);
assertEquals(4.0, newD.x,0.01);
assertEquals(0.0, newD.y,0.01);
}
/**
* Test Scale for a rectangle at the right top position, scale(x,y) = (1.5, 1.5)
* ------------------------------------
* | |
* | |
* | |
* | |
* | |
* | A(4,8) . . B(8,8) |
* | |
* | |
* | |
* | C(4,4) . . D(8,4) |
* | |
* | |
* | |
* | |
* ------------------------------------
*
* @throws Exception
*/
@Test
public void testScaleRightTop() throws Exception{
Point A = new Point(4, 8);
Point B = new Point(8, 8);
Point C = new Point(4,4);
Point D = new Point(8, 4);
AffineTransform transformer = AffineTransform.getTranslateInstance(-C.x, -C.y);
transformer.preConcatenate(AffineTransform.getScaleInstance(1.5, 1.5));
transformer.preConcatenate(AffineTransform.getTranslateInstance(C.x, C.y));
Point newA = transformer.transform(A, null);
Point newB = transformer.transform(B, null);
Point newC = transformer.transform(C, null);
Point newD = transformer.transform(D, null);
assertEquals(4.0, newA.x, 0.01);
assertEquals(10.0, newA.y, 0.01);
assertEquals(10.0, newB.x, 0.01);
assertEquals(10.0, newB.y, 0.01);
assertEquals(4.0, newC.x, 0.01);
assertEquals(4.0, newC.y, 0.01);
assertEquals(10.0, newD.x, 0.01);
assertEquals(4.0, newD.y, 0.01);
}
/**
* Test Scale for a rectangle at the right bottom position, scale(x,y) = (1.5, 1.5)
* ------------------------------------
* | |
* | |
* | |
* | |
* | |
* | A(4,8) . . B(8,8) |
* | |
* | |
* | |
* | C(4,4) . . D(8,4) |
* | |
* | |
* | |
* | |
* ------------------------------------
*
* @throws Exception
*/
@Test
public void testScaleRightBottom() throws Exception{
Point A = new Point(4, 8);
Point B = new Point(8, 8);
Point C = new Point(4,4);
Point D = new Point(8, 4);
AffineTransform transformer = AffineTransform.getTranslateInstance(-D.x, -D.y);
transformer.preConcatenate(AffineTransform.getScaleInstance(1.5, 1.5));
transformer.preConcatenate(AffineTransform.getTranslateInstance(D.x, D.y));
Point newA = transformer.transform(A, null);
Point newB = transformer.transform(B, null);
Point newC = transformer.transform(C, null);
Point newD = transformer.transform(D, null);
assertEquals(2.0, newA.x, 0.01);
assertEquals(10.0, newA.y, 0.01);
assertEquals(8.0, newB.x, 0.01);
assertEquals(10.0, newB.y, 0.01);
assertEquals(2.0, newC.x, 0.01);
assertEquals(4.0, newC.y, 0.01);
assertEquals(8.0, newD.x, 0.01);
assertEquals(4.0, newD.y, 0.01);
}
/**
* Test rotate a rectangle at the left top position, Counter Clock Wise 90 degree
* ------------------------------------
* | |
* | |
* | |
* | |
* | |
* | A(4,8) . . B(8,8) |
* | |
* | |
* | |
* | C(4,4) . . D(8,4) |
* | |
* | |
* | |
* | |
* ------------------------------------
*
* @throws Exception
*/
@Test
public void testRoteteLeftBottomCCW90() throws Exception{
Point A = new Point(4, 8);
Point B = new Point(8, 8);
Point C = new Point(4,4);
Point D = new Point(8, 4);
AffineTransform transformer = AffineTransform.getTranslateInstance(-A.x, -A.y);
transformer.preConcatenate(AffineTransform.getRotateInstance(Math.PI/2));
transformer.preConcatenate(AffineTransform.getTranslateInstance(A.x, A.y));
Point newA = transformer.transform(A, null);
Point newB = transformer.transform(B, null);
Point newC = transformer.transform(C, null);
Point newD = transformer.transform(D, null);
assertEquals(4.0, newA.x, 0.01);
assertEquals(8.0, newA.y, 0.01);
assertEquals(4.0, newB.x, 0.01);
assertEquals(12, newB.y, 0.01);
assertEquals(8.0, newC.x, 0.01);
assertEquals(8.0, newC.y, 0.01);
assertEquals(8.0, newD.x, 0.01);
assertEquals(12.0, newD.y, 0.01);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment