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


Python Vector.scale方法代码示例

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


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

示例1: PointMass

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

示例2: __init__

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import scale [as 别名]
class Particle:
  def __init__(self, pos, mass):
    self.position = pos
    self.mass = mass
    self.velocity = Vector(0, 0)
  
  def forceFrom(self, p):
    distance = p.position.subtract(self.position)
    magnitude = self.mass * p.mass / distance.dot(distance)
    return distance.normalize().scale(magnitude)
  
  def applyForce(self, force, timeDelta):
    self.velocity.addInPlace(force.scale(timeDelta / abs(self.mass)))
    self.position.addInPlace(self.velocity.scale(timeDelta))
  
  def __str__(self):
    return ("{Particle mass: " + str(self.mass) + " velocity: " +
      str(self.velocity) + " position: " + str(self.position) + "}")
开发者ID:Axure,项目名称:Benchmarks,代码行数:20,代码来源:particle.py

示例3: cast_rays

# 需要导入模块: from vector import Vector [as 别名]
# 或者: from vector.Vector import scale [as 别名]
	def cast_rays(self, output):
		print(output.shape)
		
		v_forward = (self.look_at - self.position).normalize()
	
		magnitude_right = (self.look_at - self.position).magnitude() * math.tan(self.angle / 2)
		magnitude_up = magnitude_right * (float(self.height) / float(self.width))
	
		v_up = Vector(0.0, 0.0, 1.0).scale(magnitude_up)
		v_right = v_forward.cross(v_up).normalize().scale(magnitude_right);
	
		top_left = (self.look_at + v_up - v_right) - self.position
		
		sweep_down = v_up.scale(-2)
		sweep_right = v_right.scale(2)
		
		total_pixels = float(self.height * self.width);
		pixel_count = 0;
		
		for y in range(self.height):
			fraction_down = y / float(self.height - 1)
			for x in range(self.width):
				fraction_right = x / float(self.width - 1)
				ray = Ray(self.position, top_left + sweep_down.scale(fraction_down) + sweep_right.scale(fraction_right))
				color = self.trace_ray(ray, max_depth)
				output[y,x,0] = color.r
				output[y,x,1] = color.g
				output[y,x,2] = color.b
				output[y,x,3] = color.a
				
				if (pixel_count > 0 and pixel_count % 10000 == 0):
					self.progress_token.update(pixel_count / total_pixels)
				
				pixel_count += 1
			
		return output
开发者ID:sdao,项目名称:cloudraytracer,代码行数:38,代码来源:raytracer.py


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