Root/nanonote-files/example-files/data/Examples/lua-plplot-examples/x27.lua

Source at commit 868eb3b created 13 years 1 month ago.
By Xiangfu Liu, rename nanonote-example files
1--[[ $Id: x27.lua 9526 2009-02-13 22:06:13Z smekal $
2
3    Drawing "spirograph" curves - epitrochoids, cycolids, roulettes
4
5   Copyright (C) 2009 Werner Smekal
6
7  This file is part of PLplot.
8
9  PLplot is free software you can redistribute it and/or modify
10  it under the terms of the GNU General Library Public License as published
11  by the Free Software Foundation either version 2 of the License, or
12  (at your option) any later version.
13
14  PLplot is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Library General Public License for more details.
18
19  You should have received a copy of the GNU Library General Public License
20  along with PLplot if not, write to the Free Software
21  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22--]]
23
24-- initialise Lua bindings for PLplot examples.
25dofile("plplot_examples.lua")
26
27
28function cycloid()
29  -- TODO
30end
31
32
33function spiro( params )
34  NPNT = 20000
35  xcoord = {}
36  ycoord = {}
37
38  -- Fill the coordinates
39  windings = params[4]
40  steps = math.floor(NPNT/windings)
41  dphi = 8*math.acos(-1)/steps
42
43  xmin = 0 -- This initialisation is safe!
44  xmax = 0
45  ymin = 0
46  ymax = 0
47
48  for i = 1, windings*steps+1 do
49    phi = (i-1) * dphi
50    phiw = (params[1]-params[2])/params[2]*phi
51    xcoord[i] = (params[1]-params[2])*math.cos(phi) + params[3]*math.cos(phiw)
52    ycoord[i] = (params[1]-params[2])*math.sin(phi) - params[3]*math.sin(phiw)
53
54    if xmin>xcoord[i] then xmin = xcoord[i] end
55    if xmax<xcoord[i] then xmax = xcoord[i] end
56    if ymin>ycoord[i] then ymin = ycoord[i] end
57    if ymax<ycoord[i] then ymax = ycoord[i] end
58  end
59
60  if (xmax-xmin)>(ymax-ymin) then
61    scale = xmax - xmin
62  else
63    scale = ymax - ymin
64  end
65  xmin = -0.65*scale
66  xmax = 0.65*scale
67  ymin = -0.65*scale
68  ymax = 0.65*scale
69
70  pl.wind(xmin, xmax, ymin, ymax)
71
72  pl.col0(1)
73  pl.line(xcoord, ycoord)
74end
75
76
77----------------------------------------------------------------------------
78-- main
79--
80-- Generates two kinds of plots:
81-- - construction of a cycloid (animated)
82-- - series of epitrochoids and hypotrochoids
83----------------------------------------------------------------------------
84
85-- R, r, p, N
86params = {
87  { 21, 7, 7, 3 }, -- Deltoid
88  { 21, 7, 10, 3 },
89  { 21, -7, 10, 3 },
90  { 20, 3, 7, 20 },
91  { 20, 3, 10, 20 },
92  { 20, -3, 10, 20 },
93  { 20, 13, 7, 20 },
94  { 20, 13, 20, 20 },
95  { 20,-13, 20, 20 } }
96
97-- plplot initialization
98
99-- Parse and process command line arguments
100pl.parseopts(arg, pl.PL_PARSE_FULL)
101
102-- Initialize plplot
103pl.init()
104
105-- Illustrate the construction of a cycloid
106cycloid()
107
108-- Loop over the various curves
109-- First an overview, then all curves one by one
110
111pl.ssub(3, 3) -- Three by three window
112
113for i = 1, 9 do
114  pl.adv(0)
115  pl.vpor(0, 1, 0, 1)
116  spiro(params[i])
117end
118
119pl.adv(0)
120pl.ssub(1, 1) -- One window per curve
121
122for i = 1, 9 do
123  pl.adv(0)
124  pl.vpor(0, 1, 0, 1)
125  spiro(params[i])
126end
127
128-- Don't forget to call plend() to finish off!
129pl.plend()
130

Archive Download this file



interactive