affinetransform.java

来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,366 行 · 第 1/3 页

JAVA
1,366
字号
	 * <pre>
	 * [ sx 0  0 ]
	 * [ 0  sy 0 ]
	 * [ 0  0  1 ]
	 * </pre>
	 *
	 * @param sx the x scaling factor
	 * @param sy the y scaling factor
	 * @return the scaling transform
	 */
	public static AffineTransform getScaleInstance(double sx, double sy) {
		AffineTransform t = new AffineTransform();
		t.setToScale(sx, sy);
		return t;
	}

	/**
	 * Returns a shearing transform (points are shifted in the x direction based
	 * on a factor of their y coordinate, and in the y direction as a factor of
	 * their x coordinate):
	 * <pre>
	 * [  1  shx 0 ]
	 * [ shy  1  0 ]
	 * [  0   0  1 ]
	 * </pre>
	 *
	 * @param shx the x shearing factor
	 * @param shy the y shearing factor
	 * @return the shearing transform
	 */
	public static AffineTransform getShearInstance(double shx, double shy) {
		AffineTransform t = new AffineTransform();
		t.setToShear(shx, shy);
		return t;
	}

	/**
	 * Returns the type of this transform. The result is always valid, although
	 * it may not be the simplest interpretation (in other words, there are
	 * sequences of transforms which reduce to something simpler, which this
	 * does not always detect). The result is either TYPE_GENERAL_TRANSFORM,
	 * or a bit-wise combination of TYPE_TRANSLATION, the mutually exclusive
	 * TYPE_*_ROTATIONs, and the mutually exclusive TYPE_*_SCALEs.
	 *
	 * @see #TYPE_IDENTITY
	 * @see #TYPE_TRANSLATION
	 * @see #TYPE_UNIFORM_SCALE
	 * @see #TYPE_GENERAL_SCALE
	 * @see #TYPE_QUADRANT_ROTATION
	 * @see #TYPE_GENERAL_ROTATION
	 * @see #TYPE_GENERAL_TRANSFORM
	 */
	public int getType() {
		return type;
	}

	/**
	 * Return the determinant of this transform matrix. If the determinant is
	 * non-zero, the transform is invertible; otherwise operations which require
	 * an inverse throw a NoninvertibleTransformException. A result very near
	 * zero, due to rounding errors, may indicate that inversion results do not
	 * carry enough precision to be meaningful.
	 *
	 * <p>If this is a uniform scale transformation, the determinant also
	 * represents the squared value of the scale. Otherwise, it carries little
	 * additional meaning. The determinant is calculated as:
	 * <pre>
	 * | m00 m01 m02 |
	 * | m10 m11 m12 | = m00 * m11 - m01 * m10
	 * |  0   0   1  |
	 * </pre>
	 *
	 * @return the determinant
	 * @see #createInverse()
	 */
	public double getDeterminant() {
		return m00 * m11 - m01 * m10;
	}

	/**
	 * Return the matrix of values used in this transform. If the matrix has
	 * fewer than 6 entries, only the scale and shear factors are returned;
	 * otherwise the translation factors are copied as well. The resulting
	 * values are:
	 * <pre>
	 * [ d[0] d[2] (d[4]) ]
	 * [ d[1] d[3] (d[5]) ]
	 * [  0     0    1    ]
	 * </pre>
	 *
	 * @param d the matrix to store the results into; with 4 (6) entries
	 * @throws NullPointerException if d is null
	 * @throws ArrayIndexOutOfBoundsException if d is too small
	 */
	public void getMatrix(double[] d) {
		d[0] = m00;
		d[1] = m10;
		d[2] = m01;
		d[3] = m11;
		if (d.length >= 6) {
			d[4] = m02;
			d[5] = m12;
		}
	}

	/**
	 * Returns the X coordinate scaling factor of the matrix.
	 *
	 * @return m00
	 * @see #getMatrix(double[])
	 */
	public double getScaleX() {
		return m00;
	}

	/**
	 * Returns the Y coordinate scaling factor of the matrix.
	 *
	 * @return m11
	 * @see #getMatrix(double[])
	 */
	public double getScaleY() {
		return m11;
	}

	/**
	 * Returns the X coordinate shearing factor of the matrix.
	 *
	 * @return m01
	 * @see #getMatrix(double[])
	 */
	public double getShearX() {
		return m01;
	}

	/**
	 * Returns the Y coordinate shearing factor of the matrix.
	 *
	 * @return m10
	 * @see #getMatrix(double[])
	 */
	public double getShearY() {
		return m10;
	}

	/**
	 * Returns the X coordinate translation factor of the matrix.
	 *
	 * @return m02
	 * @see #getMatrix(double[])
	 */
	public double getTranslateX() {
		return m02;
	}

	/**
	 * Returns the Y coordinate translation factor of the matrix.
	 *
	 * @return m12
	 * @see #getMatrix(double[])
	 */
	public double getTranslateY() {
		return m12;
	}

	/**
	 * Concatenate a translation onto this transform. This is equivalent, but
	 * more efficient than
	 * <code>concatenate(AffineTransform.getTranslateInstance(tx, ty))</code>.
	 *
	 * @param tx the x translation distance
	 * @param ty the y translation distance
	 * @see #getTranslateInstance(double, double)
	 * @see #concatenate(AffineTransform)
	 */
	public void translate(double tx, double ty) {
		m02 += tx * m00 + ty * m01;
		m12 += tx * m10 + ty * m11;
		updateType();
	}

	/**
	 * Concatenate a rotation onto this transform. This is equivalent, but
	 * more efficient than
	 * <code>concatenate(AffineTransform.getRotateInstance(theta))</code>.
	 *
	 * @param theta the rotation angle
	 * @see #getRotateInstance(double)
	 * @see #concatenate(AffineTransform)
	 */
	public void rotate(double theta) {
		double c = Math.cos(theta);
		double s = Math.sin(theta);
		double n00 = m00 * c + m01 * s;
		double n01 = m00 * -s + m01 * c;
		double n10 = m10 * c + m11 * s;
		double n11 = m10 * -s + m11 * c;
		m00 = n00;
		m01 = n01;
		m10 = n10;
		m11 = n11;
		updateType();
	}

	/**
	 * Concatenate a rotation about a point onto this transform. This is
	 * equivalent, but more efficient than
	 * <code>concatenate(AffineTransform.getRotateInstance(theta, x, y))</code>.
	 *
	 * @param theta the rotation angle
	 * @param x the x coordinate of the pivot point
	 * @param y the y coordinate of the pivot point
	 * @see #getRotateInstance(double, double, double)
	 * @see #concatenate(AffineTransform)
	 */
	public void rotate(double theta, double x, double y) {
		translate(x, y);
		rotate(theta);
		translate(-x, -y);
	}

	/**
	 * Concatenate a scale onto this transform. This is equivalent, but more
	 * efficient than
	 * <code>concatenate(AffineTransform.getScaleInstance(sx, sy))</code>.
	 *
	 * @param sx the x scaling factor
	 * @param sy the y scaling factor
	 * @see #getScaleInstance(double, double)
	 * @see #concatenate(AffineTransform)
	 */
	public void scale(double sx, double sy) {
		m00 *= sx;
		m01 *= sy;
		m10 *= sx;
		m11 *= sy;
		updateType();
	}

	/**
	 * Concatenate a shearing onto this transform. This is equivalent, but more
	 * efficient than
	 * <code>concatenate(AffineTransform.getShearInstance(sx, sy))</code>.
	 *
	 * @param shx the x shearing factor
	 * @param shy the y shearing factor
	 * @see #getShearInstance(double, double)
	 * @see #concatenate(AffineTransform)
	 */
	public void shear(double shx, double shy) {
		double n00 = m00 + shx * m01;
		double n01 = shx * m00 + m01;
		double n10 = m10 * shy + m11;
		double n11 = shx * m10 + m11;
		m00 = n00;
		m01 = n01;
		m10 = n10;
		m11 = n11;
		updateType();
	}

	/**
	 * Reset this transform to the identity (no transformation):
	 * <pre>
	 * [ 1 0 0 ]
	 * [ 0 1 0 ]
	 * [ 0 0 1 ]
	 * </pre>
	 */
	public void setToIdentity() {
		m00 = m11 = 1;
		m01 = m02 = m10 = m12 = 0;
		type = TYPE_IDENTITY;
	}

	/**
	 * Set this transform to a translation:
	 * <pre>
	 * [ 1 0 tx ]
	 * [ 0 1 ty ]
	 * [ 0 0 1  ]
	 * </pre>
	 *
	 * @param tx the x translation distance
	 * @param ty the y translation distance
	 */
	public void setToTranslation(double tx, double ty) {
		m00 = m11 = 1;
		m01 = m10 = 0;
		m02 = tx;
		m12 = ty;
		type = (tx == 0 && ty == 0) ? TYPE_UNIFORM_SCALE : TYPE_TRANSLATION;
	}

	/**
	 * Set this transform to a rotation. A positive angle (in radians) rotates
	 * the positive x-axis to the positive y-axis:
	 * <pre>
	 * [ cos(theta) -sin(theta) 0 ]
	 * [ sin(theta)  cos(theta) 0 ]
	 * [     0           0      1 ]
	 * </pre>
	 *
	 * @param theta the rotation angle
	 */
	public void setToRotation(double theta) {
		double c = Math.cos(theta);
		double s = Math.sin(theta);
		m00 = c;
		m01 = -s;
		m02 = 0;
		m10 = s;
		m11 = c;
		m12 = 0;
		type = (c == 1 ? TYPE_IDENTITY : c == 0 || c == -1 ? TYPE_QUADRANT_ROTATION : TYPE_GENERAL_ROTATION);
	}

	/**
	 * Set this transform to a rotation about a point. A positive angle (in
	 * radians) rotates the positive x-axis to the positive y-axis. This is the
	 * same as calling:
	 * <pre>
	 * tx.setToTranslation(x, y);
	 * tx.rotate(theta);
	 * tx.translate(-x, -y);
	 * </pre>
	 *
	 * <p>The resulting matrix is: 
	 * <pre>
	 * [ cos(theta) -sin(theta) x-x*cos+y*sin ]
	 * [ sin(theta)  cos(theta) y-x*sin-y*cos ]
	 * [     0           0            1       ]
	 * </pre>
	 *
	 * @param theta the rotation angle
	 * @param x the x coordinate of the pivot point
	 * @param y the y coordinate of the pivot point
	 */
	public void setToRotation(double theta, double x, double y) {
		double c = Math.cos(theta);
		double s = Math.sin(theta);
		m00 = c;
		m01 = -s;
		m02 = x - x * c + y * s;
		m10 = s;
		m11 = c;
		m12 = y - x * s - y * c;
		updateType();
	}

	/**
	 * Set this transform to a scale:
	 * <pre>
	 * [ sx 0  0 ]
	 * [ 0  sy 0 ]
	 * [ 0  0  1 ]
	 * </pre>
	 *
	 * @param sx the x scaling factor
	 * @param sy the y scaling factor
	 */
	public void setToScale(double sx, double sy) {
		m00 = sx;
		m01 = m02 = m10 = m12 = 0;
		m11 = sy;
		type = (sx != sy ? TYPE_GENERAL_SCALE : sx == 1 ? TYPE_IDENTITY : TYPE_UNIFORM_SCALE);
	}

	/**
	 * Set this transform to a shear (points are shifted in the x direction based
	 * on a factor of their y coordinate, and in the y direction as a factor of
	 * their x coordinate):
	 * <pre>
	 * [  1  shx 0 ]
	 * [ shy  1  0 ]
	 * [  0   0  1 ]
	 * </pre>
	 *
	 * @param shx the x shearing factor
	 * @param shy the y shearing factor
	 */
	public void setToShear(double shx, double shy) {
		m00 = m11 = 1;
		m01 = shx;
		m10 = shy;
		m02 = m12 = 0;
		updateType();
	}

	/**
	 * Set this transform to a copy of the given one.
	 *
	 * @param tx the transform to copy
	 * @throws NullPointerException if tx is null
	 */
	public void setTransform(AffineTransform tx) {
		m00 = tx.m00;
		m01 = tx.m01;
		m02 = tx.m02;
		m10 = tx.m10;
		m11 = tx.m11;
		m12 = tx.m12;
		type = tx.type;
	}

	/**
	 * Set this transform to the given values:
	 * <pre>
	 * [ m00 m01 m02 ]
	 * [ m10 m11 m12 ]
	 * [  0   0   1  ]
	 * </pre>
	 *
	 * @param m00 the x scaling component
	 * @param m10 the y shearing component
	 * @param m01 the x shearing component
	 * @param m11 the y scaling component
	 * @param m02 the x translation component
	 * @param m12 the y translation component
	 */
	public void setTransform(double m00, double m10, double m01, double m11, double m02, double m12) {
		this.m00 = m00;
		this.m10 = m10;
		this.m01 = m01;
		this.m11 = m11;
		this.m02 = m02;
		this.m12 = m12;
		updateType();
	}

	/**
	 * Set this transform to the result of performing the original version of
	 * this followed by tx. This is commonly used when chaining transformations
	 * from one space to another. In matrix form:
	 * <pre>
	 * [ this ] = [ this ] x [ tx ]
	 * </pre>
	 *
	 * @param tx the transform to concatenate
	 * @throws NullPointerException if tx is null
	 * @see #preConcatenate(AffineTransform)
	 */
	public void concatenate(AffineTransform tx) {
		double n00 = m00 * tx.m00 + m01 * tx.m10;
		double n01 = m00 * tx.m01 + m01 * tx.m11;
		double n02 = m00 * tx.m02 + m01 * tx.m12 + m02;
		double n10 = m10 * tx.m00 + m11 * tx.m10;
		double n11 = m10 * tx.m01 + m11 * tx.m11;
		double n12 = m10 * tx.m02 + m11 * tx.m12 + m12;
		m00 = n00;
		m01 = n01;
		m02 = n02;
		m10 = n10;
		m11 = n11;
		m12 = n12;
		updateType();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?