1 //  SuperTux
2 //  Copyright (C) 2020 A. Semphris <semphris@protonmail.com>
3 //
4 //  This program is free software: you can redistribute it and/or modify
5 //  it under the terms of the GNU General Public License as published by
6 //  the Free Software Foundation, either version 3 of the License, or
7 //  (at your option) any later version.
8 //
9 //  This program is distributed in the hope that it will be useful,
10 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //  GNU General Public License for more details.
13 //
14 //  You should have received a copy of the GNU General Public License
15 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef HEADER_SUPERTUX_MATH_BEZIER_HPP
18 #define HEADER_SUPERTUX_MATH_BEZIER_HPP
19 
20 #include <math/vector.hpp>
21 
22 class Color;
23 class DrawingContext;
24 
25 class Bezier
26 {
27 public:
28   // p1 is first anchor, p2 is first handle, p3 is second handle, p4 is second anchor. T is progress from p1 towards p4.
29   static Vector get_point(const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, float t);
30   // Same as above, but does not treat p1 == p2 && p3 == p4 as a special case
31   static Vector get_point_raw(const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, float t);
32   // Calculates the full length of the bezier curve (approximated)
33   static float get_length(const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, int steps = 100);
34   // Gets the point at the given length
35   static Vector get_point_at_length(const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, float length, int steps = 100);
36   // Same as get_point but gets length-normalized
37   static Vector get_point_by_length(const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, float t);
38   // FIXME: Move this to the Canvas object?
39   static void draw_curve(DrawingContext& context, const Vector& p1, const Vector& p2, const Vector& p3, const Vector& p4, int steps, Color color, int layer);
40 
41 private:
42   Bezier(const Bezier&) = delete;
43   Bezier& operator=(const Bezier&) = delete;
44 };
45 
46 #endif
47 
48 /* EOF */
49