/** * Returns a new transform representing a translation. * * @param x The x coordinate of the translation * @param y The y coordinate of the translation * @return The new translation transform */ public static DiscreteTransform2 fromTranslation(int x, int y) { return new DiscreteTransform2(Matrix3d.createTranslation(x, y)); }
/** * Adds a translation to this transform and returns it as a new transform. * * @param x The x coordinate of the translation * @param y The y coordinate of the translation * @return The translated transform as a copy */ public DiscreteTransform2 withTranslation(int x, int y) { return new DiscreteTransform2(this.matrix.translate(x, y)); }
/** * Returns a new transform representing a scaling on each axis. The scale * factors must be non-zero. * * @param x The scale factor on x * @param y The scale factor on y * @return The new scale transform */ public static DiscreteTransform2 fromScale(int x, int y) { Preconditions.checkArgument(x != 0, "x == 0"); Preconditions.checkArgument(y != 0, "y == 0"); return new DiscreteTransform2(Matrix3d.createScaling(x, y, 1)); }
/** * Adds a scale factor for each axis to this transform and returns it as a * new transform. The factors must be non-zero. * * @param x The scale factor on x * @param y The scale factor on y * @return The scaled transform as a copy */ public DiscreteTransform2 withScale(int x, int y) { Preconditions.checkArgument(x != 0, "x == 0"); Preconditions.checkArgument(y != 0, "y == 0"); return new DiscreteTransform2(this.matrix.scale(x, y, 1)); }
/** * Inverts the transform and returns it as a new transform. * * @return The inverse of this transform */ public DiscreteTransform2 invert() { return new DiscreteTransform2(this.matrix.invert()); }
/** * Returns a transform that is the composition of this transform and the * given transform. The result will apply this transformation after the * given one. * * @param that The transform to compose with * @return The new composed transform */ public DiscreteTransform2 compose(DiscreteTransform2 that) { return new DiscreteTransform2(this.matrix.mul(that.matrix)); }
/** * Returns a new transform from the given transformation matrix, if the * resulting transform would be discrete. * * @param matrix The matrix to use for the transform * @return The new transform, or {@link Optional#empty()} */ public static Optional<DiscreteTransform2> of(Matrix3d matrix) { if (Arrays.stream(matrix.toArray()) .anyMatch(value -> Math.rint(value) != value)) { return Optional.empty(); } return Optional.of(new DiscreteTransform2(matrix)); }
/** * Adds a rotation to this transform, in the xy plane, around the origin and * returns it as a new transform. The rotation is given is quarter turns. * The actual rotation is {@code quarterTurns * 90}. The rotation is around * the block center, not the corner. * * @param quarterTurns The number of quarter turns in this rotation * @return The rotated transform as a copy */ public DiscreteTransform2 withRotation(int quarterTurns) { return new DiscreteTransform2(this.matrix.rotate(Complexd.fromAngleDeg(quarterTurns * 90))); }
/** * Returns a new transform representing a rotation in the xy plane around * the origin. The rotation is given is quarter turns. The actual rotation * is {@code quarterTurns * 90}. The rotation is around the block center, * not the corner. * * @param quarterTurns The number of quarter turns in this rotation * @return The new rotation transform */ public static DiscreteTransform2 fromRotation(int quarterTurns) { return new DiscreteTransform2(Matrix3d.createRotation(Complexd.fromAngleDeg(quarterTurns * 90))); }
/** * Adds another transformation to this transformation and returns int as a * new transform. * * @param transform The transformation to add * @return The added transforms as a copy */ public DiscreteTransform2 withTransformation(DiscreteTransform2 transform) { return new DiscreteTransform2(transform.getMatrix().mul(getMatrix())); }
/** * Adds a a rotation to this transform, in the xy plane, around a given * point, and returns it as a new transform. The rotation is given is * quarter turns. The actual rotation is {@code quarterTurns * 90}. The tile * corner flag changes the point to be the tile upper corner instead of the * center. * * @param quarterTurns The number of quarter turns in this rotation * @param point The point of rotation, as tile coordinates * @param tileCorner Whether or not to use the corner of the tile instead of * the center * @return The rotated transform as a copy */ public DiscreteTransform2 withRotation(int quarterTurns, Vector2i point, boolean tileCorner) { Vector2d pointDouble = point.toDouble(); if (tileCorner) { pointDouble = pointDouble.add(0.5, 0.5); } return new DiscreteTransform2( this.matrix.translate(pointDouble.negate()).rotate(Complexd.fromAngleDeg(quarterTurns * 90)).translate(pointDouble)); }
/** * Returns a new transform representing a rotation in the xy plane, around a * given point. The rotation is given is quarter turns. The actual rotation * is {@code quarterTurns * 90}. The tile corner flag change the point to be * the tile corner instead of the center. * * @param quarterTurns The number of quarter turns in this rotation * @param point The point of rotation, as tile coordinates * @param tileCorner Whether or not to use the corner of the tile instead of * the center * @return The new rotation transform */ public static DiscreteTransform2 fromRotation(int quarterTurns, Vector2i point, boolean tileCorner) { Vector2d pointDouble = point.toDouble(); if (tileCorner) { pointDouble = pointDouble.add(0.5, 0.5); } return new DiscreteTransform2( Matrix3d.createTranslation(pointDouble.negate()).rotate(Complexd.fromAngleDeg(quarterTurns * 90)).translate(pointDouble)); }
/** * Returns a new transform representing a rotation in the xy plane, around a * given point. The rotation is given is half turns. The actual rotation is * {@code halfTurns * 180}. The tile corner flags change the point to be the * tile corner or edge instead of the center. When both flags are false, the * center is used. When only one is true the edge on the opposite axis to * the flag is used. When both are true the upper corner is used. * * @param halfTurns The number of half turns in this rotation * @param point The point of rotation, as tile coordinates * @param tileCornerX Whether or not to use the corner of the tile instead * of the center on the x axis * @param tileCornerY Whether or not to use the corner of the tile instead * of the center on the y axis * @return The new rotation transform */ public static DiscreteTransform2 fromRotation(int halfTurns, Vector2i point, boolean tileCornerX, boolean tileCornerY) { Vector2d pointDouble = point.toDouble(); if (tileCornerX) { pointDouble = pointDouble.add(0.5, 0); } if (tileCornerY) { pointDouble = pointDouble.add(0, 0.5); } return new DiscreteTransform2( Matrix3d.createTranslation(pointDouble.negate()).rotate(Complexd.fromAngleDeg(halfTurns * 180)).translate(pointDouble)); }
/** * Adds a a rotation to this transform, in the xy plane, around a given * point, and returns it as a new transform. The rotation is given is half * turns. The actual rotation is {@code halfTurns * 180}. The tile corner * flags change the point to be the tile corner or edge instead of the * center. When both flags are false, the center is used. When only one is * true the edge on the opposite axis to the flag is used. When both are * true the upper corner is used. * * @param halfTurns The number of half turns in this rotation * @param point The point of rotation, as tile coordinates * @param tileCornerX Whether or not to use the corner of the tile instead * of the center on the x axis * @param tileCornerY Whether or not to use the corner of the tile instead * of the center on the y axis * @return The rotated transform as a copy */ public DiscreteTransform2 withRotation(int halfTurns, Vector2i point, boolean tileCornerX, boolean tileCornerY) { Vector2d pointDouble = point.toDouble(); if (tileCornerX) { pointDouble = pointDouble.add(0.5, 0); } if (tileCornerY) { pointDouble = pointDouble.add(0, 0.5); } return new DiscreteTransform2( this.matrix.translate(pointDouble.negate()).rotate(Complexd.fromAngleDeg(halfTurns * 180)).translate(pointDouble)); }
/** * Inverts the transform and returns it as a new transform. * * @return The inverse of this transform */ public DiscreteTransform2 invert() { return new DiscreteTransform2(this.matrix.invert()); }
/** * Returns a new transform representing a translation. * * @param x The x coordinate of the translation * @param y The y coordinate of the translation * @return The new translation transform */ public static DiscreteTransform2 fromTranslation(int x, int y) { return new DiscreteTransform2(Matrix3d.createTranslation(x, y)); }
/** * Adds a translation to this transform and returns it as a new transform. * * @param x The x coordinate of the translation * @param y The y coordinate of the translation * @return The translated transform as a copy */ public DiscreteTransform2 withTranslation(int x, int y) { return new DiscreteTransform2(this.matrix.translate(x, y)); }
/** * Returns a transform that is the composition of this transform and the given transform. * * The result will apply this transformation after the given one. * * @param that The transform to compose with * @return The new composed transform */ public DiscreteTransform2 compose(DiscreteTransform2 that) { return new DiscreteTransform2(this.matrix.mul(that.matrix)); }
/** * Returns a new transform representing a scaling on each axis. The scale * factors must be non-zero. * * @param x The scale factor on x * @param y The scale factor on y * @return The new scale transform */ public static DiscreteTransform2 fromScale(int x, int y) { Preconditions.checkArgument(x != 0, "x == 0"); Preconditions.checkArgument(y != 0, "y == 0"); return new DiscreteTransform2(Matrix3d.createScaling(x, y, 1)); }
/** * Adds another transformation to this transformation and returns int as a * new transform. * * @param transform The transformation to add * @return The added transforms as a copy */ public DiscreteTransform2 withTransformation(DiscreteTransform2 transform) { return new DiscreteTransform2(transform.getMatrix().mul(getMatrix())); }