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


Python Vector.minus方法代码示例

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


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

示例1: apply

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import minus [as 别名]
 def apply(boids, bj):
     c = Vector()
     for b in boids:
         if b != bj:
             distance = Vector.distance(b.position, bj.position)
             if distance < 5:
                 delta = Vector.s_minus(b.position, bj.position)
                 c.minus(delta)
     return c
开发者ID:lorenzo85,项目名称:python-playground,代码行数:11,代码来源:boidsrules.py

示例2: test_minus

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import minus [as 别名]
    def test_minus(self):
        # Given
        vector = Vector(5, 10)
        vector_to_subtract = Vector(1, 2)

        expected_x = vector.x - vector_to_subtract.x
        expected_y = vector.y - vector_to_subtract.y

        # When
        vector.minus(vector_to_subtract)

        # Then
        self.assertEquals(vector.x, expected_x)
        self.assertEquals(vector.y, expected_y)
开发者ID:lorenzo85,项目名称:python-playground,代码行数:16,代码来源:vector_tests.py

示例3: Hyperplane

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

#.........这里部分代码省略.........
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Hyperplane.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e

    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ''

            if coefficient < 0:
                output += '-'
            if coefficient > 0 and not is_initial_term:
                output += '+'

            if not is_initial_term:
                output += ' '

            if abs(coefficient) != 1:
                output += '{}'.format(abs(coefficient))

            return output

        n = self.normal_vector

        try:
            initial_index = Hyperplane.first_nonzero_index(n)
            terms = [write_coefficient(
                     n[i], is_initial_term=(i == initial_index)) +
                     'x_{}'.format(i + 1)
                     for i in range(self.dimension)
                     if round(n[i], num_decimal_places) != 0]
            output = ' '.join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = '0'
            else:
                raise e

        constant = round(self.constant_term, num_decimal_places)
        if constant % 1 == 0:
            constant = int(constant)
        output += ' = {}'.format(constant)

        return output

    def is_parallel(self, plane2):
        return self.normal_vector.is_parallel(plane2.normal_vector)

    def __eq__(self, plane2):
        if self.normal_vector.is_zero():
            if not plane2.normal_vector.is_zero():
                return False

            diff = self.constant_term - plane2.constant_term
            return MyDecimal(diff).is_near_zero()

        elif plane2.normal_vector.is_zero():
            return False

        if not self.is_parallel(plane2):
            return False

        basepoint_difference = self.basepoint.minus(plane2.basepoint)
        return basepoint_difference.is_orthogonal(self.normal_vector)

    def __iter__(self):
        self.current = 0
        return self

    def next(self):
        if self.current >= len(self.normal_vector):
            raise StopIteration
        else:
            current_value = self.normal_vector[self.current]
            self.current += 1
            return current_value

    def __len__(self):
        return len(self.normal_vector)

    def __getitem__(self, i):
        return self.normal_vector[i]

    @staticmethod
    def first_nonzero_index(iterable):
        for k, item in enumerate(iterable):
            if not MyDecimal(item).is_near_zero():
                return k
        raise Exception(Hyperplane.NO_NONZERO_ELTS_FOUND_MSG)
开发者ID:gddickinson,项目名称:python_code,代码行数:104,代码来源:hyperplane.py

示例4: Line

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import minus [as 别名]
class Line(object):

    NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero elements found'
    THEY_ARE_PARALLEL_AND_DONT_INTERSECT_MSG = 'They are parallel and never intersect.'
    THEY_ARE_THE_SAME_LINE_MSG = 'Has infinite intersections because they are the same line.'

    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 2

        if not normal_vector:
            all_zeros = ['0']*self.dimension
            normal_vector = all_zeros
        self.normal_vector = Vector(normal_vector)

        if not constant_term:
            constant_term = Decimal('0')
        self.constant_term = Decimal(constant_term)

        self.set_basepoint()


    def set_basepoint(self):
        try:
            n = self.normal_vector.coordinates
            c = self.constant_term
            basepoint_coords = ['0']*self.dimension

            initial_index = Line.first_nonzero_index(n)
            initial_coefficient = n[initial_index]

            basepoint_coords[initial_index] = c/initial_coefficient
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Line.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e

    def is_parallel(self, l):
        return self.normal_vector.is_parallel(l.normal_vector)

    def __eq__(self, l):
        if(self.is_parallel(l)):
            direction_vector = self.basepoint.minus(l.basepoint)
            return direction_vector.is_orthogonal(self.normal_vector)
        return False
            
    def intersection(self, l):
        if(self.__eq__(l)):
            return self
        if(self.is_parallel(l)):
            return None
        A, B = self.normal_vector.coordinates
        C, D = l.normal_vector.coordinates
        k1 = self.constant_term
        k2 = l.constant_term
        x = (D*k1 - B*k2)/(A*D - B*C)
        y = (A*k2 - C*k1)/(A*D - B*C)
        return Vector([x,y])


    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ''

            if coefficient < 0:
                output += '-'
            if coefficient > 0 and not is_initial_term:
                output += '+'

            if not is_initial_term:
                output += ' '

            if abs(coefficient) != 1:
                output += '{}'.format(abs(coefficient))

            return output

        n = self.normal_vector.coordinates

        try:
            initial_index = Line.first_nonzero_index(n)
            terms = [write_coefficient(n[i], is_initial_term=(i==initial_index)) + 'x_{}'.format(i+1)
                     for i in range(self.dimension) if round(n[i], num_decimal_places) != 0]
            output = ' '.join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = '0'
            else:
                raise e

#.........这里部分代码省略.........
开发者ID:lem93,项目名称:Linear-Algebra-Basic-Functions,代码行数:103,代码来源:line.py

示例5: test_subtract

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import minus [as 别名]
    def test_subtract(self):
        v = Vector([7.119, 8.215])
        w = Vector([-8.223, 0.878])

        self.assertVecEqual(['15.3420000000000005258016244625', '7.33699999999999985522691758888'], v.minus(w))
开发者ID:calebpowell,项目名称:linear-algebra-py,代码行数:7,代码来源:test_vector.py

示例6: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import minus [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

示例7: Plane

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import minus [as 别名]
class Plane(object):

    NO_NONZERO_ELTS_FOUND_MSG = 'No nonzero elements found'

    def __init__(self, normal_vector=None, constant_term=None):
        self.dimension = 3

        if not normal_vector:
            all_zeros = ['0']*self.dimension
            normal_vector = all_zeros
        self.normal_vector = Vector(normal_vector)

        if not constant_term:
            constant_term = Decimal('0')
        self.constant_term = Decimal(constant_term)

        self.set_basepoint()


    def set_basepoint(self):
        try:
            n = self.normal_vector.coordinates
            c = self.constant_term
            basepoint_coords = ['0']*self.dimension

            initial_index = Plane.first_nonzero_index(n)
            initial_coefficient = n[initial_index]

            basepoint_coords[initial_index] = c/initial_coefficient
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Plane.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e

    def is_parallel(self, p):
        return self.normal_vector.is_parallel(p.normal_vector)

    def __eq__(self, p):
        if(self.is_parallel(p)):
            direction_vector = self.basepoint.minus(p.basepoint)
            return direction_vector.is_orthogonal(self.normal_vector)
        return False

    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ''

            if coefficient < 0:
                output += '-'
            if coefficient > 0 and not is_initial_term:
                output += '+'

            if not is_initial_term:
                output += ' '

            if abs(coefficient) != 1:
                output += '{}'.format(abs(coefficient))

            return output

        n = self.normal_vector.coordinates

        try:
            initial_index = Plane.first_nonzero_index(n)
            terms = [write_coefficient(n[i], is_initial_term=(i==initial_index)) + 'x_{}'.format(i+1)
                     for i in range(self.dimension) if round(n[i], num_decimal_places) != 0]
            output = ' '.join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = '0'
            else:
                raise e

        constant = round(self.constant_term, num_decimal_places)
        if constant % 1 == 0:
            constant = int(constant)
        output += ' = {}'.format(constant)

        return output


    @staticmethod
    def first_nonzero_index(iterable):
        for k, item in enumerate(iterable):
            if not MyDecimal(item).is_near_zero():
                return k
        raise Exception(Plane.NO_NONZERO_ELTS_FOUND_MSG)
开发者ID:lem93,项目名称:Linear-Algebra-Basic-Functions,代码行数:100,代码来源:plane.py

示例8: Line

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

#.........这里部分代码省略.........
            c = self.constant_term
            basepoint_coords = ['0']*self.dimension

            initial_index = Line.first_nonzero_index(n.coordinates)
            initial_coefficient = n.coordinates[initial_index]

            basepoint_coords[initial_index] = c/initial_coefficient
            self.basepoint = Vector(basepoint_coords)

        except Exception as e:
            if str(e) == Line.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e


    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ''

            if coefficient < 0:
                output += '-'
            if coefficient > 0 and not is_initial_term:
                output += '+'

            if not is_initial_term:
                output += ' '

            if abs(coefficient) != 1:
                output += '{}'.format(abs(coefficient))

            return output

        n = self.normal_vector

        try:
            initial_index = Line.first_nonzero_index(n.coordinates)
            terms = [write_coefficient(n.coordinates[i], is_initial_term=(i==initial_index)) + 'x_{}'.format(i+1)
                     for i in range(self.dimension) if round(n.coordinates[i], num_decimal_places) != 0]
            output = ' '.join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = '0'
            else:
                raise e

        constant = round(self.constant_term, num_decimal_places)
        if constant % 1 == 0:
            constant = int(constant)
        output += ' = {}'.format(constant)

        return output


    @staticmethod
    def first_nonzero_index(iterable):
        for k, item in enumerate(iterable):
            if not MyDecimal(item).is_near_zero():
                return k
        raise Exception(Line.NO_NONZERO_ELTS_FOUND_MSG)

    def is_parallel_to(self, l):
        return self.normal_vector.is_parallel_to(l.normal_vector)

    def __eq__(self, l):
        if self.normal_vector.is_zero():
            if not l.normal_vector.is_zero():
                return False
            else:
                return MyDecimal(self.constant_term - l.constant_term).is_near_zero()
        elif l.normal_vector.is_zero():
            return False

        return self.is_parallel_to(l) and self.basepoint.minus(l.basepoint).is_orthogonal_to(self.normal_vector)

    def intersection_with(self, l):
        try:
            A, B = self.normal_vector.coordinates
            C, D = l.normal_vector.coordinates
            k1 = self.constant_term
            k2 = l.constant_term

            x_numerator = D * k1 - B * k2
            y_numerator = -C * k1 + A * k2
            denominator = Decimal('1') / (A * D - B * C)

            return Vector([x_numerator, y_numerator]).times_scalar(denominator)
        except:
            if self == l:
                return self
            else:
                return None
开发者ID:aleksandrpak,项目名称:solutions,代码行数:104,代码来源:line.py

示例9: __init__

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

    def __init__(self,p1,p2):
        #make copies of incoming vector points
        self.p1 = Vector(p1.x,p1.y)
        self.p2 = Vector(p2.x,p2.y)

#    def draw(self,window):
#        pygame.draw.line(window,pygame.color.Color("red"),(int(self.p1.x),int(self.p1.y)),(int(self.p2.x),int(self.p2.y)),2)
#
    def orthog(self):
        n = self.p2.minus(self.p1)
        n = n.normalize()
        return Vector(n.y,-n.x)


#Suppose the two line segments run
#from p to p + r
#and from q to q + s.
#Then any point on the first line is representable as
#p + t r (for a scalar parameter t)
#and any point on the second line as q + u s (for a scalar parameter u).
    def intersects(self,other):
        p = self.p1
        r = self.p2.minus(p)
        q = other.p1
        s = other.p2.minus(q)
        #solving for t = (q-p) x s / (r x s)
        qmp = q.minus(p)
        qmpxs = qmp.cross(s)
        rxs = r.cross(s)
        if rxs == 0: #lines are parallel
            return None
        t = qmpxs/rxs
        #solving for u = (q-p) x r / (r x s)
        qmpxr = qmp.cross(r)
        u = qmpxr/rxs
        
        #so now we can get the coordinates by normalizing r, scaling by t
    
        tscale = r.times(t)
        intersection1 = p.plus(tscale)
        sscale = s.times(u)
        intersection2 = q.plus(sscale)

        if (u <= 1.0) and (u >= 0) and (t <= 1.0) and (t >= 0):
            #print t, u
            rscale = r.times(t)
            intersection1 = p.plus(rscale)
            sscale = s.times(u)
            intersection2 = q.plus(sscale)
            #print rscale,sscale,intersection1,intersection2
        
            #assert intersection1.equals(intersection2)
            return rscale
        else:
            return None
        

   

    def distToPoint(self, point): # x3,y3 is the point
        px = self.p2.x-self.p1.x
        py = self.p2.y-self.p1.y

        something = px*px + py*py

        u =  ((point.x - self.p1.x) * px + (point.y - self.p1.y) * py) / float(something)

        if u > 1:
            u = 1
        elif u < 0:
            u = 0

        x = self.p1.x + u * px
        y = self.p1.y + u * py

        dx = x - point.x
        dy = y - point.y

        # Note: If the actual distance does not matter,
        # if you only want to compare what this function
        # returns to other results of this function, you
        # can just return the squared distance instead
        # (i.e. remove the sqrt) to gain a little performance

        dist = math.sqrt(dx*dx + dy*dy)

        return dist    
开发者ID:hychoi99,项目名称:nahchoina,代码行数:91,代码来源:linesegment.py

示例10: Line

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

#.........这里部分代码省略.........
        except Exception as e:
            if str(e) == Line.NO_NONZERO_ELTS_FOUND_MSG:
                self.basepoint = None
            else:
                raise e

    def __str__(self):

        num_decimal_places = 3

        def write_coefficient(coefficient, is_initial_term=False):
            coefficient = round(coefficient, num_decimal_places)
            if coefficient % 1 == 0:
                coefficient = int(coefficient)

            output = ""

            if coefficient < 0:
                output += "-"
            if coefficient > 0 and not is_initial_term:
                output += "+"

            if not is_initial_term:
                output += " "

            if abs(coefficient) != 1:
                output += "{}".format(abs(coefficient))

            return output

        n = self.normal_vector

        try:
            initial_index = Line.first_nonzero_index(n)
            terms = [
                write_coefficient(n[i], is_initial_term=(i == initial_index)) + "x_{}".format(i + 1)
                for i in range(self.dimension)
                if round(n[i], num_decimal_places) != 0
            ]
            output = " ".join(terms)

        except Exception as e:
            if str(e) == self.NO_NONZERO_ELTS_FOUND_MSG:
                output = "0"
            else:
                raise e

        constant = round(self.constant_term, num_decimal_places)
        if constant % 1 == 0:
            constant = int(constant)
        output += " = {}".format(constant)

        return output

    @staticmethod
    def first_nonzero_index(iterable):
        for k, item in enumerate(iterable):
            if not MyDecimal(item).is_near_zero():
                return k
        raise Exception(Line.NO_NONZERO_ELTS_FOUND_MSG)

    def is_parallel(self, line2):
        return self.normal_vector.is_parallel(line2.normal_vector)

    def __eq__(self, line2):
        if self.normal_vector.is_zero():
            if not line2.normal_vector.is_zero():
                return False

            diff = self.constant_term - line2.constant_term
            return MyDecimal(diff).is_near_zero()

        elif line2.normal_vector.is_zero():
            return False

        if not self.is_parallel(line2):
            return False

        basepoint_difference = self.basepoint.minus(line2.basepoint)
        return basepoint_difference.is_orthogonal(self.normal_vector)

    def intersection(self, line2):

        a, b = self.normal_vector.coordinates
        c, d = line2.normal_vector.coordinates
        k1 = self.constant_term
        k2 = line2.constant_term
        denom = (a * d) - (b * c)

        if MyDecimal(denom).is_near_zero():
            if self == line2:
                return self
            else:
                return None

        one_over_denom = Decimal("1") / ((a * d) - (b * c))
        x_num = d * k1 - b * k2
        y_num = -c * k1 + a * k2

        return Vector([x_num, y_num]).times_scalar(one_over_denom)
开发者ID:gddickinson,项目名称:python_code,代码行数:104,代码来源:line.py

示例11: Vector

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

# Addition
v1 = Vector([8.218, -9.341])
v2 = Vector([-1.129, 2.111])
print ''
print 'Addition'
print v1.plus(v2)

# Subtraction
v1 = Vector([7.119, 8.215])
v2 = Vector([-8.223, 0.878])
print ''
print 'Subtraction'
print v1.minus(v2)

# Scalar Multiplication
s = 7.41
v = Vector([1.671, -1.012, -0.318])
print ''
print 'Scalar Multiplication'
print v.times_scalar(s)

# Magnitude
print ''
print 'Magnitude'
v = Vector([-0.221, 7.437])
print format(v.magnitude(), '.5f')
v = Vector([8.813, -1.331, -6.247])
print format(v.magnitude(), '.5f')
开发者ID:ehafenmaier,项目名称:twd-udacity-la-refresher,代码行数:32,代码来源:lesson.py


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