写一个 4X4 以内的不同的旋转角度的矩阵的程序
时间: 2024-10-02 11:03:46 浏览: 26
当然可以,这里是一个简单的Python程序,用于生成一个4x4矩阵的不同旋转角度(90度、180度和270度),假设我们有一个原始矩阵`matrix`:
```python
def rotate_matrix(matrix):
# 90度顺时针旋转
def rotate_90():
return [list(row)[::-1] for row in matrix]
# 180度旋转
def rotate_180():
return [[row[i] for row in reversed(matrix)] for i in range(len(matrix[0]))]
# 270度顺时针旋转
def rotate_270():
return [[matrix[j][i] for j in range(len(matrix)-1, -1, -1)] for i in range(len(matrix))]
# 显示原始矩阵和旋转后的矩阵
print("Original Matrix:")
for row in matrix:
print(row)
print("\nRotation Options:")
print(f"1. 90 degrees clockwise")
print(f"2. 180 degrees")
print(f"3. 270 degrees clockwise")
choice = int(input("Enter your choice (1, 2, or 3): "))
if choice == 1:
rotated = rotate_90()
elif choice == 2:
rotated = rotate_180()
elif choice == 3:
rotated = rotate_270()
else:
print("Invalid choice. Returning the original matrix.")
rotated = matrix
print("\nRotated Matrix:")
for row in rotated:
print(row)
# 示例4x4矩阵
matrix = [
[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]
]
rotate_matrix(matrix)
```
运行这个程序,你可以输入选择的旋转选项,它会按照所选的角度显示旋转后的矩阵。
阅读全文