def newton_raphson_root_find(self, b_curve, p, u): """A method to compute roots of polynomials using the NewtonRaphson method""" # print("calling newton raphson") q_u = b_curve.get_value(u) q_1 = BezierCurve( control_points=[((b_curve.get_control_point(i + 1)[0] - b_curve.get_control_point(i)[0]) * 3.0, (b_curve.get_control_point(i + 1)[1] - b_curve.get_control_point(i)[1]) * 3.0) for i in range(3)]) q_2 = BezierCurve(control_points=[( (q_1.get_control_point(i + 1)[0] - q_1.get_control_point(i)[0]) * 2.0, (q_1.get_control_point(i + 1)[1] - q_1.get_control_point(i)[1]) * 2.0) for i in range(2)]) q_1u = q_1.get_value(u) q_2u = q_2.get_value(u) numerator = (q_u[0] - p[0]) * q_1u[0] + (q_u[1] - p[1]) * q_1u[1] denominator = q_1u[0] ** 2 + q_1u[1] ** 2 + (q_u[0] - p[0]) * q_2u[0] + \ (q_u[1] - p[1]) * q_2u[1] if denominator == 0.0: return u u_prime = u - (numerator / denominator) return u_prime
def __init__(self, size, amplitude_min, amplitude_max, amplitude_lerp_speed, x_squish, y_squish, bezier_visibility, invert_color, constant_increase, is_drawing_instant, frequency): self.size = size self.amplitude_min = amplitude_min self.amplitude_max = amplitude_max self.amplitude_lerp_speed = amplitude_lerp_speed self.x_squish = x_squish self.y_squish = y_squish self.bezier_visibility = bezier_visibility self.constant_increase = constant_increase self.is_drawing_instant = is_drawing_instant self.amplitude = self.amplitude_min self.frequency = frequency self.is_first_cycle = True self.points_exist = True self.xar = [] self.yar = [] self.move_speed = 100 self._frequency = 0.5 self.phase = 0.0 self.ax = None self.ani = None self.is_increasing = False if invert_color: self.line_color = 'white' self.bg_color = '#2f3136' else: self.line_color = '#2f3136' self.bg_color = 'white' # THE BEZIER CURVE SECTION # self.bezier_curve = BezierCurve.BezierCurve() self.points_file = self.bezier_curve.get_points_from_file( "data/points.txt") self.bezier_points = self.bezier_curve.get_bezier_points( self.points_file, 0.01) self.index = 0 self.initial_bezier_point = [] self.target_bezier_point = self.bezier_points[0] self.bezier_x = [] self.bezier_y = [] for points in self.bezier_points: self.bezier_x.append(points[0]) self.bezier_y.append(points[1])
def fit_cubic(self, first, last, t_hat1, t_hat2): """Fit a Bezier curve to a set of digitalized points given tHat1, tHat2""" n_pts = last - first + 1 if n_pts == 2: b_curve = [self._dpoints[0]] dist = np.linalg.norm(self._dpoints[1] - self._dpoints[0]) / 3.0 b_curve.append(b_curve[0] + dist * t_hat1) b_curve.append(self._dpoints[1] + dist * t_hat2) b_curve.append(self._dpoints[1]) c_points = [x.tolist() for x in b_curve] return [BezierCurve(control_points=c_points)] u = self.chord_length_parametrize(first, last) b_curve = self.generate_bezier_curve(first, last, u, t_hat1, t_hat2) max_error, split_point = self.compute_max_error( first, last, b_curve, u) # print("Max error %f" % max_error) if max_error < self._error: return [b_curve] iteration_error = self._error**2 if max_error < iteration_error: for i in range(self._reparam_max_iter): u_prime = self.reparametrize(first, last, u, b_curve) b_curve = self.generate_bezier_curve(first, last, u_prime, t_hat1, t_hat2) max_error, split_point = self.compute_max_error( first, last, b_curve, u_prime) if max_error < self._error: return [b_curve] u = u_prime # at this moment, fitting failed t_hat_center = self.compute_center_tangent(split_point) b1_curve = self.fit_cubic(first, split_point, t_hat1, t_hat_center) t_hat_center *= -1 b2_curve = self.fit_cubic(split_point, last, t_hat_center, t_hat2) return b1_curve + b2_curve
import BezierCurve import pygame pygame.init() # Points p = [{"x": 200, "y": 350}, {"x": 200, "y": 50}, {"x": 250, "y": 50}] p1 = [{"x": 250, "y": 50}, {"x": 300, "y": 100}, {"x": 300, "y": 150}] p2 = [{"x": 300, "y": 150}, {"x": 300, "y": 100}, {"x": 350 , "y": 50}] p3 = [{"x": 350, "y": 50}, {"x": 400, "y": 50}, {"x": 400, "y": 350}] BLUE = (0, 0, 255) screen = pygame.display.set_mode([900, 500]) c1 = BezierCurve.BezierCurve(p, BLUE, screen, pygame) c2 = BezierCurve.BezierCurve(p1, BLUE, screen, pygame) c3 = BezierCurve.BezierCurve(p2, BLUE, screen, pygame) c4 = BezierCurve.BezierCurve(p3, BLUE, screen, pygame) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False screen.fill((255, 255, 255)) c1.setup_bezier() c1.draw_curve()
def generate_bezier_curve(self, first, last, u_prime, t_hat1, t_hat2): """generate control points for bezier curve for region using least-squares method""" n_pts = last - first + 1 b_curve = BezierCurve() matrix_C, matrix_X = np.zeros((2, 2)), np.zeros(2) matrix_A = [] for i in range(n_pts): matrix_A.append([ b_curve.bezier_multiplier(u_prime[i], 1) * t_hat1, b_curve.bezier_multiplier(u_prime[i], 2) * t_hat2 ]) for i in range(n_pts): matrix_C[0, 0] += np.dot(matrix_A[i][0], matrix_A[i][0]) matrix_C[0, 1] += np.dot(matrix_A[i][0], matrix_A[i][1]) matrix_C[1, 0] = matrix_C[0, 1] matrix_C[1, 1] += np.dot(matrix_A[i][1], matrix_A[i][1]) tmp = self._dpoints[first + i] - \ (b_curve.bezier_multiplier(u_prime[i], 0) * self._dpoints[first] + (b_curve.bezier_multiplier(u_prime[i], 1) * self._dpoints[first] + ( b_curve.bezier_multiplier(u_prime[i], 2) * self._dpoints[last] + b_curve.bezier_multiplier(u_prime[i], 3) * self._dpoints[last] ))) matrix_X[0] += np.dot(matrix_A[i][0], tmp) matrix_X[1] += np.dot(matrix_A[i][1], tmp) det_C0_C1 = matrix_C[0, 0] * matrix_C[1, 1] - matrix_C[ 1, 0] * matrix_C[0, 1] det_C0_X = matrix_C[0, 0] * matrix_X[1] - matrix_C[1, 0] * matrix_X[0] det_X_C1 = matrix_X[0] * matrix_C[1, 1] - matrix_X[1] * matrix_C[0, 1] # compute values of alphas alpha_l = 0.0 if det_C0_C1 == 0 else det_X_C1 / det_C0_C1 alpha_r = 0.0 if det_C0_C1 == 0 else det_C0_X / det_C0_C1 seg_length = np.linalg.norm(self._dpoints[last] - self._dpoints[first]) epsilon = 1.0e-6 * seg_length if alpha_l < epsilon or alpha_r < epsilon: dist = seg_length / 3.0 b_curve = BezierCurve(control_points=[ self._dpoints[first].tolist(), (self._dpoints[first] + dist * t_hat1).tolist(), ( self._dpoints[last] + dist * t_hat2).tolist(), self._dpoints[last].tolist() ]) return b_curve b_curve = BezierCurve(control_points=[ self._dpoints[first].tolist(), (self._dpoints[first] + alpha_l * t_hat1).tolist(), (self._dpoints[last] + alpha_r * t_hat2).tolist(), self._dpoints[last].tolist() ]) return b_curve
import numpy as np import BezierCurve as bezcur points = np.array([[1, 2, 3, 1], [5, 1, 0, 5]]) ts = 0.01 * np.array(range(101)) bc = bezcur.BezierCurve(points) if __name__ == '__main__': import timeit print timeit.timeit('bc(ts)', setup='from __main__ import *', number=100000)
from MeshLib import * from BezierCurve import * from BsplineCurve import * import matplotlib.pyplot as plt if __name__ == "__main__": curve1 = BezierCurve([]) curve2 = BsplineCurve([]) LINEAR = 1 QUADRATIC = 2 CUBIC = 3 QUARTIC = 4 # Q1(a) """ uncomment each line to see different basis """ #curve1.plot_basis(no_of_variables = 1, degree = LINEAR, func = curve1.bezier_basis, labels=['t', 'N(t)'], title = 'Linear Basis Function over unit interval') #Q1(a) #curve1.plot_basis(no_of_variables = 1, degree = QUADRATIC, func = curve1.bezier_basis, labels=['t', 'N(t)'], title='Quadratic Basis Function over unit interval') #Q1(a) #curve1.plot_basis(no_of_variables = 2, degree = LINEAR, func = curve1.bezier_basis, labels=['t', 's', 'N(s,t)'], title='Linear Basis Function over unit square') # Q1(a) #curve1.plot_basis(no_of_variables = 2, degree = QUADRATIC, func = curve1.bezier_basis, labels=['t', 's', 'N(s,t)'], title='Quadratic Basis Function over unit square') # Q1(a) """ uncomment each line to see different basis """ #curve2.plot_basis(k = 2, labels = ['t', 'N_i,2(t)'], title = "Bspline Basis with linear basis") #Q1(b) #curve2.plot_basis(k = 3, labels = ['t', 'N_i,3(t)'], title = "Bspline Basis with Quadratic basis") #Q1(b) #curve2.plot_basis(k = 4, labels = ['t', 'N_i,4(t)'], title = "Bspline Basis with Qubic basis") #Q1(b) #curve2.plot_basis(k= 5, labels=['t', 'N_i,5(t)'], title="Bspline Basis with Quartic basis") # Q1(b) """ uncomment one set of points, and then uncomment plot curve function to see the curve. Uncomment, show points, to view control points. """
] # Total time curve takes to complete, in seconds max_t = 2 # Define a Point2D() to spawn particles at particle_spawn = Point.Point2D(360, 120) # Create a window and set the caption screen = pygame.display.set_mode((480, 240)) pygame.display.set_caption("VFXCollection Demo") # Create a clock to limit framerate and calculate delta-time clock = pygame.time.Clock() # Create an instance of the VFXCollection BezierCurve class bezier_curve = BezierCurve.BezierCurve(nodes, max_t) # Create an instance of the VFXCollection ParticleGenerator class particle_generator = ParticleGenerator.ParticleGenerator( particle_gravity=Point.Point2D(0, 50), particle_lifetime=5, generation_delay=0.02, _particle_generate=generate_particle, _particle_render=render_particle) # On startup, start everything so that people don't need to know what buttons to press in order to start anything # Start the curve bezier_curve.start( 0.001) # since dt isn't defined yet, just pass a tiny value in instead # Start the particle generator particle_generator.start()