The code below is another (simple) experiment:
--
-- Knot Graph
--
-- With acknowledgements to Skip Pennock:
-- http://www.mi.sanu.ac.rs/vismath/pennock/index.html
--
supportedOrientations(LANDSCAPE_ANY)
function setup()
isSweepLine = false -- true for a sweep effect
step = 3 -- Step (in degrees)
speed = 100 / step -- Speed for animation
R = 5
iparameter("bight", 1, 10, 10)
iparameter("lead", 1, 10, 3)
dim = math.min(WIDTH, HEIGHT)
w = WIDTH/2
h = HEIGHT/2
init()
strokeWidth(5)
fill(255)
font("Inconsolata")
fontSize(24)
end
-- Not optimised for high FPS for large leads
function draw()
t = ElapsedTime - startTime
background(0)
if b ~= bight or l ~= lead then -- Sliders moved?
init() -- Reinitialise
end
translate(w, h) -- (0, 0) to viewer centre
stroke(0, 255, 0) -- Draw green axes
line(-w, 0, w, 0)
line(0, -h, 0, h)
text(title, 0, h - 20) -- Draw white title
stroke(255) -- Draw white plot
local n = math.min(
math.floor(t * speed) % (#points + 60) + 1,
#points) -- 'min' and '+60' makes pause at end
local p1 = points[1]
for i = 2, n do
local p2 = points[i]
line(p1.x, p1.y, p2.x, p2.y)
p1 = p2
end
if isSweepLine then -- Green sweep line?
stroke(0, 255, 10, 255)
line(0, 0, p1.x, p1.y)
end
end
function init()
b = bight
l = lead
title = "r = "..R.." + cos("
if b > 1 then title = title..b.." * " end -- Nicely ...
title = title.."theta"
if l > 1 then title = title.." / "..l end -- ...formatted
title = title..")"
points = {} -- Table of points of curve
local deg2rad = math.pi / 180 -- Factor to convert to radians
for angle = 0, 360 * l, step do
local theta = angle * deg2rad
local r = R + math.cos(b * theta / l) -- r = f(theta)
r = r * dim / 2 * 0.9 / (R + 1) -- Scale to viewer
table.insert(points,
vec2(r * math.cos(theta), r * math.sin(theta)))
end
startTime = ElapsedTime
end