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


Python Vector.add方法代码示例

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


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

示例1: _circleCircle

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
def _circleCircle(sp):
	c1 = sp.shapes[0]
	c2 = sp.shapes[1]
	
	# Displacement between the circles
	dVector = c2.center.sub(c1.center)
	d = dVector.len()
	dAngle = dVector.angle()
	
	if d >= c2.radius + c1.radius:
		# Circles are too far apart; no intersects
		return []
	elif d <= abs(c2.radius - c1.radius):
		# One circle contains the other; no intersects
		return []
	else:
		# Aliases
		r1 = c1.radius
		r2 = c2.radius
		# Distance of intersect along displacement axis
		d1 = (d*d + r1*r1 - r2*r2) / (2*d)
		# Displacement of intersect perpendicularly from displacement axis
		y1 = math.sqrt(r1*r1 - d1*d1)
		
		# Intersections
		i1 = Vector(d1, -y1)
		i2 = Vector(d1, y1)
		i1 = i1.rotate(dAngle)
		i2 = i2.rotate(dAngle)
		i1 = i1.add(c1.center)
		i2 = i2.add(c1.center)
	
	return [i1, i2]
开发者ID:NicoAdams,项目名称:SphericalCow,代码行数:35,代码来源:intersections.py

示例2: PointMass

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class PointMass():
    def __init__(self, x, y, mass, fixed=False):
        self.pos = Vector(x, y)
        self.mass = mass

        self.colour = constants.BLACK

        self.velocity = Vector()
        self.force = Vector()
        self.angular_velocity = 0

        self.fixed = fixed
        self.selected = False

    def add_gravity(self):
        self.force.y += constants.GRAVITY * self.mass

    def add_force(self, force_vector):
        # print(force_vector)
        self.force = self.force.add(force_vector)

    def accelerate(self):
        self.velocity = self.velocity.add(self.force.scale(1 / self.mass))

    def move(self):
        self.pos = self.pos.add(self.velocity)

    def step(self):
        if not self.fixed:
            # Forces
            self.add_gravity()

            # Velocity
            self.accelerate()
            self.move()

            self.force.reset()

            m_pos = pygame.mouse.get_pos()
            pressed = pygame.mouse.get_pressed()
            if pressed[0]:
                if (m_pos[0] - self.pos.x)**2 + (m_pos[1] - self.pos.y)**2 < (self.mass*30)**2:
                    self.selected = True

            if not pressed[0]:
                self.selected = False

            if self.selected:
                self.add_force(Vector(m_pos[0], m_pos[1]).subtract(self.pos).normalize().scale(1))


    def draw(self, screen):
        m_pos = pygame.mouse.get_pos()
        if (m_pos[0] - self.pos.x)**2 + (m_pos[1] - self.pos.y)**2 < (self.mass*30)**2:
            pygame.draw.circle(screen, self.colour, (int(round(self.pos.x)), int(round(self.pos.y))), int(round(self.mass*10)))

        # Force and velocity vectors
        # Force
        # pygame.draw.line(screen, constants.RED, (round(self.pos.x), round(self.pos.y)), (self.pos.x + self.force.x, self.pos.y + self.force.y))
开发者ID:LaurenceGA,项目名称:programmingProjects,代码行数:61,代码来源:PointMass.py

示例3: pop

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
 def pop(self):
     if self.__queue.size() > 0:
         first_elemet = self.__queue.get(0)
         temp_vector = Vector()
         for i in range(0, self.__queue.size()):
             if i > 0:
                 temp_vector.add(self.__queue.get(i))
         self.__queue = temp_vector
         return first_elemet
     else:
         return None
开发者ID:presian,项目名称:HackBulgaria,代码行数:13,代码来源:my_queue.py

示例4: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Particle:
	def __init__(self, color, pos, vel=None, mass=1):
		self.color = color
		self.pos = pos
		self.prev = pos
		self.vel = (Vector(0,0) if vel is None else vel)
		self.force = Vector(0,0)
		self.mass = mass
		self.radius
	
	def __str__(self):
		return "P("+str(self.pos.x)+", "+str(self.pos.y)+")"
	
	def applyForce(self, force):
		self.force = self.force.add(force)
	
	def update(self, time):
		self.prev = self.pos.copy()
		
		accel = self.force.mul(1/self.mass)
		self.force = Vector(0,0)
		self.vel = self.vel.add(accel.mul(time))
		self.pos = self.pos.add(self.vel.mul(time))
		
		
开发者ID:NicoAdams,项目名称:Flockular-Pro,代码行数:25,代码来源:particle.py

示例5: MoveableActor

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class MoveableActor( Actor ):

    def __init__( self ):
        Actor.__init__( self )

        self.moveVector = Vector( 0, 0 )

    def move( self, vector ):
        self.moveVector = vector

    def moveLeft( self ):
        self.move( self.moveVector.add(
            Vector( -self.speed, 0 )
        ))

    def moveRight( self ):
        self.move( self.moveVector.add(
            Vector( self.speed, 0 )
        ))

    def moveUp( self ):
        self.move( self.moveVector.add(
            Vector( 0, -self.speed )
        ))

    def moveDown( self ):
        self.move( self.moveVector.add(
            Vector( 0, self.speed )
        ))

    def stopLeft( self ):
        self.move( self.moveVector.add(
            Vector( self.speed, 0 )
        ))

    def stopRight( self ):
        self.move( self.moveVector.add(
            Vector( -self.speed, 0 )
        ))

    def stopUp( self ):
        self.move( self.moveVector.add(
            Vector( 0, self.speed )
        ))

    def stopDown( self ):
        self.move( self.moveVector.add(
            Vector( 0, -self.speed )
        ))

    def update( self, frameTime, lifeTime ):
        self.vector.add( self.moveVector )
        
        Actor.update( self, frameTime, lifeTime )
开发者ID:lsjroberts,项目名称:7d7g,代码行数:56,代码来源:actor.py

示例6: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Queue:

    def __init__(self):
        self.__queue = Vector()

    # Adds value to the end of the Queue.
    # Complexity: O(1)
    def push(self, value):
        self.__queue.add(value)

    # Returns value from the front of the Queue and removes it.
    # Complexity: O(1)
    def pop(self):
        if self.__queue.size() > 0:
            first_elemet = self.__queue.get(0)
            temp_vector = Vector()
            for i in range(0, self.__queue.size()):
                if i > 0:
                    temp_vector.add(self.__queue.get(i))
            self.__queue = temp_vector
            return first_elemet
        else:
            return None

    # Returns value from the front of the Queue without removing it.
    # Complexity: O(1)
    def peak(self):

        if self.__queue.size != 0:
            return self.__queue.get(0)
        return None

    # Returns the number of elements in the Queue.
    # Complexity: O(1)
    def size(self):
        return self.__queue.size()

    def getQueue(self):
        return self.__queue.getVector()
开发者ID:presian,项目名称:HackBulgaria,代码行数:41,代码来源:my_queue.py

示例7: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Vehicle:
    def __init__(self,n,x,y):
        brain = Perceptron(n,0.001)
        self.location = Vector([x,y])
        self.velocity = Vector([0,0])
        self.acceleration = Vector([0,0])
        self.maxforce = 0.1
        self.maxspeed = 4
    
    def update():
        self.velocity.add(self.acceleration)
        self.velocity.limit(self.maxspeed)
        self.location.add(self.velocity)
        self.acceleration.mult(0)

    def applyForce(self, force):
        acceleration.add(force)
        
    #expects an array
    def steer(self, targets):
        if isinstance(targets,Vector):
            forces = 
        elif isinstance(targets,[]):
            targets = Vector(targets)
开发者ID:EricSchles,项目名称:neuralnet,代码行数:26,代码来源:second.py

示例8: Ball

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Ball(object):
    def __init__(self, position=None, velocity=None):
        self.position = Vector(60, 60) if position is None else position
        self.velocity = Vector(0, 0) if velocity is None else position
        self.acceleration = Vector(0.01, 0.01)

    def update(self):
        self.position.add(self.velocity)

    def update(self, canvas):
        width = int(canvas['width'])
        height = int(canvas['height'])
        if self.position.x > width:
            self.position.x = 0
        if self.position.y > height:
            self.position.y = 0

        self.velocity.add(self.acceleration)
        self.position.add(self.velocity)

    def render(self, canvas):
        canvas.create_circle(self.position.x, self.position.y, 7, fill='black')
开发者ID:tsh,项目名称:fun_with_code,代码行数:24,代码来源:acceleration.py

示例9: Vector

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
from vector import Vector


if __name__ == '__main__':
    myVector = Vector()
    myVector.add('numberOne')
    assert myVector.size() == 1
    assert myVector.capacity() == 20

    myVector = Vector()
    myVector.add('numberOne')
    myVector.add('numberTwo')
    assert myVector.size() == 2
    assert myVector.capacity() == 20

    myVector = Vector()
    myVector.add('numberOne')
    assert myVector.pop() == 'numberOne'
    assert myVector.size() == 0
    assert myVector.capacity() == 20

    myVector = Vector()
    myVector.add('numberOne')
    myVector.add('numberTwo')
    assert myVector.pop() == 'numberTwo'
    assert myVector.pop() == 'numberOne'
    assert myVector.size() == 0
    assert myVector.capacity() == 20

    myVector = Vector()
    myVector.insert(0, 'Hello')
开发者ID:nacholibre,项目名称:Algo-1,代码行数:33,代码来源:vector_test.py

示例10: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Ship:
    def __init__(self, x=50, y=50):
        self.pos = Vector(x, y);
        self.speed = Vector();
        self.accel = Vector();
        self.angle = 0;
        self.tmpAngle = 0;
        self.canShoot = 1;
        self.shootLimiter = Timer(2);
        self.keyb = 1;

        self.keys = {
            "up":0,
            "down":0,
            "left":0,
            "right":0,
            "shoot":0
            };
        self.mouse = Vector(0,0);
        self.mShoot = 0;
        
        self.accel.x = 1;
        self.points = (
            Vector(0,-10),
            Vector(0,10),
            Vector(30,0)
            );

    def update(self, shoot_function, index):
        if self.canShoot:
            self.shootLimiter.reset();
        else:
            self.canShoot = self.shootLimiter.update();
        if self.keyb:
            if self.keys["up"]:       self.accel.y = -0.5;
            elif self.keys["down"]:   self.accel.y = 0.5;
            else:
                self.accel.y = 0;
                self.speed.y *= 0.98;
            if self.keys["left"]:     self.accel.x = -0.5;
            elif self.keys["right"]:  self.accel.x = 0.5;
            else:
                self.accel.x = 0;
                self.speed.x *= 0.98;
            if self.keys["shoot"] and self.canShoot:
                self.canShoot = 0;
                self.shootLimiter.reset();
                shoot_function(index);
        else:
            if self.accel.hyp() < 1: self.accel = Vector(0,1);
            self.accel.rot_to(self.mouse.minus(self.pos).angle());
            if self.mshoot and self.canShoot:
                self.canShoot = 0;
                self.shootLimiter.reset();
                shoot_function(index);

        if not self.accel.isNull(): self.angle = self.accel.angle();
        self.speed.add(self.accel);
        if not self.speed.isNull():
            hyp = self.speed.hyp();
            if hyp > 10:
                self.speed.set_length(10);
            
        self.pos.add(self.speed);
        bounce = self.pos.moveWithinBounds(Vector(800, 600));
        if bounce:
            if bounce   == 1: self.speed.x = -self.speed.x;
            elif bounce == 2: self.speed.y = -self.speed.y;

    def hit(self):
        pass;

    def set_keys(self, up, down, left, right, shoot):
        self.keyb = 1;
        self.keys["up"] = up;
        self.keys["down"] = down;
        self.keys["left"] = left;
        self.keys["right"] = right;
        self.keys["shoot"] = shoot;

    def set_mouse(self, mpos, mshoot):
        self.keyb = 0;
        self.mouse.x = mpos[0];
        self.mouse.y = mpos[1];
        self.mshoot = mshoot;
    
    def net_data(self):
        # [0] = ( posX, posY )
        # [1] = ( speedX, speedY )
        # [2] = angle
        # ( ( posX, posY ), ( speedX, speedY ), angle )
        return (self.pos.get(1), self.speed.get(1), int(self.angle * 100));

    @staticmethod
    def collide(ships, shots, explosions=0):
        for i in ships:
            for shot in shots:
                if ships[i].pos.dist_to(shot.pos) < 10:
                    ships[i].hit();

#.........这里部分代码省略.........
开发者ID:strykejern,项目名称:strykejern-pythonGame,代码行数:103,代码来源:ship.py

示例11: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Player:

    STANDING=0
    WALKING=1
    JUMPING=2
    
    def __init__(self, **kwargs ):
        self.max_speed=kwargs.get('max_speed', 60.0)
        self.acceleration=kwargs.get('acceleration', 120.0)
        self.deceleration=kwargs.get('deceleration', 120.0)
        self.jump_velocity=kwargs.get('jump_velocity', -75.0)
        self.gravity=kwargs.get('gravity', 125.0)

        self.position=Vector(kwargs.get('x', 0.0),
                             kwargs.get('y', 0.0))

        self.position_z=kwargs.get('z', 0.0)

        self.bbox=Rect(self.position.x+kwargs.get('bbox_x',0.0),
                       self.position.y+kwargs.get('bbox_y',0.0),
                       kwargs.get('bbox_width',16.0),
                       kwargs.get('bbox_height',16.0))
        
        self.velocity=Vector(0.0,0.0)
        self.z_vector=0

        self.delta_x_vector=Vector(0.0, 0.0)
        self.delta_y_vector=Vector(0.0, 0.0)

        self.delta_position=Vector(0.0, 0.0)
        self.delta_velocity=Vector(0.0, 0.0)

        self.decelerate=Vector(1.0, 1.0, magnitude=self.deceleration)

        self.accelerate_left=Vector(-1.0, 0.0, magnitude=self.acceleration)
        self.accelerate_right=Vector(1.0, 0.0, magnitude=self.acceleration)
        self.accelerate_up=Vector(0.0, -1.0, magnitude=self.acceleration)
        self.accelerate_down=Vector(0.0, 1.0, magnitude=self.acceleration)

        self.accelerate_lower_left=Vector(-1.0, 1.0, magnitude=self.acceleration)
        self.accelerate_lower_right=Vector(1.0, 1.0, magnitude=self.acceleration)
        self.accelerate_upper_left=Vector(-1.0, -1.0, magnitude=self.acceleration)
        self.accelerate_upper_right=Vector(1.0, -1.0, magnitude=self.acceleration)

        self.x_accel_vector=Vector(0,0)
        self.y_accel_vector=Vector(0,0)

        self.image=Image(kwargs.get('sprite_sheet'), color_key=(0,0,0), flags=Image.FLIPPED_BOTH)

        self.animations=kwargs.get('animations')

        self.blit_flags=Image.NONE
        self.facing_right=True
        self.state='standing'

    def update(self, controls, dt):

        if controls.jump and self.position_z == 0:
            self.z_vector=self.jump_velocity
        elif self.position_z < 0:
            self.z_vector += self.gravity*dt
        else:
            self.z_vector=0
            self.position_z=0

        x_accel=self._get_x_accel_vector(controls)
        y_accel=self._get_y_accel_vector(controls)

        self.delta_velocity.set_magnitude(0.0)
        self.delta_velocity.add(x_accel)
        self.delta_velocity.add(y_accel)
        self.delta_velocity.multiply(dt)
        
        if ((0 < self.velocity.x and self.velocity.x + self.delta_velocity.x < 0) or
            (self.velocity.x < 0 and 0 < self.velocity.x + self.delta_velocity.x)):
            self.delta_velocity.x = -self.velocity.x

        if ((0 < self.velocity.y and self.velocity.y + self.delta_velocity.y < 0) or
            (self.velocity.y < 0 and 0 < self.velocity.y + self.delta_velocity.y)):
            self.delta_velocity.y = -self.velocity.y
            
        self.velocity.add(self.delta_velocity)
        self.velocity.limit_length(self.max_speed)

        self.delta_position.set_magnitude(0.0)
        self.delta_position.add(self.velocity)
        self.delta_position.multiply(dt)

        self.delta_x_vector.x=self.delta_position.x
        self.delta_y_vector.y=self.delta_position.y

        self.position_z += self.z_vector*dt

        if self.facing_right and controls.left:
            self.facing_right=False
            self.blit_flags=Image.FLIPPED_H
        elif not self.facing_right and controls.right:
            self.facing_right=True
            self.blit_flags=Image.NONE

#.........这里部分代码省略.........
开发者ID:aukeman,项目名称:pygame_thing,代码行数:103,代码来源:player.py

示例12: com

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
 def com(self):
     # The center of mass
     comVal = Vector(0, 0)
     for p in self.points:
         comVal = comVal.add(p)
     return comVal.mul(1.0 / len(self.points))
开发者ID:NicoAdams,项目名称:SphericalCow,代码行数:8,代码来源:shape.py

示例13: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
class Physics:

	DEFAULT_MAX_SPEED = 10
	DEFAULT_WEIGHT = 1.03
	DEFAULT_FLOOR_IT_SPEED = 0.9

	ORIGINAL_MAX_SPEED = None
	maxSpeed = None
	ORIGINAL_WEIGHT = None
	weight = None
	ORIGINAL_FLOOR_IT_SPEED = None
	floorItSpeed = None

	ORIGINAL_LOCATION = None
	location = None
	ORIGINAL_VELOCITY = None
	velocity = None
	ORIGINAL_ACCELERATION = None
	acceleration = None

	def __init__(self, x, y):
		self.ORIGINAL_LOCATION = Vector(x, y)
		self.location = Vector(x, y)
		self.ORIGINAL_VELOCITY = Vector(0, 0)
		self.velocity = Vector(0, 0)
		self.ORIGINAL_ACCELERATION = Vector(0, 0)
		self.acceleration = Vector(0, 0)

		self.ORIGINAL_MAX_SPEED = Vector(self.DEFAULT_MAX_SPEED, self.DEFAULT_MAX_SPEED)
		self.maxSpeed = Vector(self.DEFAULT_MAX_SPEED, self.DEFAULT_MAX_SPEED)
		self.ORIGINAL_WEIGHT = Vector(self.DEFAULT_WEIGHT, self.DEFAULT_WEIGHT)
		self.weight = Vector(self.DEFAULT_WEIGHT, self.DEFAULT_WEIGHT)
		self.ORIGINAL_FLOOR_IT_SPEED = Vector(self.DEFAULT_FLOOR_IT_SPEED, self.DEFAULT_FLOOR_IT_SPEED)
		self.floorItSpeed = Vector(self.DEFAULT_FLOOR_IT_SPEED, self.DEFAULT_FLOOR_IT_SPEED)

	def scale(self, horizontalScale, verticalScale):
		self.location.scaleWithRespectTo(self.ORIGINAL_LOCATION, horizontalScale, verticalScale)
		self.velocity.scaleWithRespectTo(self.ORIGINAL_VELOCITY, horizontalScale, verticalScale)
		self.acceleration.scaleWithRespectTo(self.ORIGINAL_ACCELERATION, horizontalScale, verticalScale)

		self.maxSpeed.scaleWithRespectTo(self.ORIGINAL_MAX_SPEED, horizontalScale, verticalScale)
		"""
		Scaling physics SEEMS to work correctly the minute we stop scaling the
		weight as well. I'm sure the concept of why this makes sense is super
		simple but it alludes me at the moment. No matter, it seems to work,
		so I'll leave it be for now and check back later when I have other
		things to check besides just the ship...
		"""
		self.weight.scaleWithRespectTo(self.ORIGINAL_WEIGHT, horizontalScale, verticalScale)
		self.floorItSpeed.scaleWithRespectTo(self.ORIGINAL_FLOOR_IT_SPEED, horizontalScale, verticalScale)

	def updateForNewLocation(self, newLocationBasedOnActions):
		direction = vector.subtract(newLocationBasedOnActions, self.location)
		direction.normalize()

		direction.multiply(self.floorItSpeed)
		self.acceleration = direction;

		self.velocity.add(self.acceleration)
		self.velocity.limit(self.maxSpeed);
		self.velocity.stopIfSlowEnough();
		self.velocity.divideByVector(vector=self.weight)
		self.location.add(self.velocity)
开发者ID:Barrowclift,项目名称:Game-Physics-Test,代码行数:65,代码来源:physics.py

示例14: int

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import add [as 别名]
# test if vector.py is callable externally and if it works well
import sys, os
if os.getcwd() not in sys.path:
        sys.path.append(os.getcwd())
from vector import Vector

# run-time loopy
instr = 1
while instr > 0:
    instr = int(input('(0)EXIT\n(1)INSERT\n(2)ADD\n(3)GET\n(4)REMOVE\n(5)POP\n(6)SIZE\n(7)CAPACITY\n'))
    if instr == 1:
        ind = int(input('Index?\n'))
        val = input('Value?\n')
        print (Vector.insert(ind, val))
    elif instr == 2:
        val = input ('Value?\n')
        print (Vector.add(val))
    elif instr == 3:
        ind = int(input('Index?\n'))
        print (Vector.get(ind))
    elif instr == 4:
        ind = int(input('Index?\n'))
        print (Vector.remove(ind))
    elif instr == 5:
        print (Vector.pop())
    elif instr == 6:
        print (Vector.size())
    elif instr == 7:
        print (Vector.capacity())
开发者ID:brada1,项目名称:Algo-1,代码行数:31,代码来源:vector_test.py


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