/**
  * Test that point features are rendered at the expected image coordinates when the map is
  * rotated. StreamingRenderer
  *
  * @throws Exception
  */
 @Test
 public void testRotatedTransform() throws Exception {
   // If we rotate the world rectangle + 90 degrees around (0,0), we get the screen rectangle
   final Rectangle screen = new Rectangle(0, 0, 100, 50);
   final Envelope world = new Envelope(0, 50, 0, -100);
   final AffineTransform worldToScreen =
       AffineTransform.getRotateInstance(Math.toRadians(90), 0, 0);
   DefaultFeatureCollection fc = new DefaultFeatureCollection();
   fc.add(createPoint(0, 0));
   fc.add(createPoint(world.getMaxX(), world.getMinY()));
   MapContext mapContext = new DefaultMapContext(DefaultGeographicCRS.WGS84);
   mapContext.addLayer((FeatureCollection) fc, createPointStyle());
   BufferedImage image =
       new BufferedImage(screen.width, screen.height, BufferedImage.TYPE_4BYTE_ABGR);
   final StreamingRenderer sr = new StreamingRenderer();
   sr.setContext(mapContext);
   sr.paint(image.createGraphics(), screen, worldToScreen);
   assertTrue("Pixel should be drawn at 0,0 ", image.getRGB(0, 0) != 0);
   assertTrue(
       "Pixel should not be drawn in image centre ",
       image.getRGB(screen.width / 2, screen.height / 2) == 0);
   assertTrue(
       "Pixel should be drawn at image max corner ",
       image.getRGB(screen.width - 1, screen.height - 1) != 0);
 }
public class VectorDefinitionTest {
  private static final double NoDifferenceAllowed = 0.0;
  private static final double TestInitX1 = 0.0;
  private static final double TestInitY1 = 0.0;
  private static final double TestInitX2 = 1.0;
  private static final double TestInitY2 = 1.0;
  private static final double TestInitLength = Math.sqrt(2.0);
  private static final double TestNewLength = 2.0 * TestInitLength;
  private static final double TestScale = 2.0;
  private static final double TestShiftedX2 = -1.0;
  private static final double TestShiftedY2 = -1.0;
  private static final double TestShiftValue = 7.8;
  private static final double TestRotation = Math.toRadians(45.0);
  private static final double TestRotatedX = 0.0;
  private static final double TestRotatedY = Math.sqrt(2.0);
  private static final double TestRotationAllowedDifference = Math.pow(10, -10);
  private static final double TestNewX1 = 3.0;
  private static final double TestNewY1 = 4.0;
  private static final double TestNewX2 = 5.0;
  private static final double TestNewY2 = 6.0;
  private VectorDefinition vectorDefinition;

  @Before
  public void
      setUp() // do not catch these, successful reflection must be successful to be able to test
          throws NoSuchMethodException, SecurityException, InstantiationException,
              IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Constructor<VectorDefinition> constructor =
        VectorDefinition.class.getDeclaredConstructor(
            double.class, double.class, double.class, double.class);
    constructor.setAccessible(true);
    vectorDefinition =
        (VectorDefinition) constructor.newInstance(TestInitX1, TestInitY1, TestInitX2, TestInitY2);
  }

  @Test
  public final void testGetLength() {
    assertEquals(TestInitLength, vectorDefinition.getLength(), NoDifferenceAllowed);
  }

  @Test
  public final void testAppendLength() {
    vectorDefinition.AppendLength(TestInitLength);
    assertEquals(TestNewLength, vectorDefinition.getLength(), NoDifferenceAllowed);
  }

  @Test
  public final void testScale() {
    vectorDefinition.Scale(TestScale);
    assertEquals(TestInitX2 * TestScale, vectorDefinition.getX2(), NoDifferenceAllowed);
    assertEquals(TestInitY2 * TestScale, vectorDefinition.getY2(), NoDifferenceAllowed);
  }

  @Test
  public final void testSetLength() {
    vectorDefinition.setLength(TestNewLength);
    assertEquals(TestNewLength, vectorDefinition.getLength(), NoDifferenceAllowed);
  }

  @Test
  public final void testShiftOrigin() {
    vectorDefinition.shiftOrigin();
    assertEquals(TestShiftedX2, vectorDefinition.getX2(), NoDifferenceAllowed);
    assertEquals(TestShiftedY2, vectorDefinition.getY2(), NoDifferenceAllowed);
  }

  @Test
  public final void testShiftOnArrow() {

    vectorDefinition.shiftOnArrow(TestShiftValue);
    assertEquals(
        TestInitX2 - TestInitX1,
        vectorDefinition.getX2() - vectorDefinition.getX1(),
        NoDifferenceAllowed);
    assertEquals(
        TestInitY2 - TestInitY1,
        vectorDefinition.getY2() - vectorDefinition.getY1(),
        NoDifferenceAllowed);
  }

  @Test
  public final void testRotate() {
    vectorDefinition.Rotate(TestRotation);
    assertEquals(TestRotatedX, vectorDefinition.getX2(), TestRotationAllowedDifference);
    assertEquals(TestRotatedY, vectorDefinition.getY2(), TestRotationAllowedDifference);
  }

  @Test
  public final void testGetX1() {
    assertEquals(TestInitX1, vectorDefinition.getX1(), NoDifferenceAllowed);
  }

  @Test
  public final void testSetX1() {
    vectorDefinition.setX1(TestNewX1);
    assertEquals(TestNewX1, vectorDefinition.getX1(), NoDifferenceAllowed);
  }

  @Test
  public final void testGetY1() {
    assertEquals(TestInitY1, vectorDefinition.getY1(), NoDifferenceAllowed);
  }

  @Test
  public final void testSetY1() {
    vectorDefinition.setY1(TestNewY1);
    assertEquals(TestNewY1, vectorDefinition.getY1(), NoDifferenceAllowed);
  }

  @Test
  public final void testGetX2() {
    assertEquals(TestInitX2, vectorDefinition.getX2(), NoDifferenceAllowed);
  }

  @Test
  public final void testSetX2() {
    vectorDefinition.setX2(TestNewX2);
    assertEquals(TestNewX2, vectorDefinition.getX2(), NoDifferenceAllowed);
  }

  @Test
  public final void testGetY2() {
    assertEquals(TestInitY2, vectorDefinition.getY2(), NoDifferenceAllowed);
  }

  @Test
  public final void testSetY2() {
    vectorDefinition.setY2(TestNewY2);
    assertEquals(TestNewY2, vectorDefinition.getY2(), NoDifferenceAllowed);
  }

  @Test
  public final void testGetPointStart() {
    I2DPoint resultPoint = vectorDefinition.getPointStart();
    assertEquals(TestInitX1, resultPoint.getX(), NoDifferenceAllowed);
    assertEquals(TestInitY1, resultPoint.getY(), NoDifferenceAllowed);
  }

  @Test
  public final void testGetPointEnd() {
    I2DPoint resultPoint = vectorDefinition.getPointEnd();
    assertEquals(TestInitX2, resultPoint.getX(), NoDifferenceAllowed);
    assertEquals(TestInitY2, resultPoint.getY(), NoDifferenceAllowed);
  }
}
  /**
   * Run the test in the flipped or unflipped case.
   *
   * @param f -1 for the flipped case, or +1 for the unflipped case.
   */
  private static void runTest(final int f) {
    // Test identity
    final AffineTransform tr = new AffineTransform();
    tr.setToScale(1, f);
    assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(0, XAffineTransform.getRotation(tr), EPS);
    assertEquals(1, XAffineTransform.getSwapXY(tr));
    assertEquals(f, XAffineTransform.getFlip(tr));
    assertEquals(f, getFlipFromType(tr));

    // Tests rotation (< 45°)
    double r = Math.toRadians(25);
    tr.rotate(r);
    assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(r, XAffineTransform.getRotation(tr), EPS);
    assertEquals(1, XAffineTransform.getSwapXY(tr));
    assertEquals(f, XAffineTransform.getFlip(tr));
    assertEquals(f, getFlipFromType(tr));

    // Tests more rotation (> 45°)
    r = Math.toRadians(65);
    tr.rotate(Math.toRadians(40));
    assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(r, XAffineTransform.getRotation(tr), EPS);
    assertEquals(-1, XAffineTransform.getSwapXY(tr));
    assertEquals(f, XAffineTransform.getFlip(tr));
    assertEquals(f, getFlipFromType(tr));

    // Tests scale
    tr.setToScale(2, 3 * f);
    assertEquals(2, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(3, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(0, XAffineTransform.getRotation(tr), EPS);
    assertEquals(1, XAffineTransform.getSwapXY(tr));
    assertEquals(f, XAffineTransform.getFlip(tr));
    assertEquals(f, getFlipFromType(tr));

    // Tests rotation + scale
    tr.rotate(r);
    assertEquals(2, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(3, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(r, XAffineTransform.getRotation(tr), EPS);
    assertEquals(-1, XAffineTransform.getSwapXY(tr));
    assertEquals(f, XAffineTransform.getFlip(tr));
    assertEquals(1, getFlipFromType(tr)); // Always unflipped according Java 1.5.0_09...

    // Tests axis swapping
    r = Math.toRadians(-90 * f);
    tr.setTransform(0, 1, f, 0, 0, 0);
    assertEquals(1, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(1, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(r, XAffineTransform.getRotation(tr), EPS);
    assertEquals(-1, XAffineTransform.getSwapXY(tr));
    assertEquals(-f, XAffineTransform.getFlip(tr));
    assertEquals(-f, getFlipFromType(tr));

    // Tests axis swapping + scale
    tr.scale(2, 3);
    assertEquals(3, XAffineTransform.getScaleX0(tr), EPS);
    assertEquals(2, XAffineTransform.getScaleY0(tr), EPS);
    assertEquals(r, XAffineTransform.getRotation(tr), EPS);
    assertEquals(-1, XAffineTransform.getSwapXY(tr));
    assertEquals(-f, XAffineTransform.getFlip(tr));
    assertEquals(-f, getFlipFromType(tr));
  }
  /** Test of moverPunto method, of class OperacionesGeométricas. */
  @Test
  public void testMoverPunto() {
    System.out.println("MoverPunto");

    // Mover distancia y ángulo 0
    Point2D.Double punto = new Point2D.Double(3, 3);
    double ángulo = 0.0F;
    double distancia = 0.0F;
    Point2D.Double expResult = punto;
    Point2D.Double result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    assertEquals(expResult, result);

    // Puntos Cardinales

    ángulo = Math.toRadians(0);
    distancia = 1;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(4, 3);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(90);
    distancia = 1;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 4);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(180);
    distancia = 1;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(2, 3);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(-90);
    distancia = 1;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 2);
    assertEquals(expResult, result);

    // Mover distancia 0
    ángulo = Math.toRadians(0);
    distancia = 0;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 3);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(90);
    distancia = 0;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 3);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(180);
    distancia = 0;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 3);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(270);
    distancia = 0;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 3);
    assertEquals(expResult, result);

    ángulo = Math.toRadians(33);
    distancia = 0;
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(3, 3);
    assertEquals(expResult, result);

    // Mover en diagonal
    double delta = 0.000001;

    distancia = Math.sqrt(2);

    ángulo = Math.toRadians(45);
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(4, 4);
    // assertEquals(expResult, result);
    assertTrue(estáEnRango(result.getX(), expResult.getX(), delta));
    assertTrue(estáEnRango(result.getY(), expResult.getY(), delta));

    ángulo = Math.toRadians(135);
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(2, 4);
    // assertEquals(expResult, result);
    assertTrue(estáEnRango(result.getX(), expResult.getX(), delta));
    assertTrue(estáEnRango(result.getY(), expResult.getY(), delta));

    ángulo = Math.toRadians(225);
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(2, 2);
    assertTrue(estáEnRango(result.getX(), expResult.getX(), delta));
    assertTrue(estáEnRango(result.getY(), expResult.getY(), delta));

    ángulo = Math.toRadians(315);
    result = OperacionesGeométricas.moverPunto(punto, ángulo, distancia);
    expResult = new Point2D.Double(4, 2);
    // assertEquals(expResult, result);
    assertTrue(estáEnRango(result.getX(), expResult.getX(), delta));
    assertTrue(estáEnRango(result.getY(), expResult.getY(), delta));

    // assertEquals(expResult, result);

  }