为什么这个点积会产生尺寸错误?

问题描述 投票:0回答:1

此代码给出错误,指出旋转矩阵的重要尺寸和描述顶点的向量的重要尺寸不同。它声明旋转矩阵的维数为(2,2,1)。这是测试代码,我的完整程序更大,但包含另一个“ Asteroids”功能,该功能可以正常运行,但不能运行。有人可以帮忙吗?

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>
python numpy pygame
1个回答
0
投票

numpy.random.random_sample始终返回随机值数组。因此,numpy.random.random_sample不返回单个数字,它返回一个包含1个元素的数组。这导致numpy.random.random_sample(1)创建2x2x1数组而不是2x2数组。

如果要生成单个随机值,则必须获取rotation的第一个元素:

numpy.array
© www.soinside.com 2019 - 2024. All rights reserved.