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


Python Vector.norm方法代码示例

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


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

示例1: execute

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import norm [as 别名]
    def execute(self):
        # Magic numbers
        rx_project_factor = 0.6 # How much of max acc to use when projecting reciever?
        min_dist_to_sideline = 5. # How close to sidelines will be target at minimum?
        min_dist_to_endzone = 5.
        # Check for pass
        # NOTE: Checks all friends, including Blockers?
        this = self.owner
        max_range2 = this.pitch.ball.max_range_of(this)**2

        recs = [ p for p in  this.team.players.values() if p.standing and (p.pos - this.pos).mag2() < max_range2 ]
        threats = [ Threat.rx_threat(rx) for rx in recs ]
        imin = np.argmin(threats)
        best_threat = threats[imin]
        rx = recs[imin]

        my_threat = Threat.bc_threat(this)
        
        #NOTE: Doesn't take into account extra risk from pass, will lead to lots of passing.
        if my_threat > best_threat:
            # Make a pass
            # Find 'optimal' heading for the rx, weighted average of def positions using
            # the threat as the weight.
            rx_threat, scores, defenders = Threat.rx_threat(rx,full=True)
            heading=Vector(0,0)
            for d, s in zip(defenders, scores):
                # Find 'optimal' heading for this defender
                if rx.y == d.y:
                    heading_now = Vector(0,1)
                else:
                    midpoint = (d.pos + rx.pos)/2.
                    m=-1/( (rx.y-d.y)/(rx.x-d.x))
                    b = midpoint.y - m*midpoint.x
                    # Find where PB meets ez
                    # Limit target by corners, otherwise target halfway between meeting and corner.
                    ymeet = m * rx.attack_end_zone_x + b
                    if ymeet < 0:
                        ytarget=0
                    elif ymeet > rx.pitch.ysize:
                        ytarget = rx.pitch.ysize
                    else:
                        if d.y > rx.y:
                            ytarget = ymeet/2.
                        else:
                            ytarget = (ymeet + rx.pitch.ysize)/2.
                    heading_now = (Vector(rx.attack_end_zone_x,ytarget) - rx.pos).norm()
                heading += heading_now * s
            heading=heading.norm()
            # Now work out how far along heading to project as target.
            # First find TOF to Rx current location
            ball = this.pitch.ball
            tof_to_current = ball.find_time_of_flight(this, ball.find_elv_to_hit_target(this,rx.pos))
            # Project the Rx by this much time
            rx_projected_vel = (rx.vel + heading * rx.top_acc * rx_project_factor * tof_to_current).truncate(rx.top_speed)
            rx_projected_pos = rx.pos + rx_projected_vel * tof_to_current
            # Sanity check this position
            rx_projected_pos.y = Utils.bracket\
                (min_dist_to_sideline,rx_projected_pos.y,this.pitch.ysize-min_dist_to_sideline)
            rx_projected_pos.x = Utils.bracket\
                (min_dist_to_endzone,rx_projected_pos.x,this.pitch.xsize-min_dist_to_endzone)            
            # Make the throw
            this.message_handler.add(MessageHandler.Message(ball,this,'throw_made',rx_projected_pos))
开发者ID:Bogdanovist,项目名称:smash2,代码行数:64,代码来源:Player.py

示例2: Player

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

#.........这里部分代码省略.........
        return (self.x - self.defend_end_zone_x)*self.team.direction

    @property
    def opposite_team(self):
        return self.team.opposite_team

    @property
    def direction(self):
        return self.team.direction    

    @property
    def has_ball(self):
        return self.pitch.ball.carrier == self

    @property
    def in_contact(self):
        return self in self.pitch.contacts.keys()

    @property
    def team_name(self):
        if self.team.direction > 0:
            return('home')
        else:
            return('away')

    @property
    def team_in_possession(self):
        return self.team.in_possession

    def x_from_defend_end_zone(self,x):
        if self.team.direction > 0:
            return x
        else:
            return self.pitch.xsize-x  

    def move(self):

        # NOTE: Ignores mass! (assumes m=1 I guess)
        # Don't move if we are prone
        if not self.standing:
            # Prone players might still be moving, but they rapidly deccelerate!
            # Magic numbers
            prone_acc = 20.
            prone_top_speed = 5.
            desired_acc = self.vel.norm() * prone_acc * self.pitch.dt * -1.
            if self.vel.mag() < desired_acc.mag():
                self.vel=Vector(0,0)
            else:
                self.vel += desired_acc
                self.vel = self.vel.truncate(prone_top_speed)
                self.pos += self.vel * self.pitch.dt
            return
        elif self.in_contact:
            # Push resolved seperately
            return
        acc = self.current_acc()
        # Check current puff reduced acc
        # NOTE HACK: Current states consider current vel in providing desired acc
        # We undo this so that they return the actual desired velocity
        state_vel = self.move_state.execute()
        desired_vel = state_vel + self.vel 
        drag_limited_acc = acc * (1. - desired_vel.angle_factor(self.vel) * self.vel.mag() / self.top_speed)
        desired_acc = (desired_vel - self.vel).truncate(drag_limited_acc)
        self.vel += desired_acc * self.pitch.dt
        self.vel = self.vel.truncate(self.top_speed)
        self.pos += self.vel * self.pitch.dt
        # It costs puff to move
        self.puff -= desired_acc.mag() * self.pitch.dt * self.pitch.puff_fac

    def push(self):
        if not self.in_contact: return
        # NOTE: Pushing forwards always, but we don't always want to do that. Need to have hook
        # into state and define in states the push directions ( a parrallel steering behaviours).
        
    def current_acc(self):
        " Current puff reduced max accel."
        cut_in=0.5
        ability_floor=0.5
        #NOTE: Turned off for now to allow easier state debugging
        return self.top_acc
        # Reduce ability linearily after cut in, down to floor
        ratio = self.puff/self.stamina
        if ratio < cut_in:
            fac = ratio*(1.-ability_floor)/cut_in + ability_floor
        else:
            fac = 1.
        return fac*self.top_acc

    def standup(self):
        """
        Check if player is prone and if so whether they can stand yet.
        """
        # Done via property now (but is it working?)
        #if self.prone > 0.:
        self.prone -= self.pitch.dt
        #    if self.prone <= 0.:
        #        self.standing=True
    
    def drop_ball(self):
        self.send(self.pitch.ball,'ball_loose')
开发者ID:Bogdanovist,项目名称:smash2,代码行数:104,代码来源:Player.py

示例3: testBasicRotationMatrices

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import norm [as 别名]
    def testBasicRotationMatrices(self):
        'Test the basic rotation matrices.'
        m = Matrix.rotationMatrixForZ(45)

        v = m.multv(Vector(1.0, 0.0, 0.0))
        assert v.round(6) == [ 0.707107, 0.707107, 0.000000 ]
        v = m.multv(m.multv(Vector(1.0, 0.0, 0.0)))
        assert v.round(6) == [ 0.0, 1.0, 0.0 ]

        i = 0
        v = Vector(1.0, 0.0, 0.0)
        while (i < 8):
            v = m.multv(v)
            i += 1

        assert v.norm() == 1.0
        assert v.round(5) == [1.0, 0.0, 0.0], v

        assert m.multv(Vector(0, 0, 1)) == [0, 0, 1]

        m = Matrix.rotationMatrixForX(120)
        
        v = m.multv(Vector(0.0, 1.0, 0.0))
        assert round(v.norm(), 6) == 1.0, v
        assert v.round(6) == [ 0.0, -0.5, 0.866025 ]

        v = m.multv(Vector(0.0, 1.0, 0.0))
        v = m.multv(v)
        v = m.multv(v)

        assert abs(v.norm() - 1.0) < 0.000000000000001, v.norm() 
        assert round(v.norm(), 6) == 1.0, v.norm()
        assert v.round(6) == [ 0.0, 1.0, 0.0 ]

        m = Matrix.rotationMatrixForY(60)
        
        v = m.multv(Vector(0.0, 0.0, 1.0))
        assert abs(v.norm()) == 1.0, v.norm()
        assert v.round(6) == [ 0.866025, 0.0, 0.5 ], v

        v = m.multv(Vector(0.0, 0.0, 1.0))
        v = m.multv(v)
        v = m.multv(v)
        v = m.multv(v)
        v = m.multv(v)
        v = m.multv(v)

        assert abs(v.norm() - 1.0) < 0.000000000000001, v.norm() 
        assert round(v.norm(), 6) == 1.0, v.norm()
        assert v.round(6) == [ 0.0, 0.0, 1.0 ]
开发者ID:pmbrennan,项目名称:math3d,代码行数:52,代码来源:Matrix.py

示例4: Cavallo

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import norm [as 别名]
class Cavallo():
    def __init__(self, pos = [130,0],vmax = 60., thePath=[]):
        self.currentNode = 0
        self.pathDir = 1
        self.nodes = thePath.getNodes()
        self.t = self.nodes[self.currentNode]
        self.max_v = vmax
        self.p = Vector(pos)
        self.v = Vector([0., 0.])
        self.max_see_ahead = 30
        self.max_avoid_force = 1.5
        self.obstacles = []

    def setObstacles(self, obs):
        self.obstacles = obs

    def updatePosition(self, dt):
        self.p = self.p + (self.v*dt)

    def collisionDetector(self):
        dynamic_length = self.v.mod()/self.max_v*self.max_see_ahead
        ahead = self.p + self.v.norm(dynamic_length)
        ahead2 = self.p + self.v.norm(dynamic_length*0.5)
        obstacle = self.takeOver(ahead, ahead2)
        avoidance = Vector()
 
        if (obstacle.p.coord() != [-1, -1]):
            avoidance = ahead-obstacle.p
            if (avoidance.x() == 0):
                avoidance = Vector([self.max_avoid_force, 0])
            elif (avoidance.y() == 0):
                avoidance = Vector([0, self.max_avoid_force])

        return avoidance

    def takeOver(self, ahead, ahead2):
        obstacle = Obstacle([-1, -1])
        min_distance = 9999.
        for o in self.obstacles:
            collision = self.lineIntersectsCircle(o, ahead, ahead2)
            if (o.p.coord() != self.p.coord()): # avoid self veto
                distance = o.p - self.p
                if (distance.dot(ahead) > 0.):
                    if (collision and distance.mod() < min_distance):
                        obstacle = o
                        min_distance = obstacle.p.distance(self.p)

        return obstacle
                
    def lineIntersectsCircle(self, o, ahead, ahead2):
        dist1 = o.p.distance(self.p)
        dist2 = o.p.distance(ahead2)
        return (o.p.distance(ahead) <= o.radius*1.5) or (dist2 <= o.radius*1.5) or (dist1<=(o.radius+15.))
 
    def steering(self):
        current_v = self.v.mod()
        desired_velocity = (self.t-self.p).norm(current_v)
        steering = (desired_velocity-self.v)
        avoid = self.collisionDetector()
        if (steering.mod() < avoid.mod()*2.5):
            steering = avoid
        else:
            steering = steering + avoid

        if (steering.mod() > 2.):
            steering = steering.norm(2.)

        # r = v*v/a_t
        #if (steering.transv(self.v) != 0):
            #r = self.v.mod()*self.v.mod()/steering.transv(self.v)
            #k = 0.5
            #print math.sqrt(k*r)
            
        self.v = self.v + steering
        self.v = self.v.norm(current_v)

    def move(self, dt):
        # FIXME SCEGLI STRATEGIA
        self.pathFollowing()
        self.speedUp()
        self.steering()
        self.updatePosition(dt)

    def speedUp(self):
        #FIXME assegna uno scatto
        current_v = self.v.mod()
        if (current_v == 0.):
            # deve cambiare con il tipo di mossa
            self.direzione = -0.7854
            self.v = Vector([math.cos(self.direzione), math.sin(self.direzione)])
        else:
            updated_v = current_v + 1.
            if (updated_v >= self.max_v):
                updated_v = self.max_v
            
            self.v = self.v.norm(updated_v)

    def pursuit(self):
        pass
        #public function pursuit(t :Boid) :Vector3D {
#.........这里部分代码省略.........
开发者ID:matteosan1,项目名称:python_code,代码行数:103,代码来源:steering2.py

示例5: Vector

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

u =Vector ( 3, [1,2,3])
v = Vector (3,3.5)
w = u.lincomb(v,10,1)
print(math.sqrt(u.inner(u)))
print(u.norm())

print(w)
开发者ID:RubenMeijs,项目名称:WISB256,代码行数:12,代码来源:test.py

示例6: Cavallo

# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import norm [as 别名]
class Cavallo():
    def __init__(self, currentNode, offset, thePath=[]):
        #FIXME Caratteristiche cavallo
        self.currentNode = currentNode+1
        self.pathDir = 1
        self.nodes = thePath.nodes
        self.t     = self.nodes[self.currentNode].waypoint
        self.max_v = self.nodes[self.currentNode].vmax
        #self.p = Vector(pos)
        self.p     = self.nodes[currentNode].waypoint+Vector(offset)
        self.v     = Vector([0., 0.])
        self.max_see_ahead = 30
        self.max_avoid_force = 1.5
        self.obstacles = []
        self.strategy = "mossa"

    def setObstacles(self, obs):
        self.obstacles = obs

    def updatePosition(self, dt):
        self.p = self.p + (self.v*dt)

    def collisionDetector(self):
        if (self.max_v != 0):
            dynamic_length = self.v.mod()/self.max_v*self.max_see_ahead
        else:
            return Vector()

        ahead = self.p + self.v.norm(dynamic_length)
        ahead2 = self.p + self.v.norm(dynamic_length*0.5)
        obstacle = self.takeOver(ahead, ahead2)
        avoidance = Vector()
 
        if (obstacle.p.coord() != [-1, -1]):
            avoidance = ahead-obstacle.p
            if (avoidance.x() == 0):
                avoidance = Vector([self.max_avoid_force, 0])
            elif (avoidance.y() == 0):
                avoidance = Vector([0, self.max_avoid_force])

        return avoidance

    def takeOver(self, ahead, ahead2):
        obstacle = Obstacle([-1, -1])
        min_distance = 9999.
        for o in self.obstacles:
            collision = self.lineIntersectsCircle(o, ahead, ahead2)
            if (o.p.coord() != self.p.coord()): # avoid self veto
                distance = o.p - self.p
                if (distance.dot(ahead) > 0.):
                    if (collision and distance.mod() < min_distance):
                        obstacle = o
                        min_distance = obstacle.p.distance(self.p)

        return obstacle
                
    def lineIntersectsCircle(self, o, ahead, ahead2):
        dist1 = o.p.distance(self.p)
        dist2 = o.p.distance(ahead2)
        return (o.p.distance(ahead) <= o.radius*2.) or (dist2 <= o.radius*2.) or (dist1<=(o.radius*2))
 
    def steering(self):
        current_v = self.v.mod()
        #print "POS", self.p.coord()
        #print "TAR", self.t.coord()
        
        desired_velocity = (self.t-self.p).norm(current_v)
        steering = (desired_velocity-self.v)
        avoid = self.collisionDetector()
        steering = steering + avoid

        if (steering.mod() > 2.5):
            steering = steering.norm(2.5)

        # r = v*v/a_t
        #if (steering.transv(self.v) != 0):
            #r = self.v.mod()*self.v.mod()/steering.transv(self.v)
            #k = 0.5
            #print math.sqrt(k*r)
            
        self.v = self.v + steering

    def thrust(self, slowDown=False):
        current_v = self.v.mod()
        #if (current_v == 0.):
        #    self.v = Vector([-1., -1.])
        if (current_v > self.max_v):
            if ((current_v - 0.5) <= 0):
                self.v = self.v.norm(0)
            else:
                self.v = self.v.norm(current_v - 0.5)
        elif (current_v < self.max_v):
            self.v = self.v.norm(current_v + 0.5)
        else:
            self.v = self.v.norm(current_v)

    def postoAlCanape(self, posto):
        self.nodes = copy.deepcopy(self.nodes)
        self.nodes[23].nextNode = 24
        self.nodes[-posto-3].vmax = 1
#.........这里部分代码省略.........
开发者ID:matteosan1,项目名称:python_code,代码行数:103,代码来源:Cavallo.py


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