python 将二维数组旋转_在Python中旋转二维数组

这篇博客介绍如何在Python中实现二维数组的90度顺时针、180度和270度顺时针旋转。通过详细解释矩阵变换原理,展示了旋转坐标点的代码实现,并提供了在线测试环境。
摘要由CSDN通过智能技术生成

我自己也遇到过这个问题,而且我已经找到了关于这个主题的精彩维基百科页面("常见轮换"段落:

[https://en.wikipedia.org/wiki/Rotation_matrix#Ambiguities]

然后我编写了以下代码,超级详细,以便清楚地了解正在发生的事情。

我希望你能发现在你发布的非常漂亮和聪明的单行中挖掘更多内容是有用的。

要快速测试它,您可以在此处复制/粘贴它:

[http://www.codeskulptor.org/]

triangle = [[0,0],[5,0],[5,2]]

coordinates_a = triangle[0]

coordinates_b = triangle[1]

coordinates_c = triangle[2]

def rotate90ccw(coordinates):

print "Start coordinates:"

print coordinates

old_x = coordinates[0]

old_y = coordinates[1]

# Here we apply the matrix coming from Wikipedia

# for 90 ccw it looks like:

# 0,-1

# 1,0

# What does this mean?

#

# Basically this is how the calculation of the new_x and new_y is happening:

# new_x = (0)(old_x)+(-1)(old_y)

# new_y = (1)(old_x)+(0)(old_y)

#

# If you check the lonely numbers between parenthesis the Wikipedia matrix's numbers

# finally start making sense.

# All the rest is standard formula, the same behaviour will apply to other rotations, just

# remember to use the other rotation matrix values available on Wiki for 180ccw and 170ccw

new_x = -old_y

new_y = old_x

print "End coordinates:"

print [new_x, new_y]

def rotate180ccw(coordinates):

print "Start coordinates:"

print coordinates

old_x = coordinates[0]

old_y = coordinates[1]

new_x = -old_x

new_y = -old_y

print "End coordinates:"

print [new_x, new_y]

def rotate270ccw(coordinates):

print "Start coordinates:"

print coordinates

old_x = coordinates[0]

old_y = coordinates[1]

new_x = -old_x

new_y = -old_y

print "End coordinates:"

print [new_x, new_y]

print "Let's rotate point A 90 degrees ccw:"

rotate90ccw(coordinates_a)

print "Let's rotate point B 90 degrees ccw:"

rotate90ccw(coordinates_b)

print "Let's rotate point C 90 degrees ccw:"

rotate90ccw(coordinates_c)

print "=== === === === === === === === === "

print "Let's rotate point A 180 degrees ccw:"

rotate180ccw(coordinates_a)

print "Let's rotate point B 180 degrees ccw:"

rotate180ccw(coordinates_b)

print "Let's rotate point C 180 degrees ccw:"

rotate180ccw(coordinates_c)

print "=== === === === === === === === === "

print "Let's rotate point A 270 degrees ccw:"

rotate270ccw(coordinates_a)

print "Let's rotate point B 270 degrees ccw:"

rotate270ccw(coordinates_b)

print "Let's rotate point C 270 degrees ccw:"

rotate270ccw(coordinates_c)

print "=== === === === === === === === === "

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值