初步解析Java中AffineTransform類的應用。本站提示廣大學習愛好者:(初步解析Java中AffineTransform類的應用)文章只能為提供參考,不一定能成為您想要的結果。以下是初步解析Java中AffineTransform類的應用正文
AffineTransform類描寫了一種二維仿射變換的功效,它是一種二維坐標到二維坐標之間的線性變換,堅持二維圖形的“平直性”(譯注:straightness,即變換後直線照樣直線不會打彎,圓弧照樣圓弧)和“平行性”(譯注:parallelness,實際上是指保二維圖形間的絕對地位關系不變,平行線照樣平行線,訂交直線的交角不變。年夜二學過的復變,“保形變換/保角變換”都還記得吧,數學就是霸道啊!)。仿射變換可以經由過程一系列的原子變換的復合來完成,包含:平移(Translation)、縮放(Scale)、翻轉(Flip)、扭轉(Rotation)和剪切(Shear)。
此類變換可以用一個3×3的矩陣來表現,其最初一行動(0, 0, 1)。該變換矩陣將原坐標(x, y)變換為新坐標(x', y'),這裡原坐標和新坐標皆視為最末一行動(1)的三維列向量,原列向量左乘變換矩陣獲得新的列向量:
[x'] [m00 m01 m02] [x] [m00*x+m01*y+m02] [y'] = [m10 m11 m12] [y] = [m10*x+m11*y+m12] [1 ] [ 0 0 1 ] [1] [ 1 ]
幾種典范的仿射變換:
public static AffineTransform getTranslateInstance(double tx, double ty)
平移變換,將每點挪動到(x+tx, y+ty),變換矩陣為:
[ 1 0 tx ] [ 0 1 ty ] [ 0 0 1 ]
(譯注:平移變換是一種“剛體變換”,rigid-body transformation,中學學過的物理,都曉得啥叫“剛體”吧,就是不會發生形變的幻想物體,平移固然不會轉變二維圖形的外形。同理,上面的“扭轉變換”也是剛體變換,而“縮放”、“錯切”都是會轉變圖形外形的。)
public static AffineTransform getScaleInstance(double sx, double sy)
縮放變換,將每點的橫坐標縮小(減少)至sx倍,縱坐標縮小(減少)至sy倍,變換矩陣為:
[ sx 0 0 ] [ 0 sy 0 ] [ 0 0 1 ]
public static AffineTransform getShearInstance(double shx, double shy)
剪切變換,變換矩陣為:
[ 1 shx 0 ] [ shy 1 0 ] [ 0 0 1 ]
相當於一個橫向剪切與一個縱向剪切的復合
[ 1 0 0 ][ 1 shx 0 ] [ shy 1 0 ][ 0 1 0 ] [ 0 0 1 ][ 0 0 1 ]
(譯注:“剪切變換”又稱“錯切變換”,指的是相似於四邊形不穩固性那種性質,街邊小市肆那種鐵拉門都見過吧?想象一下下面鐵條組成的菱形拉動的進程,那就是“錯切”的進程。)
public static AffineTransform getRotateInstance(double theta)
扭轉變換,目的圖形環繞原點順時針扭轉theta弧度,變換矩陣為:
[ cos(theta) -sin(theta) 0 ] [ sin(theta) cos(theta) 0 ] [ 0 0 1 ]
public static AffineTransform getRotateInstance(double theta, double x, double y)
扭轉變換,目的圖形以(x, y)為軸心順時針扭轉theta弧度,變換矩陣為:
[ cos(theta) -sin(theta) x-x*cos+y*sin] [ sin(theta) cos(theta) y-x*sin-y*cos ] [ 0 0 1 ]
相當於兩次平移變換與一次原點扭轉變換的復合:
[1 0 -x][cos(theta) -sin(theta) 0][1 0 x] [0 1 -y][sin(theta) cos(theta) 0][0 1 y] [0 0 1 ][ 0 0 1 ][0 0 1]
幾何中,一個向量空間停止一次線性變換並接上一個平移,這麼一個進程就稱為仿射變換或放射映照。
可以簡略地表現為:y = Ax + b ,個中有下標的字母表現向量,而粗體的字母A表現一個矩陣。
假如臨時沒法懂得也沒有關系(我也沒懂得 ^_^#),沒緊要,我們這裡僅應用了它的幾個特例:平移和扭轉變換。
依照通例,上面先把全部代碼貼出來:
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 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 method stub repaint(); } }
比較可知,仿射變換的次序是不克不及隨意交流的。