OpenWrt packages
Sign in or create your account | Project List | Help
OpenWrt packages Git Source Tree
Root/
| 1 | --[[ $Id: x16.lua 10304 2009-08-20 09:05:43Z andrewross $ |
| 2 | |
| 3 | plshade demo, using color fill. |
| 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. |
| 25 | dofile("plplot_examples.lua") |
| 26 | |
| 27 | -- Fundamental settings. See notes[] for more info. |
| 28 | ns = 20 -- Default number of shade levels |
| 29 | nx = 35 -- Default number of data points in x |
| 30 | ny = 46 -- Default number of data points in y |
| 31 | exclude = 0 -- By default do not plot a page illustrating |
| 32 | -- exclusion. API is probably going to change |
| 33 | -- anyway, and cannot be reproduced by any |
| 34 | -- front end other than the C one. |
| 35 | |
| 36 | -- polar plot data |
| 37 | PERIMETERPTS = 100 |
| 38 | |
| 39 | -- Transformation function |
| 40 | tr = {} |
| 41 | |
| 42 | function mypltr(x, y) |
| 43 | tx = tr[1] * x + tr[2] * y + tr[3] |
| 44 | ty = tr[4] * x + tr[5] * y + tr[6] |
| 45 | |
| 46 | return tx, ty |
| 47 | end |
| 48 | |
| 49 | ---------------------------------------------------------------------------- |
| 50 | -- f2mnmx |
| 51 | -- |
| 52 | -- Returns min & max of input 2d array. |
| 53 | ---------------------------------------------------------------------------- |
| 54 | function f2mnmx(f, nx, ny) |
| 55 | fmax = f[1][1] |
| 56 | fmin = fmax |
| 57 | |
| 58 | for i = 1, nx do |
| 59 | for j = 1, ny do |
| 60 | fmax = math.max(fmax, f[i][j]) |
| 61 | fmin = math.min(fmin, f[i][j]) |
| 62 | end |
| 63 | end |
| 64 | |
| 65 | return fmin, fmax |
| 66 | end |
| 67 | |
| 68 | |
| 69 | function zdefined(x, y) |
| 70 | z = math.sqrt(x^2 + y^2) |
| 71 | |
| 72 | return z<0.4 or z>0.6 |
| 73 | end |
| 74 | |
| 75 | |
| 76 | ---------------------------------------------------------------------------- |
| 77 | -- main |
| 78 | -- |
| 79 | -- Does several shade plots using different coordinate mappings. |
| 80 | ---------------------------------------------------------------------------- |
| 81 | |
| 82 | px = {} |
| 83 | py = {} |
| 84 | |
| 85 | fill_width = 2 |
| 86 | cont_color = 0 |
| 87 | cont_width = 0 |
| 88 | |
| 89 | -- Parse and process command line arguments |
| 90 | pl.parseopts(arg, pl.PL_PARSE_FULL) |
| 91 | |
| 92 | -- Load colour palettes |
| 93 | pl.spal0("cmap0_black_on_white.pal"); |
| 94 | pl.spal1("cmap1_gray.pal",1); |
| 95 | |
| 96 | -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display |
| 97 | pl.scmap0n(3) |
| 98 | |
| 99 | -- Initialize plplot |
| 100 | pl.init() |
| 101 | |
| 102 | -- Set up transformation function |
| 103 | tr = { 2/(nx-1), 0, -1, 0, 2/(ny-1), -1 } |
| 104 | |
| 105 | -- Allocate data structures |
| 106 | clevel = {} |
| 107 | shedge = {} |
| 108 | z = {} |
| 109 | w = {} |
| 110 | |
| 111 | -- Set up data array |
| 112 | for i = 1, nx do |
| 113 | x = (i-1 - math.floor(nx/2))/math.floor(nx/2) |
| 114 | z[i] = {} |
| 115 | w[i] = {} |
| 116 | for j = 1, ny do |
| 117 | y = (j-1 - math.floor(ny/2))/math.floor(ny/2)-1 |
| 118 | z[i][j] = -math.sin(7*x) * math.cos(7*y) + x^2 - y^2 |
| 119 | w[i][j] = -math.cos(7*x) * math.sin(7*y) + 2*x*y |
| 120 | end |
| 121 | end |
| 122 | |
| 123 | zmin, zmax = f2mnmx(z, nx, ny) |
| 124 | for i = 1, ns do |
| 125 | clevel[i] = zmin + (zmax-zmin)*(i-0.5)/ns |
| 126 | end |
| 127 | |
| 128 | for i = 1, ns+1 do |
| 129 | shedge[i] = zmin + (zmax-zmin)*(i-1)/ns |
| 130 | end |
| 131 | |
| 132 | -- Set up coordinate grids |
| 133 | cgrid1 = {} |
| 134 | cgrid1["xg"] = {} |
| 135 | cgrid1["yg"] = {} |
| 136 | cgrid1["nx"] = nx |
| 137 | cgrid1["ny"] = ny |
| 138 | |
| 139 | cgrid2 = {} |
| 140 | cgrid2["xg"] = {} |
| 141 | cgrid2["yg"] = {} |
| 142 | cgrid2["nx"] = nx |
| 143 | cgrid2["ny"] = ny |
| 144 | |
| 145 | for i = 1, nx do |
| 146 | cgrid2["xg"][i] = {} |
| 147 | cgrid2["yg"][i] = {} |
| 148 | for j = 1, ny do |
| 149 | x, y = mypltr(i-1, j-1) |
| 150 | |
| 151 | argx = x*math.pi/2 |
| 152 | argy = y*math.pi/2 |
| 153 | distort = 0.4 |
| 154 | |
| 155 | cgrid1["xg"][i] = x + distort * math.cos(argx) |
| 156 | cgrid1["yg"][j] = y - distort * math.cos(argy) |
| 157 | |
| 158 | cgrid2["xg"][i][j] = x + distort * math.cos(argx) * math.cos(argy) |
| 159 | cgrid2["yg"][i][j] = y - distort * math.cos(argx) * math.cos(argy) |
| 160 | end |
| 161 | end |
| 162 | |
| 163 | -- Plot using identity transform |
| 164 | pl.adv(0) |
| 165 | pl.vpor(0.1, 0.9, 0.1, 0.9) |
| 166 | pl.wind(-1, 1, -1, 1) |
| 167 | |
| 168 | pl.psty(0) |
| 169 | |
| 170 | pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 1) |
| 171 | |
| 172 | pl.col0(1) |
| 173 | pl.box("bcnst", 0, 0, "bcnstv", 0, 0) |
| 174 | pl.col0(2) |
| 175 | |
| 176 | --pl.cont(w, 1, nx, 1, ny, clevel, mypltr, {}) |
| 177 | pl.lab("distance", "altitude", "Bogon density") |
| 178 | |
| 179 | -- Plot using 1d coordinate transform |
| 180 | |
| 181 | -- Load colour palettes |
| 182 | pl.spal0("cmap0_black_on_white.pal"); |
| 183 | pl.spal1("cmap1_blue_yellow.pal",1); |
| 184 | |
| 185 | -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display |
| 186 | pl.scmap0n(3); |
| 187 | |
| 188 | pl.adv(0) |
| 189 | pl.vpor(0.1, 0.9, 0.1, 0.9) |
| 190 | pl.wind(-1, 1, -1, 1) |
| 191 | |
| 192 | pl.psty(0) |
| 193 | |
| 194 | pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 1, "pltr1", cgrid1) |
| 195 | |
| 196 | pl.col0(1) |
| 197 | pl.box("bcnst", 0, 0, "bcnstv", 0, 0) |
| 198 | pl.col0(2) |
| 199 | pl.lab("distance", "altitude", "Bogon density") |
| 200 | |
| 201 | -- Plot using 2d coordinate transform |
| 202 | |
| 203 | -- Load colour palettes |
| 204 | pl.spal0("cmap0_black_on_white.pal"); |
| 205 | pl.spal1("cmap1_blue_red.pal",1); |
| 206 | |
| 207 | -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display |
| 208 | pl.scmap0n(3); |
| 209 | |
| 210 | pl.adv(0) |
| 211 | pl.vpor(0.1, 0.9, 0.1, 0.9) |
| 212 | pl.wind(-1, 1, -1, 1) |
| 213 | |
| 214 | pl.psty(0) |
| 215 | |
| 216 | pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 0, "pltr2", cgrid2) |
| 217 | |
| 218 | pl.col0(1) |
| 219 | pl.box("bcnst", 0, 0, "bcnstv", 0, 0) |
| 220 | pl.col0(2) |
| 221 | pl.cont(w, 1, nx, 1, ny, clevel, "pltr2", cgrid2) |
| 222 | |
| 223 | pl.lab("distance", "altitude", "Bogon density, with streamlines") |
| 224 | |
| 225 | -- Plot using 2d coordinate transform |
| 226 | |
| 227 | -- Load colour palettes |
| 228 | pl.spal0(""); |
| 229 | pl.spal1("",1); |
| 230 | |
| 231 | -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display |
| 232 | pl.scmap0n(3); |
| 233 | |
| 234 | pl.adv(0) |
| 235 | pl.vpor(0.1, 0.9, 0.1, 0.9) |
| 236 | pl.wind(-1, 1, -1, 1) |
| 237 | |
| 238 | pl.psty(0) |
| 239 | |
| 240 | pl.shades(z, -1, 1, -1, 1, shedge, fill_width, 2, 3, 0, "pltr2", cgrid2) |
| 241 | |
| 242 | pl.col0(1) |
| 243 | pl.box("bcnst", 0, 0, "bcnstv", 0, 0) |
| 244 | pl.col0(2) |
| 245 | |
| 246 | pl.lab("distance", "altitude", "Bogon density") |
| 247 | |
| 248 | -- Note this exclusion API will probably change. |
| 249 | |
| 250 | -- Plot using 2d coordinate transform and exclusion |
| 251 | if exclude~=0 then |
| 252 | |
| 253 | -- Load colour palettes |
| 254 | pl.spal0("cmap0_black_on_white.pal"); |
| 255 | pl.spal1("cmap1_gray.pal",1); |
| 256 | |
| 257 | -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display |
| 258 | pl.scmap0n(3); |
| 259 | |
| 260 | pl.adv(0) |
| 261 | pl.vpor(0.1, 0.9, 0.1, 0.9) |
| 262 | pl.wind(-1, 1, -1, 1) |
| 263 | |
| 264 | plpsty(0) |
| 265 | |
| 266 | pl.shades(z, zdefined, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, |
| 267 | 0, "pltr2", cgrid2) |
| 268 | |
| 269 | pl.col0(1) |
| 270 | pl.box("bcnst", 0, 0, "bcnstv", 0, 0) |
| 271 | |
| 272 | pl.lab("distance", "altitude", "Bogon density with exclusion") |
| 273 | end |
| 274 | |
| 275 | -- Example with polar coordinates. |
| 276 | |
| 277 | -- Load colour palettes |
| 278 | pl.spal0("cmap0_black_on_white.pal"); |
| 279 | pl.spal1("cmap1_gray.pal",1); |
| 280 | |
| 281 | -- Reduce colors in cmap 0 so that cmap 1 is useful on a 16-color display |
| 282 | pl.scmap0n(3); |
| 283 | |
| 284 | pl.adv(0) |
| 285 | pl.vpor(.1, .9, .1, .9) |
| 286 | pl.wind(-1, 1, -1, 1) |
| 287 | |
| 288 | pl.psty(0) |
| 289 | |
| 290 | -- Build new coordinate matrices. |
| 291 | for i = 1, nx do |
| 292 | r = (i-1)/(nx-1) |
| 293 | for j = 1, ny do |
| 294 | t = 2*math.pi/(ny-1)*(j-1) |
| 295 | cgrid2["xg"][i][j] = r*math.cos(t) |
| 296 | cgrid2["yg"][i][j] = r*math.sin(t) |
| 297 | z[i][j] = math.exp(-r^2)*math.cos(5*math.pi*r)*math.cos(5*t) |
| 298 | end |
| 299 | end |
| 300 | |
| 301 | -- Need a new shedge to go along with the new data set. |
| 302 | zmin, zmax = f2mnmx(z, nx, ny) |
| 303 | |
| 304 | for i = 1, ns+1 do |
| 305 | shedge[i] = zmin + (zmax-zmin)*(i-1)/ns |
| 306 | end |
| 307 | |
| 308 | -- Now we can shade the interior region. |
| 309 | pl.shades(z, -1, 1, -1, 1, shedge, fill_width, cont_color, cont_width, 0, "pltr2", cgrid2) |
| 310 | |
| 311 | -- Now we can draw the perimeter. (If do before, shade stuff may overlap.) |
| 312 | for i = 1, PERIMETERPTS do |
| 313 | t = 2*math.pi/(PERIMETERPTS-1)*(i-1) |
| 314 | px[i] = math.cos(t) |
| 315 | py[i] = math.sin(t) |
| 316 | end |
| 317 | pl.col0(1) |
| 318 | pl.line(px, py) |
| 319 | |
| 320 | -- And label the plot. |
| 321 | pl.col0(2) |
| 322 | pl.lab( "", "", "Tokamak Bogon Instability" ) |
| 323 | |
| 324 | pl.plend() |
| 325 |
