added function for computing bezier points
This commit is contained in:
parent
2645cdeff6
commit
70e7a429a7
|
@ -9,7 +9,7 @@ from .Input import *
|
|||
class Audio(GameChild):
|
||||
|
||||
UP, DOWN = .1, -.1
|
||||
BASE_VOLUME = .8
|
||||
BASE_VOLUME = 1.0
|
||||
|
||||
def __init__(self, game):
|
||||
GameChild.__init__(self, game)
|
||||
|
|
|
@ -99,6 +99,9 @@ class Vector(list):
|
|||
def vol(self):
|
||||
return self.x * self.y
|
||||
|
||||
def copy(self):
|
||||
return Vector(self.x, self.y)
|
||||
|
||||
|
||||
class EVector(Vector):
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ def get_step(start, end, speed):
|
|||
x0, y0 = start
|
||||
x1, y1 = end
|
||||
angle = atan2(x1 - x0, y1 - y0)
|
||||
return speed * sin(angle), speed * cos(angle)
|
||||
return Vector(speed * sin(angle), speed * cos(angle))
|
||||
|
||||
def get_step_relative(start, end, step):
|
||||
return get_step(start, end, get_distance(start, end) * step)
|
||||
|
@ -28,10 +28,10 @@ def get_segments(start, end, count):
|
|||
|
||||
def get_points_on_line(start, end, count):
|
||||
rel_step = get_step_relative(start, end, 1 / float(count - 1))
|
||||
points = [start]
|
||||
points = [Vector(start[0], start[1])]
|
||||
for ii in range(count - 2):
|
||||
points.append((points[-1][0] + rel_step[0], points[-1][1] + rel_step[1]))
|
||||
points.append(end)
|
||||
points.append(Vector(points[-1][0] + rel_step[0], points[-1][1] + rel_step[1]))
|
||||
points.append(Vector(end[0], end[1]))
|
||||
return points
|
||||
|
||||
def get_angle(start, end, transpose=False):
|
||||
|
@ -44,12 +44,12 @@ def get_endpoint(start, angle, magnitude, translate_angle=True):
|
|||
"""clockwise, 0 is up"""
|
||||
x0, y0 = start
|
||||
dx, dy = get_delta(angle, magnitude, translate_angle)
|
||||
return x0 + dx, y0 + dy
|
||||
return Vector(x0 + dx, y0 + dy)
|
||||
|
||||
def get_delta(angle, magnitude, translate_angle=True):
|
||||
if translate_angle:
|
||||
angle = radians(angle)
|
||||
return sin(angle) * magnitude, -cos(angle) * magnitude
|
||||
return Vector(sin(angle) * magnitude, -cos(angle) * magnitude)
|
||||
|
||||
def reflect_angle(angle, wall):
|
||||
return wall - angle
|
||||
|
@ -263,3 +263,45 @@ def get_random_hsla_color(hue_range=(0, 359), saturation_range=(0, 100),
|
|||
return get_hsla_color(
|
||||
randint(*hue_range), randint(*saturation_range), randint(*lightness_range),
|
||||
randint(*alpha_range))
|
||||
|
||||
# adapted from http://www.pygame.org/wiki/BezierCurve
|
||||
def compute_bezier_points(vertices, numPoints=60):
|
||||
points = []
|
||||
b0x = vertices[0][0]
|
||||
b0y = vertices[0][1]
|
||||
b1x = vertices[1][0]
|
||||
b1y = vertices[1][1]
|
||||
b2x = vertices[2][0]
|
||||
b2y = vertices[2][1]
|
||||
b3x = vertices[3][0]
|
||||
b3y = vertices[3][1]
|
||||
ax = -b0x + 3 * b1x + -3 * b2x + b3x
|
||||
ay = -b0y + 3 * b1y + -3 * b2y + b3y
|
||||
bx = 3 * b0x + -6 * b1x + 3 * b2x
|
||||
by = 3 * b0y + -6 * b1y + 3 * b2y
|
||||
cx = -3 * b0x + 3 * b1x
|
||||
cy = -3 * b0y + 3 * b1y
|
||||
dx = b0x
|
||||
dy = b0y
|
||||
numSteps = numPoints - 1
|
||||
h = 1.0 / numSteps
|
||||
# Compute forward differences from Bezier points and "h"
|
||||
pointX = dx
|
||||
pointY = dy
|
||||
firstFDX = ax * h ** 3 + bx * h ** 2 + cx * h
|
||||
firstFDY = ay * h ** 3 + by * h ** 2 + cy * h
|
||||
secondFDX = 6 * ax * h ** 3 + 2 * bx * h ** 2
|
||||
secondFDY = 6 * ay * h ** 3 + 2 * by * h ** 2
|
||||
thirdFDX = 6 * ax * h ** 3
|
||||
thirdFDY = 6 * ay * h ** 3
|
||||
# Compute points at each step
|
||||
points.append(Vector(pointX, pointY))
|
||||
for i in range(numSteps):
|
||||
pointX += firstFDX
|
||||
pointY += firstFDY
|
||||
firstFDX += secondFDX
|
||||
firstFDY += secondFDY
|
||||
secondFDX += thirdFDX
|
||||
secondFDY += thirdFDY
|
||||
points.append(Vector(pointX, pointY))
|
||||
return points
|
||||
|
|
Loading…
Reference in New Issue