本文整理汇总了Python中Vector.Vector.scale方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.scale方法的具体用法?Python Vector.scale怎么用?Python Vector.scale使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Vector.Vector
的用法示例。
在下文中一共展示了Vector.scale方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getReflectedNormal
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import scale [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
示例2: take_step
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import scale [as 别名]
def take_step(self, center, radius):
"""
Move the particle in to its position for next t
Handles collision with container wall
:param center:
:param radius:
:raise Exception:
"""
if not self.willcollide(center, radius):
self.position = self.step()
else:
print "Collision"
cx, cy = center
u = 0.0
px = self.position.vx
py = self.position.vy
vx = self.velocity.vx
vy = self.velocity.vy
# we find the following equation by treating (p+v*u-c)^2 as a vector, and dotting it with itself.
# we solve for u and insert our particle data
u1 = (-math.sqrt((-2 * cx * vx + 2 * px * vx + 2 * py * vy - 2 * vy * cy) ** 2 - 4 *
(vx ** 2 + vy ** 2) * (
cx ** 2 - 2 * cx * px + px ** 2 - radius ** 2 + py ** 2 - 2 * py * cy + cy ** 2))
+ 2 * cx * vx - 2 * px * vx - 2 * py * vy + 2 * vy * cy) / (2 * (vx ** 2 + vy ** 2))
u2 = (math.sqrt((-2 * cx * vx + 2 * px * vx + 2 * py * vy - 2 * vy * cy) ** 2 - 4 *
(vx ** 2 + vy ** 2) * (
cx ** 2 - 2 * cx * px + px ** 2 - radius ** 2 + py ** 2 - 2 * py * cy + cy ** 2))
+ 2 * cx * vx - 2 * px * vx - 2 * py * vy + 2 * vy * cy) / (2 * (vx ** 2 + vy ** 2))
u1 = abs(u1)
u2 = abs(u2)
# u is chosen
if 1 >= u1 > 0:
u = u1
elif 1 >= u2 > 0:
u = u2
else:
print u1
print u2
raise Exception("Could not determine distance to barrier. Particle has possibly slipped outside the container.")
# pc and projection are position Vectors
pc = self.position + Vector(self.velocity.vx * u, self.velocity.vy * u)
projection = Vector(
px=center,
py=(pc.vx, pc.vy)
).proj((self.position.vx, self.position.vy))
vp = Vector(
px=(self.position.vx, self.position.vy),
py=(projection.vx, projection.vy)
)
# p1'
p_1 = self.position + vp.scale(2 * vp.length)
# v2 is the vector from pc
v_2 = Vector(
px=(pc.vx, pc.vy),
py=(p_1.vx, p_1.vy)
).scale(self.velocity.length)
self.velocity = v_2
self.scale_for_temp()
self.position = pc + self.velocity.scale(self.velocity.length * (1 - u))
示例3: Particle
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import scale [as 别名]
class Particle(object):
position = None
velocity = None
mass = 10 ** -23
factor = 1.0
def __init__(self, positionx, positiony, velocityx, velocityy):
self.position = Vector(vx=positionx, vy=positiony)
self.velocity = Vector(vx=velocityx, vy=velocityy)
def step(self):
"""
Return the position the particle would have in the next t
:return:
"""
return Vector(
vx=self.position.vx + 1 * self.velocity.vx,
vy=self.position.vy + 1 * self.velocity.vy
)
def take_step(self, center, radius):
"""
Move the particle in to its position for next t
Handles collision with container wall
:param center:
:param radius:
:raise Exception:
"""
if not self.willcollide(center, radius):
self.position = self.step()
else:
print "Collision"
cx, cy = center
u = 0.0
px = self.position.vx
py = self.position.vy
vx = self.velocity.vx
vy = self.velocity.vy
# we find the following equation by treating (p+v*u-c)^2 as a vector, and dotting it with itself.
# we solve for u and insert our particle data
u1 = (-math.sqrt((-2 * cx * vx + 2 * px * vx + 2 * py * vy - 2 * vy * cy) ** 2 - 4 *
(vx ** 2 + vy ** 2) * (
cx ** 2 - 2 * cx * px + px ** 2 - radius ** 2 + py ** 2 - 2 * py * cy + cy ** 2))
+ 2 * cx * vx - 2 * px * vx - 2 * py * vy + 2 * vy * cy) / (2 * (vx ** 2 + vy ** 2))
u2 = (math.sqrt((-2 * cx * vx + 2 * px * vx + 2 * py * vy - 2 * vy * cy) ** 2 - 4 *
(vx ** 2 + vy ** 2) * (
cx ** 2 - 2 * cx * px + px ** 2 - radius ** 2 + py ** 2 - 2 * py * cy + cy ** 2))
+ 2 * cx * vx - 2 * px * vx - 2 * py * vy + 2 * vy * cy) / (2 * (vx ** 2 + vy ** 2))
u1 = abs(u1)
u2 = abs(u2)
# u is chosen
if 1 >= u1 > 0:
u = u1
elif 1 >= u2 > 0:
u = u2
else:
print u1
print u2
raise Exception("Could not determine distance to barrier. Particle has possibly slipped outside the container.")
# pc and projection are position Vectors
pc = self.position + Vector(self.velocity.vx * u, self.velocity.vy * u)
projection = Vector(
px=center,
py=(pc.vx, pc.vy)
).proj((self.position.vx, self.position.vy))
vp = Vector(
px=(self.position.vx, self.position.vy),
py=(projection.vx, projection.vy)
)
# p1'
p_1 = self.position + vp.scale(2 * vp.length)
# v2 is the vector from pc
v_2 = Vector(
px=(pc.vx, pc.vy),
py=(p_1.vx, p_1.vy)
).scale(self.velocity.length)
self.velocity = v_2
self.scale_for_temp()
self.position = pc + self.velocity.scale(self.velocity.length * (1 - u))
def willcollide(self, center, radius):
"""
Will the particle collide with the container if the next step is taken?
:param center:
:param radius:
:return:
#.........这里部分代码省略.........
示例4: __init__
# 需要导入模块: from Vector import Vector [as 别名]
# 或者: from Vector.Vector import scale [as 别名]
class Quaternion:
"""Representation of a quaternion, defined as:
s + ai + bj + ck
or
[s,v]
where s,a,b,c are scalars, v is a vector,
and i, j, k are defined such that:
i^2 = j^2 = k^2 = ijk = -1
ij = k, jk = i, ki = j
ji = -k, kj = -i, ik = -j
"""
def __init__(self, s, a, b, c):
self.mPrintSpec = '%f'
self.mScalar = s
self.mVector = Vector(a, b, c)
@staticmethod
def fromScalarVector(scalar, vector):
"""Define a quaternion from a scalar and a vector."""
# TODO: Refactor for performance.
return Quaternion(scalar, vector[0], vector[1], vector[2])
def clone(self):
v = self.mVector[:]
return Quaternion(self.mScalar, v[0], v[1], v[2])
def __str__(self):
return '[ %s, %s ]' % (self.mPrintSpec % self.mScalar, self.mVector)
def str2(self):
"""Alternate way to represent a Quaternion as a string."""
signs = [ ('+' if f >= 0 else '-') for f in self.mVector ]
vals = [ abs(f) for f in self.mVector ]
return '%s %s %si %s %sj %s %sk' % (self.mScalar,
signs[0],
vals[0],
signs[1],
vals[1],
signs[2],
vals[2])
def __eq__(self, q):
'Equality operator.'
return self.mScalar == q.mScalar and self.mVector == q.mVector
def __ne__(self, q):
'Not equals'
return not self.__eq__(q)
def compare(self, seq):
"""Compare the quaternion to a sequence assumed to be in
the form [ s, a, b, c ]."""
return (len(seq) == 4 and
self.mScalar == seq[0] and self.mVector[0] == seq[1] and
self.mVector[1] == seq[2] and self.mVector[2] == seq[3])
def __add__(self, q):
'Return self + q'
return Quaternion(self.mScalar + q.mScalar,
self.mVector[0] + q.mVector[0],
self.mVector[1] + q.mVector[1],
self.mVector[2] + q.mVector[2])
def __sub__(self, q):
'Return self - q'
return Quaternion(self.mScalar - q.mScalar,
self.mVector[0] - q.mVector[0],
self.mVector[1] - q.mVector[1],
self.mVector[2] - q.mVector[2])
def scale(self, s):
'Scale this quaternion by scalar s in-place.'
self.mScalar = self.mScalar * float(s)
self.mVector.scale(s)
def mults(self, s):
'Return self * scalar as a new Quaternion.'
r = Quaternion.fromScalarVector(self.mScalar, self.mVector)
r.scale(s)
return r
def mul1(self, q):
"""Multiplication Algorithm 1:
This is a very nice definition of the quaternion multiplication
operator, but it is terribly inefficient."""
s = self.mScalar * q.mScalar - self.mVector.dot(q.mVector)
v = q.mVector.mults(self.mScalar) + \
self.mVector.mults(q.mScalar) + \
self.mVector.cross(q.mVector)
return Quaternion.fromScalarVector(s, v)
def mul2(self, q):
"""Multiplication Algorithm 2: This is a much more efficient
implementation of quaternion multiplication. It isover 3x faster than
mul1."""
#.........这里部分代码省略.........