1 //--------------------------------------------------------------------------
2 // Copyright (C) 2001  Geoffrey Furnish
3 // Copyright (C) 2002-2014 Alan W. Irwin
4 // Copyright (C) 2004  Andrew Ross
5 //
6 // This file is part of PLplot.
7 //
8 // PLplot is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU Library General Public License as published by
10 // the Free Software Foundation; version 2 of the License.
11 //
12 // PLplot is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 // GNU Library General Public License for more details.
16 //
17 // You should have received a copy of the GNU Library General Public License
18 // along with PLplot; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
20 //--------------------------------------------------------------------------
21 
22 //--------------------------------------------------------------------------
23 // Implementation of PLplot example 16 in Java.
24 //--------------------------------------------------------------------------
25 
26 package plplot.examples;
27 
28 import plplot.core.*;
29 import static plplot.core.plplotjavacConstants.*;
30 
31 import java.lang.Math;
32 
33 class x16 {
34     // number of shade levels, x data points, y data points, and perimeter points.
35     static final int NSHADES      = 20;
36     static final int XPTS         = 35;
37     static final int YPTS         = 46;
38     static final int PERIMETERPTS = 100;
39     static final int NUM_AXES     = 1;
40     static final int NUM_LABELS   = 1;
41 
42     // calculated constants and array that depends on them
43     static final double XSPA = 2. / ( XPTS - 1 );
44     static final double YSPA = 2. / ( YPTS - 1 );
45     final double        tr[] = { XSPA, 0.0, -1.0, 0.0, YSPA, -1.0 };
46 
47     PLStream            pls = new PLStream();
48 
49     double fmin, fmax;
50 
51     // Compute min and max value of a 2-d array.
52 
f2mnmx( double[][] f, int nx, int ny )53     void f2mnmx( double[][] f, int nx, int ny )
54     {
55         fmax = f[0][0];
56         fmin = fmax;
57 
58         for ( int i = 0; i < nx; i++ )
59             for ( int j = 0; j < ny; j++ )
60             {
61                 if ( f[i][j] < fmin ) fmin = f[i][j];
62                 if ( f[i][j] > fmax ) fmax = f[i][j];
63             }
64     }
65 
66     // Does a variety of shade plots with continuous (cmap1) colours
67 
main( String[] args )68     public static void main( String[] args )
69     {
70         new x16( args );
71     }
72 
x16( String[] args )73     public x16( String[] args )
74     {
75         int    i, j;
76         double x, y, argx, argy, distort, r, t, zmin, zmax;
77         double[] px     = new double[PERIMETERPTS];
78         double[] py     = new double[PERIMETERPTS];
79         double[][] xg0  = new double[XPTS][YPTS];
80         double[][] yg0  = new double[XPTS][YPTS];
81         double[][] xg1  = new double[XPTS][YPTS];
82         double[][] yg1  = new double[XPTS][YPTS];
83         double[][] z    = new double[XPTS][YPTS];
84         double[][] w    = new double[XPTS][YPTS];
85         double[][] xg2  = new double[XPTS][YPTS];
86         double[][] yg2  = new double[XPTS][YPTS];
87         double[] clevel = new double[NSHADES];
88         double[] shedge = new double[NSHADES + 1];
89         final int    cont_color = 0;
90         final double fill_width = 2., cont_width = 0.;
91         double[] colorbar_width  = new double[1];
92         double[] colorbar_height = new double[1];
93 
94         int[] num_values  = new int[NUM_AXES];
95         double[][] values = new double[NUM_AXES][NSHADES + 1];
96         String axis_opts[]     = { "bcvtm" };
97         double axis_ticks[]    = { 0.0 };
98         int    axis_subticks[] = { 0 };
99 
100         int    label_opts[] = { PL_COLORBAR_LABEL_BOTTOM };
101         String labels[]     = { "Magnitude" };
102 
103         // Parse and process command line arguments.
104         pls.parseopts( args, PL_PARSE_FULL | PL_PARSE_NOPROGRAM );
105 
106         // Load colour palettes
107         pls.spal0( "cmap0_black_on_white.pal" );
108         pls.spal1( "cmap1_gray.pal", true );
109         // Reduce colors in cmap 0 so that cmap 1 is useful on a
110         //16-color display
111         pls.scmap0n( 3 );
112 
113         //Initialize plplot
114 
115         pls.init();
116         // Set up data array
117 
118         for ( i = 0; i < XPTS; i++ )
119         {
120             x = (double) ( i - ( XPTS / 2 ) ) / (double) ( XPTS / 2 );
121             for ( j = 0; j < YPTS; j++ )
122             {
123                 y = (double) ( j - ( YPTS / 2 ) ) / (double) ( YPTS / 2 ) - 1.0;
124 
125                 z[i][j] = -Math.sin( 7. * x ) * Math.cos( 7. * y ) + x * x - y * y;
126                 w[i][j] = -Math.cos( 7. * x ) * Math.sin( 7. * y ) + 2 * x * y;
127             }
128         }
129 
130         f2mnmx( z, XPTS, YPTS );
131         zmin = fmin;
132         zmax = fmax;
133 
134         for ( i = 0; i < NSHADES; i++ )
135             clevel[i] = zmin + ( zmax - zmin ) * ( i + 0.5 ) / NSHADES;
136 
137         for ( i = 0; i < NSHADES + 1; i++ )
138             shedge[i] = zmin + ( zmax - zmin ) * i / NSHADES;
139 
140         for ( i = 0; i < XPTS; i++ )
141         {
142             for ( j = 0; j < YPTS; j++ )
143             {
144                 // Replacement for mypltr of x16c.c
145                 x = tr[0] * i + tr[1] * j + tr[2];
146                 y = tr[3] * i + tr[4] * j + tr[5];
147 
148                 argx    = x * Math.PI / 2;
149                 argy    = y * Math.PI / 2;
150                 distort = 0.4;
151 
152                 // Note xg0 ==> yg1 are one-dimensional because of arrangement of
153                 //zeros in the final tr definition above.  However, for now
154                 //we are using raw interface here so must nominally treat them
155                 //as two-dimensional.
156                 xg0[i][j] = x;
157                 yg0[i][j] = y;
158 
159                 xg1[i][j] = x + distort * Math.cos( argx );
160                 yg1[i][j] = y - distort * Math.cos( argy );
161 
162                 xg2[i][j] = x + distort * Math.cos( argx ) * Math.cos( argy );
163                 yg2[i][j] = y - distort * Math.cos( argx ) * Math.cos( argy );
164             }
165         }
166 
167         // Plot using identity transform
168 
169         pls.adv( 0 );
170         pls.vpor( 0.1, 0.9, 0.1, 0.9 );
171         pls.wind( -1.0, 1.0, -1.0, 1.0 );
172 
173         pls.psty( 0 );
174 
175         pls.shades( z, -1., 1., -1., 1.,
176             shedge, fill_width,
177             cont_color, cont_width,
178             true, xg0, yg0 );
179 
180         // Smaller text
181         pls.schr( 0.0, 0.75 );
182         // Small ticks on the vertical axis
183         pls.smaj( 0.0, 0.5 );
184         pls.smin( 0.0, 0.5 );
185 
186         num_values[0] = NSHADES + 1;
187         for ( i = 0; i < NSHADES + 1; i++ )
188         {
189             values[0][i] = shedge[i];
190         }
191         pls.colorbar( colorbar_width, colorbar_height,
192             PL_COLORBAR_SHADE | PL_COLORBAR_SHADE_LABEL, 0,
193             0.005, 0.0, 0.0375, 0.875, 0, 1, 1, 0.0, 0.0,
194             cont_color, cont_width,
195             label_opts, labels,
196             axis_opts,
197             axis_ticks, axis_subticks,
198             num_values, values );
199 
200         // Reset text and tick sizes
201         pls.schr( 0.0, 1.0 );
202         pls.smaj( 0.0, 1.0 );
203         pls.smin( 0.0, 1.0 );
204 
205         pls.col0( 1 );
206         pls.box( "bcnst", 0.0, 0, "bcnstv", 0.0, 0 );
207         pls.col0( 2 );
208         pls.lab( "distance", "altitude", "Bogon density" );
209 
210         // Plot using 1d coordinate transform
211 
212         // Load colour palettes
213         pls.spal0( "cmap0_black_on_white.pal" );
214         pls.spal1( "cmap1_blue_yellow.pal", true );
215         // Reduce colors in cmap 0 so that cmap 1 is useful on a
216         //16-color display
217         pls.scmap0n( 3 );
218 
219         pls.adv( 0 );
220         pls.vpor( 0.1, 0.9, 0.1, 0.9 );
221         pls.wind( -1.0, 1.0, -1.0, 1.0 );
222 
223         pls.psty( 0 );
224 
225         pls.shades( z, -1., 1., -1., 1.,
226             shedge, fill_width,
227             cont_color, cont_width,
228             true, xg1, yg1 );
229 
230         // Smaller text
231         pls.schr( 0.0, 0.75 );
232         // Small ticks on the vertical axis
233         pls.smaj( 0.0, 0.5 );
234         pls.smin( 0.0, 0.5 );
235 
236         num_values[0] = NSHADES + 1;
237         for ( i = 0; i < NSHADES + 1; i++ )
238         {
239             values[0][i] = shedge[i];
240         }
241         pls.colorbar( colorbar_width, colorbar_height,
242             PL_COLORBAR_SHADE | PL_COLORBAR_SHADE_LABEL, 0,
243             0.005, 0.0, 0.0375, 0.875, 0, 1, 1, 0.0, 0.0,
244             cont_color, cont_width,
245             label_opts, labels,
246             axis_opts,
247             axis_ticks, axis_subticks,
248             num_values, values );
249 
250         // Reset text and tick sizes
251         pls.schr( 0.0, 1.0 );
252         pls.smaj( 0.0, 1.0 );
253         pls.smin( 0.0, 1.0 );
254 
255         pls.col0( 1 );
256         pls.box( "bcnst", 0.0, 0, "bcnstv", 0.0, 0 );
257         pls.col0( 2 );
258 
259         pls.lab( "distance", "altitude", "Bogon density" );
260 
261         // Plot using 2d coordinate transform
262 
263         // Load colour palettes
264         pls.spal0( "cmap0_black_on_white.pal" );
265         pls.spal1( "cmap1_blue_red.pal", true );
266         // Reduce colors in cmap 0 so that cmap 1 is useful on a
267         //16-color display
268         pls.scmap0n( 3 );
269 
270         pls.adv( 0 );
271         pls.vpor( 0.1, 0.9, 0.1, 0.9 );
272         pls.wind( -1.0, 1.0, -1.0, 1.0 );
273 
274         pls.psty( 0 );
275 
276         pls.shades( z, -1., 1., -1., 1.,
277             shedge, fill_width,
278             cont_color, cont_width,
279             false, xg2, yg2 );
280 
281         // Smaller text
282         pls.schr( 0.0, 0.75 );
283         // Small ticks on the vertical axis
284         pls.smaj( 0.0, 0.5 );
285         pls.smin( 0.0, 0.5 );
286 
287         num_values[0] = NSHADES + 1;
288         for ( i = 0; i < NSHADES + 1; i++ )
289         {
290             values[0][i] = shedge[i];
291         }
292         pls.colorbar( colorbar_width, colorbar_height,
293             PL_COLORBAR_SHADE | PL_COLORBAR_SHADE_LABEL, 0,
294             0.005, 0.0, 0.0375, 0.875, 0, 1, 1, 0.0, 0.0,
295             cont_color, cont_width,
296             label_opts, labels,
297             axis_opts,
298             axis_ticks, axis_subticks,
299             num_values, values );
300 
301         // Reset text and tick sizes
302         pls.schr( 0.0, 1.0 );
303         pls.smaj( 0.0, 1.0 );
304         pls.smin( 0.0, 1.0 );
305 
306         pls.col0( 1 );
307         pls.box( "bcnst", 0.0, 0, "bcnstv", 0.0, 0 );
308         pls.col0( 2 );
309         pls.cont( w, 1, XPTS, 1, YPTS, clevel, xg2, yg2 );
310 
311         pls.lab( "distance", "altitude", "Bogon density, with streamlines" );
312 
313         // Plot using 2d coordinate transform
314 
315         // Load colour palettes
316         pls.spal0( "" );
317         pls.spal1( "", true );
318         // Reduce colors in cmap 0 so that cmap 1 is useful on a
319         //16-color display
320         pls.scmap0n( 3 );
321 
322         pls.adv( 0 );
323         pls.vpor( 0.1, 0.9, 0.1, 0.9 );
324         pls.wind( -1.0, 1.0, -1.0, 1.0 );
325 
326         pls.psty( 0 );
327 
328         pls.shades( z, -1., 1., -1., 1.,
329             shedge, fill_width,
330             2, 3.,
331             false, xg2, yg2 );
332 
333         // Smaller text
334         pls.schr( 0.0, 0.75 );
335         // Small ticks on the vertical axis
336         pls.smaj( 0.0, 0.5 );
337         pls.smin( 0.0, 0.5 );
338 
339         num_values[0] = NSHADES + 1;
340         for ( i = 0; i < NSHADES + 1; i++ )
341         {
342             values[0][i] = shedge[i];
343         }
344         pls.colorbar( colorbar_width, colorbar_height,
345             PL_COLORBAR_SHADE | PL_COLORBAR_SHADE_LABEL, 0,
346             0.005, 0.0, 0.0375, 0.875, 0, 1, 1, 0.0, 0.0,
347             2, 3.0,
348             label_opts, labels,
349             axis_opts,
350             axis_ticks, axis_subticks,
351             num_values, values );
352 
353         // Reset text and tick sizes
354         pls.schr( 0.0, 1.0 );
355         pls.smaj( 0.0, 1.0 );
356         pls.smin( 0.0, 1.0 );
357 
358         pls.col0( 1 );
359         pls.box( "bcnst", 0.0, 0, "bcnstv", 0.0, 0 );
360         pls.col0( 2 );
361 
362         pls.lab( "distance", "altitude", "Bogon density" );
363 
364         // Note this exclusion API will probably change so don't bother
365         // with it for x16.java example.
366 
367 
368         //Example with polar coordinates.
369 
370         // Load colour palettes
371         pls.spal0( "cmap0_black_on_white.pal" );
372         pls.spal1( "cmap1_gray.pal", true );
373         // Reduce colors in cmap 0 so that cmap 1 is useful on a
374         //16-color display
375         pls.scmap0n( 3 );
376 
377         pls.adv( 0 );
378         pls.vpor( .1, .9, .1, .9 );
379         pls.wind( -1., 1., -1., 1. );
380 
381         pls.psty( 0 );
382 
383         // Build new coordinate matrices.
384 
385         for ( i = 0; i < XPTS; i++ )
386         {
387             r = ( (double) i ) / ( XPTS - 1 );
388             for ( j = 0; j < YPTS; j++ )
389             {
390                 t         = ( 2. * Math.PI / ( YPTS - 1. ) ) * j;
391                 xg2[i][j] = r * Math.cos( t );
392                 yg2[i][j] = r * Math.sin( t );
393                 z[i][j]   = Math.exp( -r * r ) * Math.cos( 5. * Math.PI * r ) * Math.cos( 5. * t );
394             }
395         }
396 
397         //Need a new shedge to go along with the new data set.
398 
399         f2mnmx( z, XPTS, YPTS );
400         zmin = fmin;
401         zmax = fmax;
402 
403         for ( i = 0; i < NSHADES + 1; i++ )
404             shedge[i] = zmin + ( zmax - zmin ) * i / NSHADES;
405 
406         pls.shades( z, -1., 1., -1., 1.,
407             shedge, fill_width,
408             cont_color, cont_width,
409             false, xg2, yg2 );
410 
411         // Smaller text
412         pls.schr( 0.0, 0.75 );
413         // Small ticks on the vertical axis
414         pls.smaj( 0.0, 0.5 );
415         pls.smin( 0.0, 0.5 );
416 
417         num_values[0] = NSHADES + 1;
418         for ( i = 0; i < NSHADES + 1; i++ )
419         {
420             values[0][i] = shedge[i];
421         }
422         pls.colorbar( colorbar_width, colorbar_height,
423             PL_COLORBAR_SHADE | PL_COLORBAR_SHADE_LABEL, 0,
424             0.005, 0.0, 0.0375, 0.875, 0, 1, 1, 0.0, 0.0,
425             cont_color, cont_width,
426             label_opts, labels,
427             axis_opts,
428             axis_ticks, axis_subticks,
429             num_values, values );
430 
431         // Reset text and tick sizes
432         pls.schr( 0.0, 1.0 );
433         pls.smaj( 0.0, 1.0 );
434         pls.smin( 0.0, 1.0 );
435 
436 // Now we can draw the perimeter.  (If do before, shade stuff may overlap.)
437         for ( i = 0; i < PERIMETERPTS; i++ )
438         {
439             t     = ( 2. * Math.PI / ( PERIMETERPTS - 1 ) ) * i;
440             px[i] = Math.cos( t );
441             py[i] = Math.sin( t );
442         }
443         pls.col0( 1 );
444         pls.line( px, py );
445 
446         // And label the plot.
447 
448         pls.col0( 2 );
449         pls.lab( "", "", "Tokamak Bogon Instability" );
450 
451         // Clean up
452 
453         pls.end();
454     }
455 }
456 
457 //--------------------------------------------------------------------------
458 //                              End of x16.java
459 //--------------------------------------------------------------------------
460 
461