Skip to content

Commit 23cc35b

Browse files
committedOct 28, 2014
Add two examples of animation
1 parent 3399bf2 commit 23cc35b

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
 

‎examples/integral-anim.lua

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
require "graph"
2+
3+
local pi, sin, cos, exp = math.pi, math.sin, math.cos, math.exp
4+
5+
local function integral_fill()
6+
local function f(x) return exp(-0.1*x) * cos(x) end
7+
local p = graph.plot "y = f(x)"
8+
local x0, x1 = 0, 10*pi
9+
local cc = graph.fxline(f, x0, x1, k)
10+
p.sync = false
11+
p:limits(0, -1, 10*pi, 1)
12+
p:pushlayer()
13+
p:show()
14+
local N = 256
15+
local yellow = graph.rgba(255,255,0,155)
16+
for k= 2, N do
17+
local x = x0 + k * (x1-x0) / N
18+
local ca = graph.fxline(f, x0, x, k)
19+
ca:line_to(x, 0); ca:line_to(0, 0); ca:close()
20+
p:clear()
21+
p:add(ca, yellow)
22+
p:addline(cc)
23+
p:flush()
24+
end
25+
end
26+
27+
integral_fill()

‎examples/vonkoch-anim.lua

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
require "graph"
2+
3+
local pi, sqrt3_2 = math.pi, math.sqrt(3)/2
4+
5+
local sx = {2, 1, -1, -2, -1, 1}
6+
local sy = {0, 1, 1, 0, -1, -1}
7+
8+
for k=1, 6 do
9+
sx[k] = 0.5 * sx[k]
10+
sy[k] = sqrt3_2 * sy[k]
11+
end
12+
13+
local function vonkoch_alpha(n, alpha)
14+
local sh = {1, -2, 1}
15+
local a, x, y = 0, 0, 0
16+
local w = {}
17+
for i = 1, n+1 do w[i] = 0 end
18+
local s = 1 / (3^n)
19+
local line = graph.path(x, y)
20+
while w[n+1] == 0 do
21+
local x1, y1 = x + (s/3) * sx[a+1], y + (s/3) * sy[a+1]
22+
local ap = (a + 1) % 6
23+
local x2e, y2e = x1 + (s/3) * sx[ap+1], y1 + (s/3) * sy[ap+1]
24+
local x2b, y2b = x + (s/2) * sx[a +1], y + (s/2) * sy[a +1]
25+
local x3, y3 = x + (2*s/3) * sx[a+1], y + (2*s/3) * sy[a+1]
26+
line:line_to(x1, y1)
27+
line:line_to(x2b * (1-alpha) + x2e * alpha, y2b * (1-alpha) + y2e * alpha)
28+
line:line_to(x3, y3)
29+
x, y = x + s * sx[a+1], y + s * sy[a+1]
30+
line:line_to(x, y)
31+
for k = 1, n+1 do
32+
w[k] = (w[k] + 1) % 4
33+
if w[k] ~= 0 then
34+
a = (a + sh[w[k]]) % 6
35+
break
36+
end
37+
end
38+
end
39+
return line
40+
end
41+
42+
-- Create an empty plot (canvas). Since it is created as a "canvas" we
43+
-- have to explicitily set the limits and perform the flush operation.
44+
p = graph.canvas("Von Koch's curve")
45+
p:limits(-0.15, -1, 1.15, 0.3)
46+
p.units = false
47+
p:show()
48+
49+
-- Create the inner solid triangle
50+
local t = graph.path()
51+
t:move_to(0,0)
52+
t:line_to(1,0)
53+
t:line_to(0.5,-sqrt3_2)
54+
t:close()
55+
56+
-- Defines two colors for the outline and the filling.
57+
local c = graph.webcolor(1)
58+
local cfill = graph.rgba(0,0,180,50)
59+
60+
-- Add the inner triangle to the plot and push a new graphical layer.
61+
-- In this way the triangle will remain whel p:clear() will be called.
62+
p:add(t, cfill)
63+
p:pushlayer()
64+
65+
for n = 0, 4 do
66+
for alpha = 0, 1.0, 0.01 do
67+
p:clear() -- clear the plot
68+
69+
-- Create the Von Koch's curve segment
70+
local v = vonkoch_alpha(n, alpha)
71+
72+
-- Add the segment three times but rotated and translated with
73+
-- a solid fill.
74+
p:add(v, cfill)
75+
p:add(v, cfill, {}, {{"translate", x=1, y=0}, {"rotate", angle=-2*pi/3}})
76+
p:add(v, cfill, {}, {{"translate", x=0.5, y=-sqrt3_2}, {"rotate", angle=-2*2*pi/3}})
77+
78+
-- Add the same lines that before but only to make the outlines.
79+
p:addline(v, c)
80+
p:addline(v, c, {}, {{"translate", x=1, y=0}, {"rotate", angle=-2*pi/3}})
81+
p:addline(v, c, {}, {{"translate", x=0.5, y=-sqrt3_2}, {"rotate", angle=-2*2*pi/3}})
82+
83+
-- Flush the plot so that the windows is actually updated on the screeen.
84+
p:flush()
85+
end
86+
end

0 commit comments

Comments
 (0)
Please sign in to comment.