CC150 1.7 ROTATE MATRIX 旋转矩阵

1.7 Rotate Matrix
Given an image represented by an N*N matrix, where each pixel(像素) in the image is 4 bytes, write a method to rotate te image by 90 degrees. Can you do this in place?

思路1:
1 分层实施旋转;
2
2.1 移动上层到数组;
2.2 移动左层 到上层 ;
2.3 移动下层 到左层 ;
2.4 移动右层 到下层 ;
2.5 移动上层 到右层 ;
缺点:需要额外一层的空间;

改良思路2:
根据索引,逐个 元素替换,优点: in-place,只占用一个额外空间
伪代码:
for i=0 to n
     temp=top[i]
     top[i]=left[i]
     left[i]=bottom[i]
     bottom[i]=right[i]
     right[i]=temp[i]

从外层向内层实施;


C# 代码

public static void Rotate(int[,] matrix, int n)
        {
            for (int layer = 0; layer < n / 2; layer++)
            {
                int first = layer;
                int last = n - 1 - layer;
                for (int i = first; i < last; i++)
                {
                    int offset = i - first;
                    //save top
                    int top = matrix[first, i];

                    //left->top
                    matrix[first, i] = matrix[last - offset, first];

                    //bottom->left
                    matrix[last-offset,first] = matrix[last, last - offset];

                    //right->bottom
                    matrix[last, last - offset] = matrix[i, last];

                    //top->right
                    matrix[i, last] = top;
                }
            }


        }


import open3d as o3d import numpy as np # ------------------------------ # 步骤1: 生成直线点云(沿Y轴生成5个点) # ------------------------------ points = np.array([[0, i, 0] for i in range(5)], dtype=np.float32) pcd = o3d.geometry.PointCloud() pcd.points = o3d.utility.Vector3dVector(points) # ------------------------------ # 步骤2: 定义组合旋转矩阵(X→Z→Y顺序) # ------------------------------ def Rx(theta): """绕X轴旋转矩阵""" return np.array([ [1, 0, 0], [0, np.cos(theta), -np.sin(theta)], [0, np.sin(theta), np.cos(theta)] ]) def Ry(theta): """绕Y轴旋转矩阵""" return np.array([ [np.cos(theta), 0, np.sin(theta)], [0, 1, 0], [-np.sin(theta), 0, np.cos(theta)] ]) def Rz(theta): """绕Z轴旋转矩阵""" return np.array([ [np.cos(theta), -np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1] ]) # 定义旋转角度(单位:弧度) angle_x = np.deg2rad(-45) # X轴-45° angle_z = np.deg2rad(180) # Z轴180° angle_y = np.deg2rad(55) # Y轴55° # 计算组合旋转矩阵(顺序:X→Z→Y) rot_matrix = Ry(angle_y) @ Rz(angle_z) @ Rx(angle_x) # ------------------------------ # 步骤3: 创建XYZ轴箭头模板 # ------------------------------ def create_arrow(axis, color, length=0.3): """创建轴对齐箭头""" arrow = o3d.geometry.TriangleMesh.create_arrow( cylinder_radius=0.02, cone_radius=0.05, cylinder_height=length*0.7, # 圆柱 cone_height=length*0.3 #箭头 ) arrow.paint_uniform_color(color) # # 关键步骤:将箭头沿默认方向(Z轴负方向)平移,使尾部对齐原点 arrow.translate([0, 0, -length*0.7]) # 平移距离=圆柱高度 # 调整箭头初始方向(默认沿Z轴) if axis == 'X': arrow.rotate(o3d.geometry.get_rotation_matrix_from_xyz([0, np.pi/2, 0])) elif axis == 'Y': arrow.rotate(o3d.geometry.get_rotation_matrix_from_xyz([-np.pi/2, 0, 0])) return arrow arrow_x = create_arrow('X', [1, 0, 0]) # X轴红色 arrow_y = create_arrow('Y', [0, 1, 0]) # Y轴绿色 arrow_z = create_arrow('Z', [0, 0, 1]) # Z轴蓝色 # ------------------------------ # 步骤4: 应用位姿变换到每个点 # ------------------------------ combined_arrows = [] for point in points: # 复制箭头模板 ax = o3d.geometry.TriangleMesh(arrow_x) ay = o3d.geometry.TriangleMesh(arrow_y) az = o3d.geometry.TriangleMesh(arrow_z) # 应用组合旋转和平移 for arrow in [ax, ay, az]: arrow.rotate(rot_matrix, center=(0,0,0)) # 绕原点旋转 arrow.translate(point) # 平移到当前点 combined_arrows.extend([ax, ay, az]) # ------------------------------ # 步骤5: 可视化 # ------------------------------ o3d.visualization.draw_geometries([pcd] + combined_arrows)请问为什么xyz的箭头不是尾部相接,而是特别不美观的中间相接?
最新发布
03-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值