# -*- coding: utf-8 -*- # import importlib # importlib.import_module("./vector") from vector import Vector class Bezier: def __init__(self,pts): if len(pts)==4: self.pts = [p.copy() for p in pts] if len(pts)==3: self.pts = [pts[0].copy(), 1/3*pts[0]+2/3*pts[1], 1/3*pts[2]*2/3*pts[1], pts[2].copy()] if len(pts)==2: self.pts = [pts[0].copy(), 2/3*pts[0]+1/3*pts[1], 1/3*pts[0]+2/3*pts[1], pts[1].copy()] # Punkt für Parameter t (in [0,1]) def x(self,t): return (1.0-t)**3*self.pts[0]+\ 3*(1.0-t)**2*t*self.pts[1]+\ 3*(1.0-t)*t**2*self.pts[2]+\ t**3*self.pts[3] # Geschwindigkeit (dx/dt) in t-Parametrierung def v(self,t): return -3*(1.0-t)**2*self.pts[0]+\ 3*(1.0-4*t+3*t**2)*self.pts[1]+\ 3*(2*t-3*t**2)*self.pts[2]+\ 3*t**2*self.pts[3] # Zeichnet den Spline # Achtung: Funktioniert nur, wenn diese Datei ausgeführt wird... def draw(self, steps=1000): move(self.pts[0][0], self.pts[0][1]); for i in range(1,steps+1): p = self.x(1.0*i/steps) lineTo((p[0],p[1])) if __name__== "__main__": from gpanel import * makeGPanel(Size(600,600)) window(0, 599, 0, 599) b = Bezier([Vector([100,500,0]), Vector([200,0,0]), Vector([300,300,0]), Vector([500,300,0])]) b.draw()