Difference between revisions of "BezierCurve:evaluate"

(A better example. (The old example did exactly what BezierCurve:render is for.))
(Make a circle follow a curve)
Line 18: Line 18:
 
=== Make a circle follow a curve ===
 
=== Make a circle follow a curve ===
 
<source lang="lua">
 
<source lang="lua">
local curve = love.math.newBezierCurve({125,125, 125,225, 175,125, 225,125})
+
local controlPoints = {125,125, 125,225, 175,125, 225,125}
 +
local curve = love.math.newBezierCurve(controlPoints)
  
 
function love.draw()
 
function love.draw()
Line 29: Line 30:
 
love.graphics.line(curve:render())
 
love.graphics.line(curve:render())
 
end
 
end
 +
</source>
 +
 +
 +
=== Calculate point at value t ===
 +
<source lang="lua">
 +
function evaluateBezier (controlPoints, t)
 +
local nPts = #controlPoints /2
 +
local Vtemp = {}
 +
for i = 1,  #controlPoints, 2 do
 +
table.insert (Vtemp, {x=controlPoints[i], y=controlPoints[i+1]})
 +
end
 +
for i = 2, nPts do
 +
for j = 1, i-1 do
 +
Vtemp[j].x = (1-t)*Vtemp[j].x + t*Vtemp[j+1].x
 +
Vtemp[j].y = (1-t)*Vtemp[j].y + t*Vtemp[j+1].y
 +
end
 +
end
 +
local Q = {x=Vtemp[1].x, y=Vtemp[1].y}
 +
return Q
 +
end
 +
 +
local controlPoints = {125,125, 125,225, 175,125, 225,125}
 +
local t = 0.5
 +
local point = evaluateBezier (controlPoints, t)
 
</source>
 
</source>
  

Revision as of 17:44, 16 December 2021

Available since LÖVE 0.9.0
This function is not supported in earlier versions.

Evaluate Bézier curve at parameter t. The parameter must be between 0 and 1 (inclusive).

This function can be used to move objects along paths or tween parameters. However it should not be used to render the curve, see BezierCurve:render for that purpose.

Function

Synopsis

x,y = BezierCurve:evaluate(t)

Arguments

number t
Where to evaluate the curve.

Returns

number x
x coordinate of the curve at parameter t.
number y
y coordinate of the curve at parameter t.

Examples

Make a circle follow a curve

local controlPoints = {125,125, 125,225, 175,125, 225,125}
local curve = love.math.newBezierCurve(controlPoints)

function love.draw()
	local time     = love.timer.getTime()
	local loopTime = 4
	local t        = (time / loopTime) % 1
	local x, y     = curve:evaluate(t)

	love.graphics.circle("fill", x, y, 8)
	love.graphics.line(curve:render())
end


Calculate point at value t

function evaluateBezier (controlPoints, t)
	local nPts = #controlPoints /2
	local Vtemp = {}
	for i = 1,  #controlPoints, 2 do
		table.insert (Vtemp, {x=controlPoints[i], y=controlPoints[i+1]})
	end
	for i = 2, nPts do
		for j = 1, i-1 do
			Vtemp[j].x = (1-t)*Vtemp[j].x + t*Vtemp[j+1].x
			Vtemp[j].y = (1-t)*Vtemp[j].y + t*Vtemp[j+1].y
		end
	end
	local Q = {x=Vtemp[1].x, y=Vtemp[1].y}
	return Q
end

local controlPoints = {125,125, 125,225, 175,125, 225,125}
local t = 0.5
local point = evaluateBezier (controlPoints, t)

See Also


Other Languages