|
| 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