2x2矩阵相乘模版

由于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 };
}
时间: 2024-08-24 04:13:42

2x2矩阵相乘模版的相关文章

【CUDA并行编程之四】矩阵相乘

前面介绍了基本的Cuda编程的相关知识,那么这一篇在此基础之上来看看GPU在处理数据计算上的高效能,我们拿矩阵相乘来作为例子. 1.CPU上执行矩阵相乘以及性能. 在CPU上进行矩阵相乘运算的代码: mat_mul.cc: <span style="font-family:Microsoft YaHei;font-size:18px;">//a[i]*b[i] + c[i] = d[i] #include<iostream> #include<vector

C语言 &#183; 矩阵相乘 &#183; 算法提高

算法提高 矩阵相乘 时间限制:1.0s   内存限制:256.0MB 问题描述 小明最近在为线性代数而头疼,线性代数确实很抽象(也很无聊),可惜他的老师正在讲这矩阵乘法这一段内容. 当然,小明上课打瞌睡也没问题,但线性代数的习题可是很可怕的. 小明希望你来帮他完成这个任务. 现在给你一个ai行aj列的矩阵和一个bi行bj列的矩阵, 要你求出他们相乘的积(当然也是矩阵). (输入数据保证aj=bi,不需要判断) 输入格式 输入文件共有ai+bi+2行,并且输入的所有数为整数(long long范围

矩阵相乘

有一个x*y的矩阵和一个y*z矩阵相乘,元素均为整数,求两个矩阵相乘得到的矩阵.这是一道华为OJ题,具体描述忘记了,大致内容如此.并且要求实现的函数参数为指针. 例如矩阵1为 int m1[1][3]={2,-6,3}; 矩阵2为 int m2[3][1]={4,-2,-4}; 乘积矩阵初始化为 int r[1][1]=0; 要求实现的函数为 void matrix_multiple(int *m1,int *m2,int *r,int x,int y,int z) 代码如下: void mat

稀疏矩阵的三元组顺序表存储及矩阵相乘算法小结

稀疏矩阵的三元组顺序表存储及矩阵相乘算法小结 巧若拙(欢迎转载,但请注明出处:http://blog.csdn.net/qiaoruozhuo) 一:稀疏矩阵的三元组顺序表数据结构 typedef int ElemType; typedef struct { intx, y;  //该非零元素的行下标和列下标 ElemTypee; //该非零元素的值 } Triple; typedef struct { Tripledata[MAXSIZE]; //非零元素三元组顺序表 intmu, nu, t

ObjC语法练习 冒泡排序、选择排序、矩阵相乘

用OC实现的冒泡排序.选择排序.矩阵相乘,纯粹是用来练习语法. 冒泡排序,程序如下: void bubbleSort() { //初始化数组 NSMutableArray *array1 = [[NSMutableArray alloc] initWithCapacity:8]; [array1 addObject:@"5"]; [array1 addObject:@"10"]; [array1 addObject:@"8"]; [array1

cublas 矩阵相乘API详解

#include "cuda_runtime.h"#include "device_launch_parameters.h" #include <stdio.h>#include <stdlib.h>#include "cublas_v2.h" void multiCPU(float *c, float *a, float *b, unsigned int aH, unsigned int aW, unsigned int

CUDA 矩阵相乘完整代码

#include "cuda_runtime.h"#include "device_launch_parameters.h" #include <stdio.h>#include <stdlib.h>#include <time.h>#include "cublas_v2.h" #define BLOCK_SIZE 16 cudaError_t multiCuda(float *c, float *a, flo

CUDA 矩阵相乘

#include "cuda_runtime.h"#include "device_launch_parameters.h" #include <stdio.h>#include <stdlib.h>#include "cublas_v2.h" #define BLOCK_SIZE 16 /***************/ 用cuBlas的内置函数API,cublasSgemm cudaError_t multiWithc

HDU 4920 Matrix multiplication(矩阵相乘)

各种TEL,233啊.没想到是处理掉0的情况就可以过啊.一直以为会有极端数据.没想到竟然是这样的啊..在网上看到了一个AC的神奇的代码,经典的矩阵乘法,只不过把最内层的枚举,移到外面就过了啊...有点不理解啊,复杂度不是一样的吗.. Matrix multiplication Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 640