C#旋转(转换)问题 [英] C# rotation (transformation) issue

查看:233
本文介绍了C#旋转(转换)问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建程序,该程序将是在画布上绘制的鱼的列表. (下一步将根据一些计算来更改鱼的位置) 每条鱼都由位图表示(带有鱼图片的png文件7x12 px).

I'm trying to create program where will be List of fishes drawn on the canvas. (Next step will be changing the place of fishes based on some calculation) Each fish is represented by bitmap (png file 7x12 px with picture of fish).

我创建了Form,其上的PictureBox是我的绘图画布.尺寸为640x480像素.

I created Form on which is PictureBox which is my drawing canvas. It has size 640x480 px.

在下面的代码中,我使用了简化的代码(我切断了所有不必要的东西).我的问题是变换矩阵,目前只有旋转.

In code below is simplified code I use (I cutted off all not necesary stuff). My problem is with tranformation matrix, at the moment only with rotation.

问题位于方法Draw() Fish 类中,在此我尝试进行转换每条鱼旋转30度,但稍后每条鱼将具有不同的起始旋转角度.我想用一条围绕鱼中心旋转角度将鱼旋转到哪里的方式进行转换.因此,在这种情况下,所有鱼都应成一条线,并按其旋转角度(此处为30度)旋转.
但是它们被放置在对角线上,所以转换以某种方式被弄乱了. 我怎样才能解决这个问题? 我可能不正确地使用了变形.

Problem is in class Fish in method Draw() where I'm trying to make a tranformation, at the moment I set the rotation 30 degrees for each fish but later each fish will have different start rotation angle. I want to make a transformation with the where will the fish will be rotated by the rotation angle around its centre. So in this case all fishes should be in one line and rotated each by its rotation angle (here 30 degrees).
But they are placed on diagonale, so the transformation is messed up somehow. How can I fix this? I'm probably using tansformation incorectly.

在类中使用的命名空间

using System.Drawing;//Graphics, Point
using System.Drawing.Drawing2D;//Matrix

    class Fish {
      public Point position;
      public int rotation;
      public Graphics g;
      public Image fishImage;
      private Rectangle rect;
      private Matrix matrix;

      public Fish(ref Graphics g, int x, int y, int rotation,Image img){
        this.g =  g;
        position = new Point(x,y);
        this.rotation = rotation;
        this.fishImage = img;
        this.rect = new Rectangle(position.X,position.Y, fishImage.Width, fishImage.Height);

      }

   public void Draw() {
    matrix = new Matrix();
    matrix.Rotate((float)rotation, Matrix.Append); //if i comment this it
    //will be drawn in one line
    //according to the initial values for position
    //if i let the rotation here it will be on diagonale
    //i want it on one line but rotated
    g.Transform = matrix;

    rect = new Rectangle(position.X, position.Y, fishImage.Width, fishImage.Height);
    g.DrawImage(fishImage, rect);
   }
}//end Fish class

表格

public partial class Form1 : Form
{
 private Bitmap canvasBitmap; //bitmap for drawing
 private Graphics g;          
 Image fishImage;

 private List<Fish> fishes = new List<Fish>();
  public Form1() {
    InitializeComponent();
   //png image 7x12 pixels 
   fishImage = FishGenetic.Properties.Resources.fishImage; 
   //on Form there is placed PictureBox called canvas
   //so canvas is PictureBox 640x480 px
   canvasBitmap = new Bitmap(canvas.Width, canvas.Height);
   canvas.Image = canvasBitmap;

   //prepare graphics
   g = Graphics.FromImage(canvasBitmap);
   g.SmoothingMode = SmoothingMode.AntiAlias;

   InitFishes();
   DrawFishes();
   canvas.Invalidate(); //invalidate the canvas 

  }//end Form1 constructor     


 private void InitFishes() {

   Fish fish1 = new Fish(ref g, 10, 10, 30, fishImage);
   Fish fish2 = new Fish(ref g, 20, 10, 30, fishImage);
   Fish fish3 = new Fish(ref g, 30, 10, 30, fishImage);
   Fish fish4 = new Fish(ref g, 40, 10, 30, fishImage);
   Fish fish5 = new Fish(ref g, 50, 10, 30, fishImage);
   fishes.Add(fish1);
   fishes.Add(fish2);
   fishes.Add(fish3);
   fishes.Add(fish4);
   fishes.Add(fish5);
 }

private void DrawFishes() {
   foreach(Fish fish in fishes) {
       fish.Draw();
   }
}

}//end Form1 class

主班

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }

推荐答案

您应该使用RotateAt而不是Rotate. Rotate方法将鱼绕到控件的左上角(原点),而RotateAt则绕您指定的点旋转某些东西.

You should use RotateAt instead of Rotate. Rotate method rotates the fish around the upper left corner of the control (origin) while RotateAt rotates something around a point specified by you.

只需计算每条鱼的中心(X =左+ FishWidth/2,Y =顶部+ FishHeight/2)并围绕该点旋转即可.

Just calculate the center of each fish (X=Left+FishWidth/2, Y=Top+FishHeight/2) and rotate around that point.

这篇关于C#旋转(转换)问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆