1 /** 2 * Bezier Curve 3 */ 4 module d2d.math.BezierCurve; 5 6 import d2d.math.Vector; 7 8 /** 9 * A class that represents a Bezier Curve 10 * Supposedly the most visually appealing curves 11 * Needs a lot of complicated math; this class doesn't have much functionality other than what one might need to draw it 12 */ 13 class BezierCurve(T, uint dimensions) { 14 15 Vector!(T, dimensions)[] controlPoints; ///The points that control the path of the curve; usually don't actually exist on curve 16 17 /** 18 * Gets numPoints amount of points that are on the bezier curve evenly spaced from the beginning point to the end point (t 0 => 1) 19 */ 20 Vector!(T, dimensions)[numPoints] getPoints(uint numPoints)() { 21 Vector!(T, dimensions)[numPoints] containedPoints; 22 enum tStep = 1.0 / (numPoints + 1); 23 foreach (pointNumber; 0 .. numPoints) { 24 Vector!(double, dimensions)[] tempVals; 25 foreach (point; this.controlPoints) { 26 tempVals ~= cast(Vector!(double, dimensions)) point; 27 } 28 for (uint i = cast(uint) this.controlPoints.length - 1; i > 0; i--) { 29 foreach (j; 0 .. i) { 30 tempVals[j] += (tempVals[j + 1] - tempVals[j]) * (pointNumber * tStep); 31 } 32 } 33 containedPoints[pointNumber] = cast(Vector!(T, dimensions)) tempVals[0]; 34 } 35 return containedPoints; 36 } 37 38 /** 39 * Creates a bezier curve given a list of control points 40 */ 41 this(Vector!(T, dimensions)[] cPoints...) { 42 this.controlPoints = cPoints; 43 } 44 45 }