|
| 1 | +package com.codename1.ui.geom; |
| 2 | + |
| 3 | +import com.codename1.junit.FormTest; |
| 4 | +import com.codename1.junit.UITestBase; |
| 5 | +import com.codename1.ui.Transform; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +class AffineTransformFormTest extends UITestBase { |
| 10 | + |
| 11 | + @FormTest |
| 12 | + void testAffineTransformMatchesRotationMatrix() { |
| 13 | + double centerX = 150d; |
| 14 | + double centerY = 200d; |
| 15 | + double angle = Math.PI / 4d; |
| 16 | + |
| 17 | + AffineTransform affine = new AffineTransform(); |
| 18 | + affine.setToRotation(angle, centerX, centerY); |
| 19 | + Transform fromAffine = affine.toTransform(); |
| 20 | + |
| 21 | + Transform rotation = Transform.makeRotation((float) angle, (float) centerX, (float) centerY); |
| 22 | + |
| 23 | + float[][] points = new float[][]{ |
| 24 | + new float[]{100f, 140f}, |
| 25 | + new float[]{(float) centerX, (float) centerY}, |
| 26 | + new float[]{0f, 0f}, |
| 27 | + new float[]{(float) (centerX + 50), (float) (centerY - 30)} |
| 28 | + }; |
| 29 | + |
| 30 | + for (float[] point : points) { |
| 31 | + float[] affineResult = new float[3]; |
| 32 | + float[] rotationResult = new float[3]; |
| 33 | + fromAffine.transformPoint(point, affineResult); |
| 34 | + rotation.transformPoint(point, rotationResult); |
| 35 | + |
| 36 | + assertEquals(rotationResult[0], affineResult[0], 0.0001f, |
| 37 | + "X should match for point " + point[0] + "," + point[1]); |
| 38 | + assertEquals(rotationResult[1], affineResult[1], 0.0001f, |
| 39 | + "Y should match for point " + point[0] + "," + point[1]); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @FormTest |
| 44 | + void testAffineTransformRotatesPointAroundCenter() { |
| 45 | + double centerX = 75d; |
| 46 | + double centerY = 100d; |
| 47 | + double topY = 25d; |
| 48 | + double angle = Math.PI / 4d; |
| 49 | + |
| 50 | + AffineTransform affine = new AffineTransform(); |
| 51 | + affine.setToRotation(angle, centerX, centerY); |
| 52 | + Transform transform = affine.toTransform(); |
| 53 | + |
| 54 | + float[] input = new float[]{(float) centerX, (float) topY}; |
| 55 | + float[] output = transform.transformPoint(input); |
| 56 | + |
| 57 | + double sin = Math.sin(angle); |
| 58 | + double cos = Math.cos(angle); |
| 59 | + double expectedX = centerX - (topY - centerY) * sin; |
| 60 | + double expectedY = centerY + (topY - centerY) * cos; |
| 61 | + |
| 62 | + assertEquals(expectedX, output[0], 0.0001f); |
| 63 | + assertEquals(expectedY, output[1], 0.0001f); |
| 64 | + } |
| 65 | +} |
0 commit comments