Skip to content

Commit 314eca5

Browse files
author
Alan W. Irwin
committed
Add fill pages to the 27th lua example to make the results consistent
with the corresponding C example. svn path=/trunk/; revision=11600
1 parent a078ef6 commit 314eca5

1 file changed

Lines changed: 56 additions & 22 deletions

File tree

examples/lua/x27.lua

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,52 +25,70 @@
2525
dofile("plplot_examples.lua")
2626

2727

28+
--------------------------------------------------------------------------
29+
-- Calculate greatest common divisor following pseudo-code for the
30+
-- Euclidian algorithm at http://en.wikipedia.org/wiki/Euclidean_algorithm
31+
32+
function gcd (a, b)
33+
a = math.floor(math.abs(a))
34+
b = math.floor(math.abs(b))
35+
while b~=0 do
36+
t = b
37+
b = math.mod(a,b)
38+
a = t
39+
end
40+
return a
41+
end
42+
2843
function cycloid()
2944
-- TODO
3045
end
3146

3247

33-
function spiro( params )
34-
NPNT = 20000
48+
function spiro( params, fill )
49+
NPNT = 2000
3550
xcoord = {}
3651
ycoord = {}
3752

3853
-- Fill the coordinates
39-
windings = params[4]
54+
-- Proper termination of the angle loop very near the beginning
55+
-- point, see
56+
-- http://mathforum.org/mathimages/index.php/Hypotrochoid.
57+
windings = math.floor(math.abs(params[2])/gcd(params[1], params[2]))
4058
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
59+
dphi = 2*math.pi/steps
4760

4861
for i = 1, windings*steps+1 do
4962
phi = (i-1) * dphi
5063
phiw = (params[1]-params[2])/params[2]*phi
5164
xcoord[i] = (params[1]-params[2])*math.cos(phi) + params[3]*math.cos(phiw)
5265
ycoord[i] = (params[1]-params[2])*math.sin(phi) - params[3]*math.sin(phiw)
5366

67+
if i == 1 then
68+
xmin = xcoord[i]
69+
xmax = xcoord[i]
70+
ymin = ycoord[i]
71+
ymax = ycoord[i]
72+
end
5473
if xmin>xcoord[i] then xmin = xcoord[i] end
5574
if xmax<xcoord[i] then xmax = xcoord[i] end
5675
if ymin>ycoord[i] then ymin = ycoord[i] end
5776
if ymax<ycoord[i] then ymax = ycoord[i] end
5877
end
5978

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
79+
xmin = xmin - 0.15*(xmax-xmin)
80+
xmax = xmax + 0.15*(xmax-xmin)
81+
ymin = ymin - 0.15*(ymax-ymin)
82+
ymax = ymax + 0.15*(ymax-ymin)
6983

7084
pl.wind(xmin, xmax, ymin, ymax)
7185

7286
pl.col0(1)
73-
pl.line(xcoord, ycoord)
87+
if fill == 1 then
88+
pl.fill(xcoord, ycoord)
89+
else
90+
pl.line(xcoord, ycoord)
91+
end
7492
end
7593

7694

@@ -83,6 +101,11 @@ end
83101
----------------------------------------------------------------------------
84102

85103
-- R, r, p, N
104+
-- R and r should be integers to give correct termination of the
105+
-- angle loop using gcd.
106+
-- N.B. N is just a place holder since it is no longer used
107+
-- (because we now have proper termination of the angle loop).
108+
86109
params = {
87110
{ 21, 7, 7, 3 }, -- Deltoid
88111
{ 21, 7, 10, 3 },
@@ -110,20 +133,31 @@ cycloid()
110133

111134
pl.ssub(3, 3) -- Three by three window
112135

136+
fill = 0
137+
for i = 1, 9 do
138+
pl.adv(0)
139+
pl.vpor(0, 1, 0, 1)
140+
spiro(params[i], fill)
141+
end
142+
143+
pl.adv(0)
144+
pl.ssub(1, 1) -- One window per curve
145+
113146
for i = 1, 9 do
114147
pl.adv(0)
115148
pl.vpor(0, 1, 0, 1)
116-
spiro(params[i])
149+
spiro(params[i], fill)
117150
end
118151

152+
-- fill the curves.
153+
fill = 1
119154
pl.adv(0)
120155
pl.ssub(1, 1) -- One window per curve
121156

122157
for i = 1, 9 do
123158
pl.adv(0)
124159
pl.vpor(0, 1, 0, 1)
125-
spiro(params[i])
160+
spiro(params[i], fill)
126161
end
127162

128-
-- Don't forget to call plend() to finish off!
129163
pl.plend()

0 commit comments

Comments
 (0)