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

1--[[ $Id: x28.lua 10710 2009-12-08 06:51:27Z airwin $
2
3    pl.mtex3, plptex3 demo.
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
25-- initialise Lua bindings for PLplot examples.
26dofile("plplot_examples.lua")
27
28-- Choose these values to correspond to tick marks.
29XPTS = 2
30YPTS = 2
31NREVOLUTION = 16
32NROTATION = 8
33NSHEAR = 8
34
35----------------------------------------------------------------------------
36-- main
37--
38-- Demonstrates plotting text in 3D.
39----------------------------------------------------------------------------
40
41xmin=0
42xmax=1
43xmid = 0.5*(xmax + xmin)
44xrange = xmax - xmin
45ymin=0
46ymax=1
47ymid = 0.5*(ymax + ymin)
48yrange = ymax - ymin
49zmin=0
50zmax=1
51zmid = 0.5*(zmax + zmin)
52zrange = zmax - zmin
53ysmin = ymin + 0.1 * yrange
54ysmax = ymax - 0.1 * yrange
55ysrange = ysmax - ysmin
56dysrot = ysrange / ( NROTATION - 1 )
57dysshear = ysrange / ( NSHEAR - 1 )
58zsmin = zmin + 0.1 * zrange
59zsmax = zmax - 0.1 * zrange
60zsrange = zsmax - zsmin
61dzsrot = zsrange / ( NROTATION - 1 )
62dzsshear = zsrange / ( NSHEAR - 1 )
63
64pstring = "The future of our civilization depends on software freedom."
65
66-- Allocate and define the minimal x, y, and z to insure 3D box
67x = {}
68y = {}
69z = {}
70   
71for i = 1, XPTS do
72  x[i] = xmin + (i-1) * (xmax-xmin)/(XPTS-1)
73end
74
75for j = 1, YPTS do
76 y[j] = ymin + (j-1) * (ymax-ymin)/(YPTS-1)
77end
78
79for i = 1, XPTS do
80  z[i] = {}
81  for j = 1, YPTS do
82    z[i][j] = 0
83  end
84end
85
86-- Parse and process command line arguments
87pl.parseopts(arg, pl.PL_PARSE_FULL)
88   
89pl.init()
90
91-- Page 1: Demonstrate inclination and shear capability pattern.
92pl.adv(0)
93pl.vpor(-0.15, 1.15, -0.05, 1.05)
94pl.wind(-1.2, 1.2, -0.8, 1.5)
95pl.w3d(1, 1, 1, xmin, xmax, ymin, ymax, zmin, zmax, 20, 45)
96
97pl.col0(2)
98pl.box3("b", "", xmax-xmin, 0,
99        "b", "", ymax-ymin, 0,
100        "bcd", "", zmax-zmin, 0)
101
102-- z = zmin.
103pl.schr(0, 1)
104for i = 1, NREVOLUTION do
105  omega = 2*math.pi*(i-1)/NREVOLUTION
106  sin_omega = math.sin(omega)
107  cos_omega = math.cos(omega)
108  x_inclination = 0.5*xrange*cos_omega
109  y_inclination = 0.5*yrange*sin_omega
110  z_inclination = 0
111  x_shear = -0.5*xrange*sin_omega
112  y_shear = 0.5*yrange*cos_omega
113  z_shear = 0
114  pl.ptex3( xmid, ymid, zmin, x_inclination, y_inclination, z_inclination,
115           x_shear, y_shear, z_shear, 0, " revolution")
116end
117
118-- x = xmax.
119pl.schr(0, 1)
120for i = 1, NREVOLUTION do
121  omega = 2.*math.pi*(i-1)/NREVOLUTION
122  sin_omega = math.sin(omega)
123  cos_omega = math.cos(omega)
124  x_inclination = 0.
125  y_inclination = -0.5*yrange*cos_omega
126  z_inclination = 0.5*zrange*sin_omega
127  x_shear = 0
128  y_shear = 0.5*yrange*sin_omega
129  z_shear = 0.5*zrange*cos_omega
130  pl.ptex3(xmax, ymid, zmid, x_inclination, y_inclination, z_inclination,
131           x_shear, y_shear, z_shear, 0, " revolution")
132end
133
134-- y = ymax.
135pl.schr(0, 1)
136for i = 1, NREVOLUTION do
137  omega = 2.*math.pi*(i-1)/NREVOLUTION
138  sin_omega = math.sin(omega)
139  cos_omega = math.cos(omega)
140  x_inclination = 0.5*xrange*cos_omega
141  y_inclination = 0.
142  z_inclination = 0.5*zrange*sin_omega
143  x_shear = -0.5*xrange*sin_omega
144  y_shear = 0.
145  z_shear = 0.5*zrange*cos_omega
146  pl.ptex3(xmid, ymax, zmid, x_inclination, y_inclination, z_inclination,
147           x_shear, y_shear, z_shear, 0, " revolution")
148end
149
150-- Draw minimal 3D grid to finish defining the 3D box.
151pl.mesh(x, y, z, pl.DRAW_LINEXY)
152
153-- Page 2: Demonstrate rotation of string around its axis.
154pl.adv(0)
155pl.vpor(-0.15, 1.15, -0.05, 1.05)
156pl.wind(-1.2, 1.2, -0.8, 1.5)
157pl.w3d(1, 1, 1, xmin, xmax, ymin, ymax, zmin, zmax, 20, 45)
158
159pl.col0(2)
160pl.box3("b", "", xmax-xmin, 0,
161        "b", "", ymax-ymin, 0,
162        "bcd", "", zmax-zmin, 0)
163
164-- y = ymax.
165pl.schr(0, 1)
166x_inclination = 1
167y_inclination = 0
168z_inclination = 0
169x_shear = 0
170for i = 1, NROTATION do
171  omega = 2.*math.pi*(i-1)/NROTATION
172  sin_omega = math.sin(omega)
173  cos_omega = math.cos(omega)
174  y_shear = 0.5*yrange*sin_omega
175  z_shear = 0.5*zrange*cos_omega
176  zs = zsmax - dzsrot * (i-1)
177  pl.ptex3(xmid, ymax, zs,
178           x_inclination, y_inclination, z_inclination,
179           x_shear, y_shear, z_shear,
180           0.5, "rotation for y = y#dmax#u")
181end
182
183-- x = xmax.
184pl.schr(0, 1)
185x_inclination = 0
186y_inclination = -1
187z_inclination = 0
188y_shear = 0
189for i = 1, NROTATION do
190  omega = 2.*math.pi*(i-1)/NROTATION
191  sin_omega = math.sin(omega)
192  cos_omega = math.cos(omega)
193  x_shear = 0.5*xrange*sin_omega
194  z_shear = 0.5*zrange*cos_omega
195  zs = zsmax - dzsrot * (i-1)
196  pl.ptex3(xmax, ymid, zs,
197           x_inclination, y_inclination, z_inclination,
198           x_shear, y_shear, z_shear,
199           0.5, "rotation for x = x#dmax#u")
200end
201
202-- z = zmin.
203pl.schr(0, 1)
204x_inclination = 1
205y_inclination = 0
206z_inclination = 0
207x_shear = 0
208for i = 1, NROTATION do
209  omega = 2.*math.pi*(i-1)/NROTATION
210  sin_omega = math.sin(omega)
211  cos_omega = math.cos(omega)
212  y_shear = 0.5*yrange*cos_omega
213  z_shear = 0.5*zrange*sin_omega
214  ys = ysmax - dysrot * (i-1)
215  pl.ptex3(xmid, ys, zmin,
216           x_inclination, y_inclination, z_inclination,
217           x_shear, y_shear, z_shear,
218           0.5, "rotation for z = z#dmin#u")
219end
220
221-- Draw minimal 3D grid to finish defining the 3D box.
222pl.mesh(x, y, z, pl.DRAW_LINEXY)
223
224-- Page 3: Demonstrate shear of string along its axis.
225-- Work around xcairo and pngcairo (but not pscairo) problems for
226-- shear vector too close to axis of string. (N.B. no workaround
227-- would be domega = 0.)
228domega = 0.05
229pl.adv(0)
230pl.vpor(-0.15, 1.15, -0.05, 1.05)
231pl.wind(-1.2, 1.2, -0.8, 1.5)
232pl.w3d(1, 1, 1, xmin, xmax, ymin, ymax, zmin, zmax, 20, 45)
233
234pl.col0(2)
235pl.box3("b", "", xmax-xmin, 0,
236        "b", "", ymax-ymin, 0,
237        "bcd", "", zmax-zmin, 0)
238
239-- y = ymax.
240pl.schr(0, 1)
241x_inclination = 1
242y_inclination = 0
243z_inclination = 0
244y_shear = 0
245for i = 1, NSHEAR do
246  omega = domega + 2.*math.pi*(i-1)/NSHEAR
247  sin_omega = math.sin(omega)
248  cos_omega = math.cos(omega)
249  x_shear = 0.5*xrange*sin_omega
250  z_shear = 0.5*zrange*cos_omega
251  zs = zsmax - dzsshear * (i-1)
252  pl.ptex3(xmid, ymax, zs,
253           x_inclination, y_inclination, z_inclination,
254           x_shear, y_shear, z_shear,
255           0.5, "shear for y = y#dmax#u")
256end
257
258-- x = xmax.
259pl.schr(0, 1)
260x_inclination = 0
261y_inclination = -1
262z_inclination = 0
263x_shear = 0
264for i = 1, NSHEAR do
265  omega = domega + 2.*math.pi*(i-1)/NSHEAR
266  sin_omega = math.sin(omega)
267  cos_omega = math.cos(omega)
268  y_shear = -0.5*yrange*sin_omega
269  z_shear = 0.5*zrange*cos_omega
270  zs = zsmax - dzsshear * (i-1)
271  pl.ptex3(xmax, ymid, zs,
272           x_inclination, y_inclination, z_inclination,
273           x_shear, y_shear, z_shear,
274           0.5, "shear for x = x#dmax#u")
275end
276
277-- z = zmin.
278pl.schr(0, 1)
279x_inclination = 1
280y_inclination = 0
281z_inclination = 0
282z_shear = 0
283for i = 1, NSHEAR do
284  omega = domega + 2.*math.pi*(i-1)/NSHEAR
285  sin_omega = math.sin(omega)
286  cos_omega = math.cos(omega)
287  y_shear = 0.5*yrange*cos_omega
288  x_shear = 0.5*xrange*sin_omega
289  ys = ysmax - dysshear * (i-1)
290  pl.ptex3(xmid, ys, zmin,
291           x_inclination, y_inclination, z_inclination,
292           x_shear, y_shear, z_shear,
293           0.5, "shear for z = z#dmin#u")
294end
295
296-- Draw minimal 3D grid to finish defining the 3D box.
297pl.mesh(x, y, z, pl.DRAW_LINEXY)
298
299-- Page 4: Demonstrate drawing a string on a 3D path.
300pl.adv(0)
301pl.vpor(-0.15, 1.15, -0.05, 1.05)
302pl.wind(-1.2, 1.2, -0.8, 1.5)
303pl.w3d(1, 1, 1, xmin, xmax, ymin, ymax, zmin, zmax, 40, -30)
304
305pl.col0(2)
306pl.box3("b", "", xmax-xmin, 0,
307        "b", "", ymax-ymin, 0,
308        "bcd", "", zmax-zmin, 0)
309
310pl.schr(0, 1.2)
311-- domega controls the spacing between the various characters of the
312-- string and also the maximum value of omega for the given number
313-- of characters in *pstring.
314domega = 2.*math.pi/string.len(pstring)
315omega = 0
316
317-- 3D function is a helix of the given radius and pitch
318radius = 0.5
319pitch = 1/(2*math.pi)
320
321for i = 1, string.len(pstring) do
322  sin_omega = math.sin(omega)
323  cos_omega = math.cos(omega)
324  xpos = xmid + radius*sin_omega
325  ypos = ymid - radius*cos_omega
326  zpos = zmin + pitch*omega
327  
328  -- In general, the inclination is proportional to the derivative of
329  --the position wrt theta.
330  x_inclination = radius*cos_omega
331  y_inclination = radius*sin_omega
332  z_inclination = pitch
333  
334  -- The shear vector should be perpendicular to the 3D line with Z
335  -- component maximized, but for low pitch a good approximation is
336  --a constant vector that is parallel to the Z axis.
337  x_shear = 0
338  y_shear = 0
339  z_shear = 1
340  pl.ptex3(xpos, ypos, zpos, x_inclination, y_inclination, z_inclination,
341           x_shear, y_shear, z_shear, 0.5, string.sub(pstring, i, i))
342  omega = omega + domega
343end
344
345-- Draw minimal 3D grid to finish defining the 3D box.
346pl.mesh(x, y, z, pl.DRAW_LINEXY)
347
348-- Page 5: Demonstrate pl.mtex3 axis labelling capability
349pl.adv(0)
350pl.vpor(-0.15, 1.15, -0.05, 1.05)
351pl.wind(-1.2, 1.2, -0.8, 1.5)
352pl.w3d(1, 1, 1, xmin, xmax, ymin, ymax, zmin, zmax, 20, 45)
353   
354pl.col0(2)
355pl.box3("b", "", xmax-xmin, 0,
356        "b", "", ymax-ymin, 0,
357        "bcd", "", zmax-zmin, 0)
358
359pl.schr(0, 1)
360pl.mtex3("xp", 3, 0.5, 0.5, "Arbitrarily displaced")
361pl.mtex3("xp", 4.5, 0.5, 0.5, "primary X-axis label")
362pl.mtex3("xs", -2.5, 0.5, 0.5, "Arbitrarily displaced")
363pl.mtex3("xs", -1, 0.5, 0.5, "secondary X-axis label")
364pl.mtex3("yp", 3, 0.5, 0.5, "Arbitrarily displaced")
365pl.mtex3("yp", 4.5, 0.5, 0.5, "primary Y-axis label")
366pl.mtex3("ys", -2.5, 0.5, 0.5, "Arbitrarily displaced")
367pl.mtex3("ys", -1, 0.5, 0.5, "secondary Y-axis label")
368pl.mtex3("zp", 4.5, 0.5, 0.5, "Arbitrarily displaced")
369pl.mtex3("zp", 3, 0.5, 0.5, "primary Z-axis label")
370pl.mtex3("zs", -2.5, 0.5, 0.5, "Arbitrarily displaced")
371pl.mtex3("zs", -1, 0.5, 0.5, "secondary Z-axis label")
372
373-- Draw minimal 3D grid to finish defining the 3D box.
374pl.mesh(x, y, z, pl.DRAW_LINEXY)
375
376pl.plend()
377

Archive Download this file



interactive