/* Copyright (c) 2017 Alex Diener This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution. Alex Diener alex@ludobloom.com */ #include "gamemath/BezierCurve.h" #include "gamemath/VectorConversions.h" #include #include Vector2f BezierCurve_sample(Vector2f p0, Vector2f c0, Vector2f c1, Vector2f p1, float value) { Vector2f midp0c0, midc0c1, midc1p1, midMid1, midMid2, result; midp0c0.x = p0.x + (c0.x - p0.x) * value; midp0c0.y = p0.y + (c0.y - p0.y) * value; midc0c1.x = c0.x + (c1.x - c0.x) * value; midc0c1.y = c0.y + (c1.y - c0.y) * value; midc1p1.x = c1.x + (p1.x - c1.x) * value; midc1p1.y = c1.y + (p1.y - c1.y) * value; midMid1.x = midp0c0.x + (midc0c1.x - midp0c0.x) * value; midMid1.y = midp0c0.y + (midc0c1.y - midp0c0.y) * value; midMid2.x = midc0c1.x + (midc1p1.x - midc0c1.x) * value; midMid2.y = midc0c1.y + (midc1p1.y - midc0c1.y) * value; result.x = midMid1.x + (midMid2.x - midMid1.x) * value; result.y = midMid1.y + (midMid2.y - midMid1.y) * value; return result; // Slow //return Vector2f_interpolate(Vector2f_interpolate(Vector2f_interpolate(p0, c0, value), Vector2f_interpolate(c0, c1, value), value), Vector2f_interpolate(Vector2f_interpolate(c0, c1, value), Vector2f_interpolate(c1, p1, value), value), value); } float BezierCurve_sampleXAtY(Vector2f p0, Vector2f c0, Vector2f c1, Vector2f p1, float y, unsigned int iterations) { float guess, guessAdjust; Vector2f leftSample, rightSample, newSample; unsigned int iteration; if (p0.y < p1.y) { if (y < p0.y) { return p0.x; } if (y > p1.y) { return p1.x; } } else { if (y < p1.y) { return p1.x; } if (y > p0.y) { return p0.x; } } guess = 0.5f; guessAdjust = 0.25f; leftSample = p0; rightSample = p1; for (iteration = 0; iteration < iterations; iteration++) { newSample = BezierCurve_sample(p0, c0, c1, p1, guess); if ((newSample.y < y) == (p0.y < p1.y)) { guess += guessAdjust; guessAdjust *= 0.5f; leftSample = newSample; } else { guess -= guessAdjust; guessAdjust *= 0.5f; rightSample = newSample; } } if (fabsf(leftSample.y - rightSample.y) < 0.00001f) { return leftSample.x; } return leftSample.x + (rightSample.x - leftSample.x) * (y - leftSample.y) / (rightSample.y - leftSample.y); } float BezierCurve_sampleYAtX(Vector2f p0, Vector2f c0, Vector2f c1, Vector2f p1, float x, unsigned int iterations) { return BezierCurve_sampleXAtY(Vector2f_yx(p0), Vector2f_yx(c0), Vector2f_yx(c1), Vector2f_yx(p1), x, iterations); } void BezierCurve_getSamples(Vector2f p0, Vector2f c0, Vector2f c1, Vector2f p1, Vector2f * outSamples, unsigned int sampleCount) { unsigned int sampleIndex; for (sampleIndex = 0; sampleIndex < sampleCount; sampleIndex++) { outSamples[sampleIndex] = BezierCurve_sample(p0, c0, c1, p1, sampleIndex / (float) (sampleCount - 1)); } }