Preliminary analysis of the use of AffineTransform classes in Java _java

Source: Internet
Author: User
Tags cos sin stub

The AffineTransform class describes the function of a two-dimensional affine transformation, it is a linear transformation between two-dimensional coordinates to two-dimensional coordinates, maintaining the "straightness" of two-dimensional graphs (straightness, i.e., a straight or straight line without bending, arc or arc) and "parallelism". Parallelness, in fact, is to protect the relative position of two-dimensional graphics, parallel lines or parallel lines, the intersection of the same line of the same angle. Sophomore study of the complex, "conformal transformation/conformal transformation" All remember it, mathematics is the King Ah! )。 Affine transformations can be achieved through a series of compositing of atomic transformations, including: panning (translation), scaling (Scale), Flip (flip), rotation (rotation), and clipping (shear).

Such transformations can be expressed in a 3x3 matrix, with the last behavior (0, 0, 1). The transformation matrix transforms the original coordinates (x, y) into the new coordinates (x ', y '), where the original coordinates and the new coordinates are considered as the last behavior (1), and the new column vectors are obtained by the left multiplication matrix of the original column vector:

[x '] [M00 m01 m02] [x] [m00*x+m01*y+m02] 
[y '] = [M10 M11 M12] [y] = [M10*X+M11*Y+M12] 


Several typical affine transformations:

 
 


A translation transformation that moves each point to (X+TX, Y+ty) and transforms the matrix to:

[1 0 TX] 
[0 1 Ty] 


Translation transformation is a "rigid body transformation", rigid-body transformation, Middle School physics, all know what is called "rigid body" bar, is not produce deformation of the ideal object, translation of course will not change the shape of two-dimensional graphics. Similarly, the following "rotation transformation" is also a rigid body transformation, and "scaling", "wrong cut" will change the shape of the graphic. )

 
 


Scaling transformation, the horizontal axis of each point is enlarged (reduced) to SX times, ordinate zoom (zoom) to Sy times, transformation matrix is:

[SX 0 0] 
[0 Sy 0] 

public static AffineTransform getshearinstance (double shx, double shy) 

Shear Transformation, Transformation matrix is:

[1 Shx 0] 
[Shy 1 0] 
[0 0 1] 

Equivalent to a transverse shear and a longitudinal shear of the compound

[1 0 0] [1 Shx 0] 
[Shy 1 0] [0 1 0] 


The "shear Transformation", also known as the "staggered transformation", refers to the nature of the four-side instability, which has been seen in the small street shop? Imagine the diamond-pulling process of the top iron bars, which is the process of "wrong-cut". )

 
 


Rotation transformation, the target shape rotates clockwise theta radians around the origin, and the transformation matrix is:

[Cos (theta)-sin (theta) 0] 

[Sin (theta) cos (theta) 0] 
[0 0 1] 


 
 

Rotation transformation, the target figure in (x, y) axis clockwise rotation theta radians, transformation matrix is:

[Cos (theta)-sin (theta) X-x*cos+y*sin] 
[Sin (theta) cos (theta) Y-x*sin-y*cos] 


The equivalent of a two-time translation transformation and a rotational transformation of a primary point:

[1 0-x] [Cos (theta)-sin (theta) 0] [1 0 x] 
[0 1-y] [Sin (theta) cos (theta) 0] [0 1 Y] 

In geometry, a vector space carries out a linear transformation and then a translation, a process called an affine transformation or a radiation mapping.

Can be simply expressed as: y = Ax + B, where there is a subscript letter representation vector, while the bold letter a represents a matrix.

It doesn't matter if it's not understood at the moment (I don't understand ^_^ #), it doesn't matter, we only use a few of its special cases here: Translation and rotation transformations.

As usual, post the entire code first:

Import Java.applet.Applet;
Import Java.awt.BorderLayout;
Import Java.awt.Checkbox;
Import Java.awt.CheckboxGroup;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Panel;
Import java.awt.event.ItemEvent;
Import Java.awt.event.ItemListener;
Import Java.awt.geom.AffineTransform;
Import Java.awt.geom.Rectangle2D;


Import Java.util.Random;
 
 public class Affinetest extends Applet implements itemlistener{private rectangle2d rect;
 Private Checkbox Rotatefirst;
 
 Private Checkbox Translatefirst;
 public void init () {setlayout (New BorderLayout ());
 CheckboxGroup CBG = new CheckboxGroup ();
 panel p = new Panel ();
 Rotatefirst = new Checkbox ("Rotate, translate", CBG, True);
 Rotatefirst.additemlistener (this);
 P.add (Rotatefirst);
 Translatefirst = new Checkbox ("Translate, rotate", CBG, false);
 Translatefirst.additemlistener (this);
 P.add (Translatefirst);
 Add (P, Borderlayout.south);
 Rect = new Rectangle2d.float ( -0.5f, -0.5f, 1.0f, 1.0f);
} 
 public void Paint (Graphics g) {graphics2d g2d = (graphics2d) g;
 Final AffineTransform identify = new AffineTransform ();
 Boolean rotate = Rotatefirst.getstate ();
 Random r = new Random ();
 Final double Oneradian = Math.toradians (1.0);
  for (double radians = 0.0; radians < 2.0*math.pi; radians + = Oneradian) {g2d.settransform (identify);
  if (rotate) {g2d.translate (100, 100);
  G2d.rotate (radians);
  else {g2d.rotate (radians);
  G2d.translate (100, 100);
  } g2d.scale (100, 100);
  G2d.setcolor (New Color (R.nextint ()));
 G2d.fill (rect);
 @Override public void itemstatechanged (ItemEvent arg0) {//TODO auto-generated the method stub repaint ();
}} import Java.applet.Applet;
Import Java.awt.BorderLayout;
Import Java.awt.Checkbox;
Import Java.awt.CheckboxGroup;
Import Java.awt.Color;
Import Java.awt.Graphics;
Import Java.awt.Graphics2D;
Import Java.awt.Panel;
Import java.awt.event.ItemEvent;
Import Java.awt.event.ItemListener; Import Java.awt.geom.AffineTRansform;
Import Java.awt.geom.Rectangle2D;


Import Java.util.Random;
 
 public class Affinetest extends Applet implements itemlistener{private rectangle2d rect;
 Private Checkbox Rotatefirst;
 
 Private Checkbox Translatefirst;
 public void init () {setlayout (New BorderLayout ());
 CheckboxGroup CBG = new CheckboxGroup ();
 panel p = new Panel ();
 Rotatefirst = new Checkbox ("Rotate, translate", CBG, True);
 Rotatefirst.additemlistener (this);
 P.add (Rotatefirst);
 Translatefirst = new Checkbox ("Translate, rotate", CBG, false);
 Translatefirst.additemlistener (this);
 P.add (Translatefirst);
 Add (P, Borderlayout.south);
 Rect = new Rectangle2d.float ( -0.5f, -0.5f, 1.0f, 1.0f);
 } public void Paint (Graphics g) {graphics2d g2d = (graphics2d) g;
 Final AffineTransform identify = new AffineTransform ();
 Boolean rotate = Rotatefirst.getstate ();
 Random r = new Random ();
 Final double Oneradian = Math.toradians (1.0); for (double radians = 0.0; radians < 2.0*math.pi; radians + = OneradIan) {g2d.settransform (identify);
  if (rotate) {g2d.translate (100, 100);
  G2d.rotate (radians);
  else {g2d.rotate (radians);
  G2d.translate (100, 100);
  } g2d.scale (100, 100);
  G2d.setcolor (New Color (R.nextint ()));
 G2d.fill (rect);
 @Override public void itemstatechanged (ItemEvent arg0) {//TODO auto-generated the method stub repaint ();

 }

}


In contrast, the sequence of affine transformations cannot be exchanged freely.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.