mirror of
https://github.com/love2d/love.git
synced 2026-08-12 16:40:57 +02:00
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
This commit is contained in:
@@ -20,7 +20,9 @@
|
||||
|
||||
#include "wrap_Math.h"
|
||||
#include "wrap_RandomGenerator.h"
|
||||
#include "wrap_BezierCurve.h"
|
||||
#include "MathModule.h"
|
||||
#include "BezierCurve.h"
|
||||
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
@@ -74,6 +76,44 @@ int w_newRandomGenerator(lua_State *L)
|
||||
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;
|
||||
@@ -228,6 +268,7 @@ static const luaL_Reg functions[] =
|
||||
{ "random", w_random },
|
||||
{ "randomnormal", w_randomnormal },
|
||||
{ "newRandomGenerator", w_newRandomGenerator },
|
||||
{ "newBezierCurve", w_newBezierCurve },
|
||||
{ "triangulate", w_triangulate },
|
||||
{ "isConvex", w_isConvex },
|
||||
{ "noise", w_noise },
|
||||
@@ -237,6 +278,7 @@ static const luaL_Reg functions[] =
|
||||
static const lua_CFunction types[] =
|
||||
{
|
||||
luaopen_randomgenerator,
|
||||
luaopen_beziercurve,
|
||||
0
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user