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

1--[[ $Id: x03.lua 10613 2009-11-19 12:05:09Z andrewross $
2
3    Polar plot demo.
4
5  Copyright (C) 2008 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----------------------------------------------------------------------------
28-- main
29--
30-- Generates polar plot, with 1-1 scaling.
31----------------------------------------------------------------------------
32
33x0 = {}
34y0 = {}
35x = {}
36y = {}
37
38dtr = math.pi/180
39for i = 1, 361 do
40  x0[i] = math.cos(dtr * (i-1))
41  y0[i] = math.sin(dtr * (i-1))
42end
43
44-- Parse and process command line arguments
45
46pl.parseopts(arg, pl.PL_PARSE_FULL)
47
48-- Set orientation to portrait - note not all device drivers
49-- support this, in particular most interactive drivers do not
50pl.sori(1)
51
52-- Initialize plplot
53pl.init()
54
55-- Set up viewport and window, but do not draw box
56pl.env(-1.3, 1.3, -1.3, 1.3, 1, -2)
57
58-- Draw circles for polar grid
59for i = 1, 10 do
60        pl.arc(0, 0, 0.1*i, 0.1*i, 0, 360, 0);
61end
62
63pl.col0(2)
64for i=1, 12 do
65    theta = 30 * (i-1)
66    dx = math.cos(dtr * theta)
67    dy = math.sin(dtr * theta)
68
69  -- Draw radial spokes for polar grid
70
71    pl.join(0, 0, dx, dy)
72
73  -- Write labels for angle
74    if theta < 9.99 then
75      offset = 0.45
76    else
77    if theta < 99.9 then
78      offset = 0.30
79    else
80      offset = 0.15
81    end
82    end
83
84  -- Slightly off zero to avoid floating point logic flips at 90 and 270 deg.
85    if dx >= -0.00001 then
86        pl.ptex(dx, dy, dx, dy, -offset, tostring(math.floor(theta)))
87    else
88        pl.ptex(dx, dy, -dx, -dy, 1.+offset, tostring(math.floor(theta)))
89  end
90end
91
92-- Draw the graph
93for i=1, 361 do
94  r = math.sin(dtr * (5*(i-1)))
95  x[i] = x0[i] * r
96  y[i] = y0[i] * r
97end
98pl.col0(3)
99pl.line(x, y)
100
101pl.col0(4)
102pl.mtex("t", 2, 0.5, 0.5, "#frPLplot Example 3 - r(#gh)=sin 5#gh")
103
104-- Close the plot at end
105pl.plend()
106

Archive Download this file



interactive