Commits

vrld  committed ee6953b

Add support for Bezier curves of arbitrary degree.

New functions:

bezier = love.math.newBezierCurve(control-points)
degree = bezier:getDegree() -- degree = #control-points - 1
x,y = bezier:getControlPoint(pos) -- pos < 0 => string.sub semantics
bezier:setControlPoint(pos, x, y) -- pos < 0 => string.sub semantics
bezier:insertControlPoint(x,y, [pos = -1])
x,y = bezier:eval(t) -- 0 <= t <= 1
polyline = bezier:render([accuracy = 5]) -- returns a table of points

Example:

function love.load()
b = love.math.newBezierCurve(10,10, 80,400, 380,400, 470,10)
end

function love.draw()
if not curve then -- curve rendering is expensive-ish
curve = b:render()
end
love.graphics.line(curve)
end

  • Participants
  • Parent commits 629d1c4

Comments (0)

Files changed (6)

File src/common/runtime.cpp

 
 	// Math
 	{"RandomGenerator", MATH_RANDOM_GENERATOR_ID},
+	{"BezierCurve", MATH_BEZIER_CURVE_ID},
 
 	// Audio
 	{"Source", AUDIO_SOURCE_ID},

File src/common/types.h

 
 	// Math
 	MATH_RANDOM_GENERATOR_ID,
+	MATH_BEZIER_CURVE_ID,
 
 	// Audio
 	AUDIO_SOURCE_ID,
 
 // Math.
 const bits MATH_RANDOM_GENERATOR_T = (bits(1) << MATH_RANDOM_GENERATOR_ID) | OBJECT_T;
+const bits MATH_BEZIER_CURVE_T = (bits(1) << MATH_BEZIER_CURVE_ID) | OBJECT_T;
 
 // Audio.
 const bits AUDIO_SOURCE_T = (bits(1) << AUDIO_SOURCE_ID) | OBJECT_T;

File src/modules/math/MathModule.cpp

 // LOVE
 #include "MathModule.h"
 #include "common/Vector.h"
+#include "BezierCurve.h"
 
 // STL
 #include <cmath>
 	return new RandomGenerator();
 }
 
+BezierCurve *Math::newBezierCurve(const vector<Vector> &points)
+{
+	return new BezierCurve(points);
+}
+
 vector<Triangle> Math::triangulate(const vector<vertex> &polygon)
 {
 	if (polygon.size() < 3)

File src/modules/math/MathModule.h

 // LOVE
 #include "common/Module.h"
 #include "common/math.h"
+#include "common/Vector.h"
 #include "common/int.h"
 
 // Noise
 namespace math
 {
 
+class BezierCurve;
+
 class Math : public Module
 {
 private:
 	 **/
 	RandomGenerator *newRandomGenerator();
 
+	/**
+	 * Creates a new bezier curve.
+	 **/
+	BezierCurve *newBezierCurve(const std::vector<Vector> &points);
+
 	virtual const char *getName() const
 	{
 		return "love.math";

File src/modules/math/wrap_Math.cpp

 
 #include "wrap_Math.h"
 #include "wrap_RandomGenerator.h"
+#include "wrap_BezierCurve.h"
 #include "MathModule.h"
+#include "BezierCurve.h"
 
 #include <cmath>
 #include <iostream>
 	return 1;
 }
 
+int w_newBezierCurve(lua_State *L)
+{
+	std::vector<Vector> points;
+	if (lua_istable(L, 1))
+	{
+		size_t top = lua_objlen(L, 1);
+		points.reserve(top / 2);
+		for (size_t i = 1; i <= top; i += 2)
+		{
+			lua_rawgeti(L, 1, i);
+			lua_rawgeti(L, 1, i+1);
+
+			Vector v;
+			v.x = luaL_checknumber(L, -2);
+			v.y = luaL_checknumber(L, -1);
+			points.push_back(v);
+
+			lua_pop(L, 2);
+		}
+	}
+	else
+	{
+		size_t top = lua_gettop(L);
+		points.reserve(top / 2);
+		for (size_t i = 1; i <= top; i += 2)
+		{
+			Vector v;
+			v.x = luaL_checknumber(L, i);
+			v.y = luaL_checknumber(L, i+1);
+			points.push_back(v);
+		}
+	}
+
+	BezierCurve *curve = Math::instance.newBezierCurve(points);
+	luax_newtype(L, "BezierCurve", MATH_BEZIER_CURVE_T, (void *)curve);
+	return 1;
+}
+
 int w_triangulate(lua_State *L)
 {
 	std::vector<vertex> vertices;
 	{ "random", w_random },
 	{ "randomnormal", w_randomnormal },
 	{ "newRandomGenerator", w_newRandomGenerator },
+	{ "newBezierCurve", w_newBezierCurve },
 	{ "triangulate", w_triangulate },
 	{ "isConvex", w_isConvex },
 	{ "noise", w_noise },
 static const lua_CFunction types[] =
 {
 	luaopen_randomgenerator,
+	luaopen_beziercurve,
 	0
 };
 

File src/modules/math/wrap_Math.h

 int w_random(lua_State *L);
 int w_randomnormal(lua_State *L);
 int w_newRandomGenerator(lua_State *L);
+int w_newBezierCurve(lua_State *L);
 int w_triangulate(lua_State *L);
 int w_isConvex(lua_State *L);
 int w_noise(lua_State *L);