Silverlight 引路蜂二维图形库示例:坐标变换

jerry Silverligth 2015年11月26日 收藏

类AffineTransform用于二维平面上坐标变换。可以对坐标进行平移,缩放,旋转等变换。下面例子显示了坐标变换的用法。

  1. private void Transform()
  2. {
  3.  Path path;
  4.  
  5.  /* The first matrix */
  6.  AffineTransform matrix1 = new AffineTransform();
  7.  /* The second matrix */
  8.  AffineTransform matrix2 = new AffineTransform();
  9.  /* The third matrix */
  10.  AffineTransform matrix3 = new AffineTransform();
  11.  
  12.  /** Colors */
  13.  Color blackColor = new Color(0xff000000, false);
  14.  Color redColor = new Color(0xffff0000, false);
  15.  Color greenColor = new Color(0xff00ff00, false);
  16.  Color blueColor = new Color(0xff0000ff, false);
  17.  Color fillColor = new Color(0x4f0000ff, true);
  18.  /* Define the path */
  19.  path = new Path();
  20.  path.MoveTo(50, 0);
  21.  path.LineTo(0, 0);
  22.  path.LineTo(0, 50);
  23.  
  24.  /* Define the matrix1 as  "translate(50,50)"  */
  25.  matrix1.Translate(50, 50);
  26.  
  27.  /* Define the matrix2 as "translate(50,50) + rotate(-45)" */
  28.  matrix2 = new AffineTransform(matrix1);
  29.  AffineTransform m = new AffineTransform();
  30.  
  31.  m.Rotate(-45 * Math.PI / 180.0, 0, 0);
  32.  /* Concatenates the m  to the matrix2.
  33.   * [matrix2] =  [matrix2] * [m];
  34.   */
  35.  matrix2.Concatenate(m);
  36.  
  37.  /* Define the matrix3 as
  38.   * "translate(50,50) + rotate(-45) + translate(-20,80)"  */
  39.  /* Copy the matrix2 to the matrix3 */
  40.  matrix3 = new AffineTransform(matrix2);
  41.  m = new AffineTransform();
  42.  
  43.  m.Translate(-20, 80);
  44.  /* Concatenates the m  to the matrix3.
  45.   * [matrix3] =  [matrix3] * [m]
  46.   */
  47.  matrix3.Concatenate(m);
  48.  //Clear the canvas with white color.
  49.  graphics2D.Clear(Color.White);
  50.  
  51.  graphics2D.AffineTransform = (matrix1);
  52.  SolidBrush brush = new SolidBrush(fillColor);
  53.  Pen pen = new Pen(redColor, 4);
  54.  
  55.  graphics2D.SetPenAndBrush(pen, brush);
  56.  graphics2D.Draw(null, path);
  57.  
  58.  graphics2D.AffineTransform = (matrix2);
  59.  
  60.  pen = new Pen(greenColor, 4);
  61.  
  62.  graphics2D.SetPenAndBrush(pen, brush);
  63.  graphics2D.Draw(null, path);
  64.  graphics2D.AffineTransform = (matrix3);
  65.  
  66.  pen = new Pen(blueColor, 4);
  67.  
  68.  graphics2D.SetPenAndBrush(pen, brush);
  69.  graphics2D.Draw(null, path);
  70.  
  71. }