Python 为什么这个点积会产生尺寸误差?
该代码给出了一个错误,说明旋转矩阵的重要维度与描述顶点的向量的重要维度不同。它表示旋转矩阵具有维数(2,2,1)。这是测试代码,我的完整程序更大,但包含另一个“小行星”功能,工作正常,但这一个没有。有人能帮忙吗Python 为什么这个点积会产生尺寸误差?,python,numpy,pygame,Python,Numpy,Pygame,该代码给出了一个错误,说明旋转矩阵的重要维度与描述顶点的向量的重要维度不同。它表示旋转矩阵具有维数(2,2,1)。这是测试代码,我的完整程序更大,但包含另一个“小行星”功能,工作正常,但这一个没有。有人能帮忙吗 import pygame import pygame.locals import numpy import random asteroids = [] amount = 50 screen = (1600, 1200) pygame.init() display = pygame.
import pygame
import pygame.locals
import numpy
import random
asteroids = []
amount = 50
screen = (1600, 1200)
pygame.init()
display = pygame.display.set_mode(screen)
clock = pygame.time.Clock()
def rotation(angle):
rotation = numpy.array([[numpy.cos(angle), -numpy.sin(angle)], [numpy.sin(angle), numpy.cos(angle)]])
return rotation
def Asteroids(angle):
offset2 = numpy.array([[-10.0, 0.0], [-8.0, -5.0], [-5.0, -8.0], [0.0, -10.0], [6.0, -8.0], [9.0, -4.0], [4.0, -2.0], [10.0, 0.0], [7.0, 7.5], [2.5, 4.0], [0.0, 10.0], [-6.0, 8.0], [-9.0, 3.0], [-4.0, 0.0]])
asteroid = []
for n in range(len(offset2)):
offset2[n] = rotation(angle).dot(offset2[n])
asteroid.append(offset2[n])
return asteroid
def asteroid_creator(amount):
for i in range(amount):
i = Boid()
vec = numpy.random.random_sample(2) - numpy.array([0.5, 0.5])
i.position = numpy.random.random_sample(2) * screen
i.velocity = vec / numpy.linalg.norm(vec)
i.acceleration = numpy.array([0.0, 0.0])
i.angle = numpy.random.random_sample(1) * 2 * numpy.pi
asteroids.append(i)
def drawing_asteroid_creator(amount):
for n in range(amount):
#pygame.draw.circle(display, (255 - (n / amount * 255), 255 - (n / amount * 255), 255 - (n / amount * 255)), (int(asteroids[n].position[0]), int(asteroids[n].position[1])), 21)
#pygame.draw.circle(display, (n / amount * 255, n / amount * 255, n / amount * 255), (int(asteroids[n].position[0]), int(asteroids[n].position[1])), 19)
asteroid_angle = asteroids[n].angle
vertices = asteroids[n].position + numpy.array([(Asteroids(asteroid_angle)[0]), (Asteroids(asteroid_angle)[1]), (Asteroids(asteroid_angle)[2]), (Asteroids(asteroid_angle)[3]), (Asteroids(asteroid_angle)[4]), (Asteroids(asteroid_angle)[5]), (Asteroids(asteroid_angle)[6]), (Asteroids(asteroid_angle)[7]), (Asteroids(asteroid_angle)[8]), (Asteroids(asteroid_angle)[9]), (Asteroids(asteroid_angle)[10]), (Asteroids(asteroid_angle)[11]), (Asteroids(asteroid_angle)[12]), (Asteroids(asteroid_angle)[13])])
pygame.draw.polygon(display, (255, 255, 255), (vertices), 2)
asteroids[n].update()
class Boid:
def _init_(self):
self.position = position
self.velocity = velocity
self.acceleration = acceleration
self.angle = angle
def update(self):
self.position += self.velocity
self.velocity += self.acceleration
self.acceleration = 0
asteroid_creator(amount)
while True:
display.fill((0, 0, 0))
drawing_asteroid_creator(amount)
对不起,我忘记了错误信息
Traceback (most recent call last):
File "c:/Users/Marieke/Documents/Python stuff/Boid2.py", line 176, in <module>
drawing_asteroid_creator(amount)
File "c:/Users/Marieke/Documents/Python stuff/Boid2.py", line 135, in drawing_asteroid_creator
vertices = asteroids[n].position + numpy.array([(Asteroids(asteroid_angle)[0]), (Asteroids(asteroid_angle)[1]), (Asteroids(asteroid_angle)[2]), (Asteroids(asteroid_angle)[3]), (Asteroids(asteroid_angle)[4]), (Asteroids(asteroid_angle)[5]), (Asteroids(asteroid_angle)[6]), (Asteroids(asteroid_angle)[7]), (Asteroids(asteroid_angle)[8]), (Asteroids(asteroid_angle)[9]), (Asteroids(asteroid_angle)[10]),
(Asteroids(asteroid_angle)[11]), (Asteroids(asteroid_angle)[12]), (Asteroids(asteroid_angle)[13])])
File "c:/Users/Marieke/Documents/Python stuff/Boid2.py", line 48, in Asteroids
offset2[i] = rotation(angle).dot(offset2[i])
ValueError: shapes (2,2,1) and (2,) not aligned: 1 (dim 2) != 2 (dim 0)
PS C:\Users\Marieke\Documents\Python stuff>
回溯(最近一次呼叫最后一次):
文件“c:/Users/Marieke/Documents/Python stuff/Boid2.py”,第176行,在
绘图\u小行星\u创建者(金额)
文件“c:/Users/Marieke/Documents/Python stuff/Boid2.py”,第135行,在绘图\u asteroid\u creator中
顶点=小行星[n]。位置+numpy.数组([(小行星(小行星角)[0]),(小行星(小行星角)[1]),(小行星(小行星角)[2]),(小行星(小行星角)[3]),(小行星(小行星角)[4]),(小行星(小行星角)[5]),(小行星(小行星角)[6]),(小行星(小行星角)[7]),(小行星(小行星角)[8]),(小行星)(小行星_角)[9],(小行星(小行星_角)[10],
(小行星(小行星角)[11],(小行星(小行星角)[12],(小行星(小行星角)[13]))
文件“c:/Users/Marieke/Documents/Python stuff/Boid2.py”,第48行,在小行星中
偏移量2[i]=旋转(角度).dot(偏移量2[i])
值错误:形状(2,2,1)和(2,)未对齐:1(尺寸2)!=2(尺寸0)
PS C:\Users\Marieke\Documents\Python stuff>
始终返回一个随机值数组。因此
numpy.random.random\u sample(1)
不返回单个数字,而是返回一个包含1个元素的数组。这导致旋转
创建一个2x2x1数组,而不是2x2数组
若要生成单个随机值,必须获取由numpy.random.random\u sample生成的第一个元素:
i.angle=numpy.random.random_样本(1)[0]*2*numpy.pi
始终将完整的错误消息(从单词“Traceback”开始)作为文本(而不是屏幕截图)进行讨论(不是注释)。还有其他有用的信息。似乎您的角度是带有单个元素的列表,您需要旋转(角度[0])
顺便说一句:在\uuuuuuuuu\uuuuuuuu>的两侧应该是两个\uuuuuuuuuuu