码迷,mamicode.com
首页 > 其他好文 > 详细

2x2矩阵相乘模版

时间:2016-10-31 21:06:35      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:enable   alt   分享   ret   return   deb   exe   img   debug   

由于Unity只有4x4矩阵,今天要做一个2x2矩阵的旋转,居然忘了顺序。故写下作为模版记录。

 

顺序:

技术分享

 

 

下面是使用其进行旋转的C#代码:

public struct Position
{
    public int X;
    public int Y;

    public override string ToString() { return "X: " + X + " Y: " + Y; }
}

void OnEnable()//Execute
{
    var position = new Position() { X = 1, Y = 0 };
    var anchor = new Position() { X = 0, Y = 0 };

    Debug.Log("position: " + position);//print position: X: 1 Y: 0
    position = Rotation(90, position, anchor);
    Debug.Log("position rot: " + position);//print position rot: X: 0 Y: 1
}

Position Rotation(float rotValue, Position pos, Position anchor)
{
    var matrix00 = Mathf.Cos(rotValue * Mathf.Deg2Rad);
    var matrix01 = -Mathf.Sin(rotValue * Mathf.Deg2Rad);
    var matrix10 = Mathf.Sin(rotValue * Mathf.Deg2Rad);
    var matrix11 = Mathf.Cos(rotValue * Mathf.Deg2Rad);

    var x = (float)pos.X - anchor.X;
    var y = (float)pos.Y - anchor.Y;

    var rx = matrix00 * x + matrix01 * y;
    var ry = matrix10 * x + matrix11 * y;

    x = anchor.X + rx;
    y = anchor.Y + ry;

    var intX = Mathf.RoundToInt(x);
    var intY = Mathf.RoundToInt(y);

    return new Position() { X = intX, Y = intY };
}

 

2x2矩阵相乘模版

标签:enable   alt   分享   ret   return   deb   exe   img   debug   

原文地址:http://www.cnblogs.com/hont/p/6017172.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!