本文整理汇总了Python中Vector.Vector.normalize方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.normalize方法的具体用法?Python Vector.normalize怎么用?Python Vector.normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector.Vector
的用法示例。
在下文中一共展示了Vector.normalize方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: magnitude_and_direction
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import normalize [as 别名]
def magnitude_and_direction():
v = Vector([-0.221, 7.437])
print("magnitude is {0}".format(v.magnitude()))
v = Vector([8.813, -1.331, -6.247])
print("magnitude is {0}".format(v.magnitude()))
v = Vector([5.581, -2.136])
print("normilized vector is {0}".format(v.normalize()))
v = Vector([1.996, 3.108, -4.554])
print("normilized vector is {0}".format(v.normalize()))
示例2: getReflectedNormal
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import normalize [as 别名]
def getReflectedNormal(self, ray, t):
# need to find the point, then find normal at point
#then get reflected ray, origin=pt
p = Point(*(ray.d.scale(t)).v )+ray.o
normal = Vector(*(p-self.origin).v)
normal.normalize()
rr = Ray()
rr.o=p
rr.d = ray.d - normal.scale(2*ray.d.dot(normal))
rr.d=Vector(*rr.d.v)
return rr, normal
示例3: __init__
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import normalize [as 别名]
class SpaceShip:
def __init__(self, coords):
self.coords = Vector(coords)
self.image = pygame.Surface((50, 40), pygame.SRCALPHA)
self.speed = Vector((10, 0))
self.boost = 1
self.direction = self.speed
self.state = NORMAL
self.draw()
def events(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.state = TURN_LEFT
if event.key == pygame.K_RIGHT:
self.state = TURN_RIGHT
if event.key == pygame.K_UP:
self.state = SPEED_UP
if event.key == pygame.K_DOWN:
self.state = SPEED_DOWN
if event.type == pygame.KEYUP:
self.state = NORMAL
def update(self, delta_time):
# Анализ состояния корабля
if self.state == TURN_LEFT:
self.speed.rotate(-ROTATION_ANGLE * (delta_time / 1000))
if self.speed.len != 0:
self.direction = self.speed
if self.state == TURN_RIGHT:
self.speed.rotate(ROTATION_ANGLE * (delta_time / 1000))
if self.speed.len != 0:
self.direction = self.speed
if self.state == SPEED_UP:
if self.speed.len != 0:
self.direction = self.speed
self.speed += self.speed.normalize() * self.boost
else:
self.speed = self.direction
if self.state == SPEED_DOWN:
if self.speed.len != 0:
self.direction = self.speed
self.speed -= self.speed.normalize() * self.boost
if self.speed.len < self.boost:
self.speed = Vector((0, 0))
# Пересечение экрана
if self.coords.x > RES_X:
self.coords.x = -50
if self.coords.y > RES_Y:
self.coords.y = -50
if self.coords.x < -50:
self.coords.x = RES_X
if self.coords.y < -50:
self.coords.y = RES_Y
self.coords += self.speed * (delta_time / 1000)
def draw(self):
pygame.draw.circle(self.image, (200, 0, 0), (20, 20), 20)
pygame.draw.circle(self.image, (200, 150, 0), (40, 20), 10)
# pygame.draw.rect(self.image, (0, 200, 0), self.image.get_rect(), 1)
def render(self, screen):
rotate_image = pygame.transform.rotate(self.image, self.direction.angle)
rect = rotate_image.get_rect(center=self.image.get_rect().center)
rect.move_ip(self.coords.as_point())
screen.blit(rotate_image, rect)
dv = Vector(self.image.get_rect().center)
pygame.draw.line(screen, (0, 255, 0), (self.coords + dv).as_point(), (self.coords + self.speed + dv).as_point())