02
11
07
15
简单的图形学变换
2.1k
等22人参与

在这几小节,我们看到了如何利用矩阵和矩阵乘法,实现图形变换。这些是二维图形学的基本原理。

在这里,如果你对进一步编程感兴趣,我强烈建议大家基于这几小节所学习的内容,建立一个属于自己的小型图形变换库。

你的代码只要支持可以渲染“点”就可以。你可以使用若干个点,在屏幕上绘制出一个圆或者是一个长方形。之后,对每一个点都使用同样的变换矩阵变换,就可以看到渲染的点组成的整体图形(圆或者长方形)发生了变换。

进一步,你可以将这些变换封装成方法,形成属于自己的小型图形变换库。

为了完成这个编程,大家需要简单查询一下,如何使用 Python 绘制一个点。有很多 Python 的库可以做到这一点,比如 cairo。

相信这是一个很酷的练习。大家可以在这里分享你的代码或者你实现的效果(github 链接也可以。)

大家加油!:)


我的作业
去发布

登录后即可发布作业,立即

全部作业
import matplotlib.pyplot as plt
import numpy as np

def plot_quadrilateral(matrix):
    # Ensure the matrix has two rows and four columns
    if len(matrix) != 2 or len(matrix[0]) != 4:
        raise ValueError("The input matrix must have two rows and four columns.")

    # Get the coordinates of the vertices
    x_coords = matrix[0]
    y_coords = matrix[1]

    # Plot the points
    plt.scatter(x_coords, y_coords)

    # Connect the points to form the quadrilateral
    plt.plot([x_coords[0], x_coords[1]], [y_coords[0], y_coords[1]])
    plt.plot([x_coords[1], x_coords[2]], [y_coords[1], y_coords[2]])
    plt.plot([x_coords[2], x_coords[3]], [y_coords[2], y_coords[3]])
    plt.plot([x_coords[3], x_coords[0]], [y_coords[3], y_coords[0]])

    # Set the chart title and axis labels
    plt.title('Quadrilateral Plot')
    plt.xlabel('X axis')
    plt.ylabel('Y axis')

    # Ensure equal aspect ratio
    plt.axis('equal')

    plt.savefig('myplot.png')

# 示例矩阵
matrix = np.array([
    [1, 3, 3,1],  # x 坐标
    [2, 2, 4, 4]   # y 坐标
])

# 调用函数
plot_quadrilateral(matrix)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
代码块
复制 预览
复制成功!
代码块
复制 预览
复制成功!
0
评论
提交于  2024-08-26 08:10:15

登录后即可查看更多作业,立即

微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号