当前位置: 首页>>代码示例>>Python>>正文


Python Vector.projection方法代码示例

本文整理汇总了Python中vector.Vector.projection方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.projection方法的具体用法?Python Vector.projection怎么用?Python Vector.projection使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在vector.Vector的用法示例。


在下文中一共展示了Vector.projection方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Node

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import projection [as 别名]
class Node(object):
    """
    A node is a mass circle which acts gravity, air resistance. It has a
    position and a velocity and can be connected by springs
    """

    def __init__(self, pos=(0,0), vel=(0,0), acc=(0,0)):
        """
        Creates a node with a start position, velocity and acceleration
        """
        self.pos = Vector(pos[0], pos[1])
        self.vel = Vector(vel[0], vel[1])
        self.acc = Vector(acc[0], acc[1])

    def refresh(self, atr=AIR_RESISTANCE, grav=GRAVITY, elast=ELASTICITY):
        """
        Refreshes node: changes position based on velocity and velocity based
        on acceleration. Also applies gravity and air resistance
        """
        self.pos = self.pos + self.vel
        self.vel = self.vel + (self.acc * elast)
        self.pos = self.pos + (self.acc * (1.0 - elast))
        self.acc = Vector(0,0)

        self.vel = self.vel + Vector(grav[0], grav[1])
        self.vel = self.vel * (1.0 - atr)

    def colideWall(self, limit, side, atr_normal=0.6, atr_surface=0.5, min_vel=0.99, radius=RADIUS):
        """
        Colides this node with a wall, if possible. Applies surface friction and changes velocity
        """
        global UP, DOWN, LEFT, RIGHT
        colision_strenght = 0

        if side == LEFT and self.pos.x < limit + radius:
            self.pos.x = limit + radius
            self.vel.x = self.vel.x * - atr_normal if abs(self.vel.x * - atr_normal) > min_vel else 0.0
            self.vel.y *= atr_surface
            colision_strenght = abs(self.vel.x)
        elif side == RIGHT and self.pos.x > limit - radius:
            self.pos.x = limit - radius
            self.vel.x = self.vel.x * - atr_normal if abs(self.vel.x * - atr_normal) > min_vel else 0.0
            self.vel.y *= atr_surface
            colision_strenght = abs(self.vel.x)

        if side == UP and self.pos.y < limit + radius:
            self.pos.y = limit + radius
            self.vel.x *= atr_surface
            self.vel.y = self.vel.y * - atr_normal if abs(self.vel.y * - atr_normal) > min_vel else 0.0
            colision_strenght = abs(self.vel.y)
        elif side == DOWN and self.pos.y > limit - radius:
            self.pos.y = limit - radius
            self.vel.x *= atr_surface
            self.vel.y = self.vel.y * - atr_normal if abs(self.vel.y * - atr_normal) > min_vel else 0.0
            colision_strenght = abs(self.vel.y)
        elif side not in [UP, LEFT, RIGHT, DOWN]:
            raise ValueError("side must be UP, LEFT, RIGHT or DOWN.")

        return colision_strenght

    def colide(self, other, radius=RADIUS):
        """
        Colides this node with another one, if possible
        """
        # Nao pode colidir consigo proprio
        if (self is other):
            return

        # calcula distancia entre os bots
        distancia = (self.pos - other.pos).length()

        # detecta colisao
        if distancia != 0 and distancia <= radius*2:
            colisao = (self.pos - other.pos)        # vetor colisao(ligando os dois)

            # projeta velocidades no vetor colisao
            trans_a = self.vel.projection(colisao)
            trans_b = other.vel.projection(colisao)

            # corrige posicoes
            r = (colisao.unit() * radius * 2) - colisao
            self.pos += r / 2.0
            other.pos -= r / 2.0

            # troca velocidades
            self.vel += trans_b - trans_a
            other.vel += trans_a - trans_b

    def __repr__(self):
        return "<Node pos=%dx%d, vel=%.2fx%.2f>" % (self.pos.x, self.pos.y, self.vel.x, self.vel.y)
开发者ID:rodrigosetti,项目名称:springbots,代码行数:92,代码来源:gear.py

示例2: Vector

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import projection [as 别名]
for first, second in vectors:
    print first.parallel(second)
print

print "Orthogonal"
for first, second in vectors:
    print first.orthogonal(second)
print

print "Projection"
print "  - parallel"
print Vector([0.825, 2.036]).projection(Vector([3.039, 1.879])).round(3)
print "  - orthogonal"
v = Vector([-9.88, -3.264, -8.159])
b = Vector([-2.155, -9.353, -9.473])
print (v - b.projection(v)).round(3)
print "  - full composition"
v = Vector([3.009, -6.172, 3.692, -2.51])
b = Vector([6.404, -9.144, 2.759, 8.718])
print b.projection(v).round(3)
print (v - b.projection(v)).round(3)
print

print "Cross Product"
print Vector([8.462, 7.893, -8.187]).cross(Vector([6.984, -5.975, 4.778])).round(3)
print

print "Area of Parallelogram"
print round(Vector([-8.987, -9.838, 5.031]).cross(Vector([-4.268, -1.861, -8.866])).magnitude(), 3)
print
开发者ID:SorenPeterson,项目名称:linearalgebra,代码行数:32,代码来源:vector_computations.py


注:本文中的vector.Vector.projection方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。