1 //	plmtex3, plptex3 demo.
2 //
3 // Copyright (C) 2007, 2008, 2009  Alan W. Irwin
4 // Copyright (C) 2007  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; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // PLplot is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU Library General Public License for more details.
17 //
18 // You should have received a copy of the GNU Library General Public License
19 // along with PLplot; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
21 //--------------------------------------------------------------------------
22 
23 //--------------------------------------------------------------------------
24 // Implementation of PLplot example 28 in Java.
25 //--------------------------------------------------------------------------
26 
27 package plplot.examples;
28 
29 import plplot.core.*;
30 import static plplot.core.plplotjavacConstants.*;
31 
32 import java.lang.Math;
33 
34 class x28 {
35     PLStream   pls = new PLStream();
36 
37     static int XPTS        = 2;
main( String[] args )38     static int YPTS        = 2;
39     static int NREVOLUTION = 16;
40     static int NROTATION   = 8;
41     static int NSHEAR      = 8;
42 
43     x28( String[] args )
44     {
45         double xmin     = 0., xmax = 1.0, xmid = 0.5 * ( xmax + xmin ), xrange = xmax - xmin,
46                ymin     = 0., ymax = 1.0, ymid = 0.5 * ( ymax + ymin ), yrange = ymax - ymin,
47                zmin     = 0., zmax = 1.0, zmid = 0.5 * ( zmax + zmin ), zrange = zmax - zmin,
48                ysmin    = ymin + 0.1 * yrange,
x27( String[] args )49                ysmax    = ymax - 0.1 * yrange,
50                ysrange  = ysmax - ysmin,
51                dysrot   = ysrange / ( NROTATION - 1 ),
52                dysshear = ysrange / ( NSHEAR - 1 ),
53                zsmin    = zmin + 0.1 * zrange,
54                zsmax    = zmax - 0.1 * zrange,
55                zsrange  = zsmax - zsmin,
56                dzsrot   = zsrange / ( NROTATION - 1 ),
57                dzsshear = zsrange / ( NSHEAR - 1 ),
58                ys, zs,
59                x_inclination, y_inclination, z_inclination,
60                x_shear, y_shear, z_shear,
61                omega, sin_omega, cos_omega, domega;
62         int    i, j;
63         double radius, pitch, xpos, ypos, zpos;
64         // p1string must be exactly one character + the null termination
65         // character.
66         String pstring = "The future of our civilization depends on software freedom.";
67 
68         // Allocate and define the minimal x, y, and z to insure 3D box
69         double[] x   = new double[XPTS];
70         double[] y   = new double[YPTS];
71         double[][] z = new double[XPTS][YPTS];
72 
73         for ( i = 0; i < XPTS; i++ )
74         {
75             x[i] = xmin + i * ( xmax - xmin ) / ( XPTS - 1 );
76         }
77 
78         for ( j = 0; j < YPTS; j++ )
79             y[j] = ymin + j * ( ymax - ymin ) / ( YPTS - 1 );
80 
81         for ( i = 0; i < XPTS; i++ )
82         {
83             for ( j = 0; j < YPTS; j++ )
84             {
85                 z[i][j] = 0.;
86             }
87         }
88 
89         // Parse and process command line arguments
90 
91         pls.parseopts( args, PL_PARSE_FULL | PL_PARSE_NOPROGRAM );
92 
93         pls.init();
94 
95         // Page 1: Demonstrate inclination and shear capability pattern.
96 
97         pls.adv( 0 );
98         pls.vpor( -0.15, 1.15, -0.05, 1.05 );
99         pls.wind( -1.2, 1.2, -0.8, 1.5 );
100         pls.w3d( 1.0, 1.0, 1.0, xmin, xmax, ymin, ymax, zmin, zmax,
101             20., 45. );
102 
103         pls.col0( 2 );
104         pls.box3( "b", "", xmax - xmin, 0,
105             "b", "", ymax - ymin, 0,
106             "bcd", "", zmax - zmin, 0 );
107 
108         // z = zmin.
109         pls.schr( 0., 1.0 );
110         for ( i = 0; i < NREVOLUTION; i++ )
111         {
112             omega         = 2. * Math.PI * ( (double) i / (double) NREVOLUTION );
113             sin_omega     = Math.sin( omega );
114             cos_omega     = Math.cos( omega );
115             x_inclination = 0.5 * xrange * cos_omega;
116             y_inclination = 0.5 * yrange * sin_omega;
117             z_inclination = 0.;
118             x_shear       = -0.5 * xrange * sin_omega;
119             y_shear       = 0.5 * yrange * cos_omega;
120             z_shear       = 0.;
121             pls.ptex3(
122                 xmid, ymid, zmin,
123                 x_inclination, y_inclination, z_inclination,
124                 x_shear, y_shear, z_shear,
125                 0.0, "  revolution" );
gcd( int a, int b )126         }
127 
128         // x = xmax.
129         pls.schr( 0., 1.0 );
130         for ( i = 0; i < NREVOLUTION; i++ )
131         {
132             omega         = 2. * Math.PI * ( (double) i / (double) NREVOLUTION );
133             sin_omega     = Math.sin( omega );
134             cos_omega     = Math.cos( omega );
135             x_inclination = 0.;
136             y_inclination = -0.5 * yrange * cos_omega;
137             z_inclination = 0.5 * zrange * sin_omega;
138             x_shear       = 0.;
139             y_shear       = 0.5 * yrange * sin_omega;
140             z_shear       = 0.5 * zrange * cos_omega;
141             pls.ptex3(
cycloid()142                 xmax, ymid, zmid,
143                 x_inclination, y_inclination, z_inclination,
144                 x_shear, y_shear, z_shear,
145                 0.0, "  revolution" );
146         }
147 
148         // y = ymax.
spiro( double params[], boolean fill )149         pls.schr( 0., 1.0 );
150         for ( i = 0; i < NREVOLUTION; i++ )
151         {
152             omega         = 2. * Math.PI * ( (double) i / (double) NREVOLUTION );
153             sin_omega     = Math.sin( omega );
154             cos_omega     = Math.cos( omega );
155             x_inclination = 0.5 * xrange * cos_omega;
156             y_inclination = 0.;
157             z_inclination = 0.5 * zrange * sin_omega;
158             x_shear       = -0.5 * xrange * sin_omega;
159             y_shear       = 0.;
160             z_shear       = 0.5 * zrange * cos_omega;
161             pls.ptex3(
162                 xmid, ymax, zmid,
163                 x_inclination, y_inclination, z_inclination,
164                 x_shear, y_shear, z_shear,
165                 0.0, "  revolution" );
166         }
167         // Draw minimal 3D grid to finish defining the 3D box.
168         pls.mesh( x, y, z, DRAW_LINEXY );
169 
170         // Page 2: Demonstrate rotation of string around its axis.
171         pls.adv( 0 );
172         pls.vpor( -0.15, 1.15, -0.05, 1.05 );
173         pls.wind( -1.2, 1.2, -0.8, 1.5 );
174         pls.w3d( 1.0, 1.0, 1.0, xmin, xmax, ymin, ymax, zmin, zmax,
175             20., 45. );
176 
177         pls.col0( 2 );
178         pls.box3( "b", "", xmax - xmin, 0,
179             "b", "", ymax - ymin, 0,
180             "bcd", "", zmax - zmin, 0 );
181 
182         // y = ymax.
183         pls.schr( 0., 1.0 );
184         x_inclination = 1.;
185         y_inclination = 0.;
186         z_inclination = 0.;
187         x_shear       = 0.;
188         for ( i = 0; i < NROTATION; i++ )
189         {
190             omega     = 2. * Math.PI * ( (double) i / (double) NROTATION );
191             sin_omega = Math.sin( omega );
192             cos_omega = Math.cos( omega );
193             y_shear   = 0.5 * yrange * sin_omega;
194             z_shear   = 0.5 * zrange * cos_omega;
195             zs        = zsmax - dzsrot * i;
196             pls.ptex3(
197                 xmid, ymax, zs,
198                 x_inclination, y_inclination, z_inclination,
199                 x_shear, y_shear, z_shear,
200                 0.5, "rotation for y = y#dmax#u" );
201         }
202 
203         // x = xmax.
204         pls.schr( 0., 1.0 );
205         x_inclination = 0.;
206         y_inclination = -1.;
207         z_inclination = 0.;
208         y_shear       = 0.;
209         for ( i = 0; i < NROTATION; i++ )
210         {
211             omega     = 2. * Math.PI * ( (double) i / (double) NROTATION );
212             sin_omega = Math.sin( omega );
213             cos_omega = Math.cos( omega );
214             x_shear   = 0.5 * xrange * sin_omega;
215             z_shear   = 0.5 * zrange * cos_omega;
216             zs        = zsmax - dzsrot * i;
217             pls.ptex3(
218                 xmax, ymid, zs,
arcs()219                 x_inclination, y_inclination, z_inclination,
220                 x_shear, y_shear, z_shear,
221                 0.5, "rotation for x = x#dmax#u" );
222         }
223 
224         // z = zmin.
225         pls.schr( 0., 1.0 );
226         x_inclination = 1.;
227         y_inclination = 0.;
228         z_inclination = 0.;
229         x_shear       = 0.;
230         for ( i = 0; i < NROTATION; i++ )
231         {
232             omega     = 2. * Math.PI * ( (double) i / (double) NROTATION );
233             sin_omega = Math.sin( omega );
234             cos_omega = Math.cos( omega );
235             y_shear   = 0.5 * yrange * cos_omega;
236             z_shear   = 0.5 * zrange * sin_omega;
237             ys        = ysmax - dysrot * i;
238             pls.ptex3(
239                 xmid, ys, zmin,
240                 x_inclination, y_inclination, z_inclination,
241                 x_shear, y_shear, z_shear,
242                 0.5, "rotation for z = z#dmin#u" );
243         }
244         // Draw minimal 3D grid to finish defining the 3D box.
245         pls.mesh( x, y, z, DRAW_LINEXY );
246 
247         // Page 3: Demonstrate shear of string along its axis.
248         // Work around xcairo and pngcairo (but not pscairo) problems for
249         // shear vector too close to axis of string. (N.B. no workaround
250         // would be domega = 0.)
251         domega = 0.05;
252         pls.adv( 0 );
253         pls.vpor( -0.15, 1.15, -0.05, 1.05 );
254         pls.wind( -1.2, 1.2, -0.8, 1.5 );
255         pls.w3d( 1.0, 1.0, 1.0, xmin, xmax, ymin, ymax, zmin, zmax,
256             20., 45. );
257 
258         pls.col0( 2 );
259         pls.box3( "b", "", xmax - xmin, 0,
260             "b", "", ymax - ymin, 0,
261             "bcd", "", zmax - zmin, 0 );
262 
263         // y = ymax.
264         pls.schr( 0., 1.0 );
265         x_inclination = 1.;
266         y_inclination = 0.;
267         z_inclination = 0.;
268         y_shear       = 0.;
269         for ( i = 0; i < NSHEAR; i++ )
270         {
271             omega     = domega + 2. * Math.PI * ( (double) i / (double) NSHEAR );
272             sin_omega = Math.sin( omega );
273             cos_omega = Math.cos( omega );
274             x_shear   = 0.5 * xrange * sin_omega;
275             z_shear   = 0.5 * zrange * cos_omega;
276             zs        = zsmax - dzsshear * i;
277             pls.ptex3(
278                 xmid, ymax, zs,
279                 x_inclination, y_inclination, z_inclination,
280                 x_shear, y_shear, z_shear,
281                 0.5, "shear for y = y#dmax#u" );
282         }
283 
284         // x = xmax.
285         pls.schr( 0., 1.0 );
286         x_inclination = 0.;
287         y_inclination = -1.;
288         z_inclination = 0.;
289         x_shear       = 0.;
290         for ( i = 0; i < NSHEAR; i++ )
291         {
292             omega     = domega + 2. * Math.PI * ( (double) i / (double) NSHEAR );
293             sin_omega = Math.sin( omega );
294             cos_omega = Math.cos( omega );
295             y_shear   = -0.5 * yrange * sin_omega;
296             z_shear   = 0.5 * zrange * cos_omega;
297             zs        = zsmax - dzsshear * i;
298             pls.ptex3(
299                 xmax, ymid, zs,
300                 x_inclination, y_inclination, z_inclination,
301                 x_shear, y_shear, z_shear,
302                 0.5, "shear for x = x#dmax#u" );
303         }
304 
305         // z = zmin.
306         pls.schr( 0., 1.0 );
307         x_inclination = 1.;
308         y_inclination = 0.;
309         z_inclination = 0.;
310         z_shear       = 0.;
311         for ( i = 0; i < NSHEAR; i++ )
312         {
313             omega     = domega + 2. * Math.PI * ( (double) i / (double) NSHEAR );
314             sin_omega = Math.sin( omega );
315             cos_omega = Math.cos( omega );
316             y_shear   = 0.5 * yrange * cos_omega;
317             x_shear   = 0.5 * xrange * sin_omega;
318             ys        = ysmax - dysshear * i;
319             pls.ptex3(
320                 xmid, ys, zmin,
321                 x_inclination, y_inclination, z_inclination,
322                 x_shear, y_shear, z_shear,
323                 0.5, "shear for z = z#dmin#u" );
324         }
325         // Draw minimal 3D grid to finish defining the 3D box.
326         pls.mesh( x, y, z, DRAW_LINEXY );
327 
328         // Page 4: Demonstrate drawing a string on a 3D path.
329         pls.adv( 0 );
330         pls.vpor( -0.15, 1.15, -0.05, 1.05 );
331         pls.wind( -1.2, 1.2, -0.8, 1.5 );
332         pls.w3d( 1.0, 1.0, 1.0, xmin, xmax, ymin, ymax, zmin, zmax,
333             40., -30. );
334 
335         pls.col0( 2 );
336         pls.box3( "b", "", xmax - xmin, 0,
337             "b", "", ymax - ymin, 0,
338             "bcd", "", zmax - zmin, 0 );
339 
340         pls.schr( 0., 1.2 );
341         // domega controls the spacing between the various characters of the
342         // string and also the maximum value of omega for the given number
343         // of characters in *pstring.
344         domega = 2. * Math.PI / pstring.length();
345         omega  = 0.;
346         // 3D function is a helix of the given radius and pitch
347         radius = 0.5;
348         pitch  = 1. / ( 2. * Math.PI );
349         for ( i = 0; i < pstring.length(); i++ )
350         {
351             sin_omega = Math.sin( omega );
352             cos_omega = Math.cos( omega );
353             xpos      = xmid + radius * sin_omega;
354             ypos      = ymid - radius * cos_omega;
355             zpos      = zmin + pitch * omega;
356             // In general, the inclination is proportional to the derivative of
357             // the position wrt theta.
358             x_inclination = radius * cos_omega;;
359             y_inclination = radius * sin_omega;
360             z_inclination = pitch;
361             // The shear vector should be perpendicular to the 3D line with Z
362             // component maximized, but for low pitch a good approximation is
363             // a constant vector that is parallel to the Z axis.
364             x_shear = 0.;
365             y_shear = 0.;
366             z_shear = 1.;
367             pls.ptex3(
368                 xpos, ypos, zpos,
369                 x_inclination, y_inclination, z_inclination,
370                 x_shear, y_shear, z_shear,
371                 0.5, pstring.substring( i, i + 1 ) );
372             omega += domega;
373         }
374         // Draw minimal 3D grid to finish defining the 3D box.
375         pls.mesh( x, y, z, DRAW_LINEXY );
376 
377         // Page 5: Demonstrate plmtex3 axis labelling capability
378         pls.adv( 0 );
379         pls.vpor( -0.15, 1.15, -0.05, 1.05 );
380         pls.wind( -1.2, 1.2, -0.8, 1.5 );
381         pls.w3d( 1.0, 1.0, 1.0, xmin, xmax, ymin, ymax, zmin, zmax,
382             20., 45. );
383 
384         pls.col0( 2 );
385         pls.box3( "b", "", xmax - xmin, 0,
386             "b", "", ymax - ymin, 0,
387             "bcd", "", zmax - zmin, 0 );
388 
389         pls.schr( 0., 1.0 );
390         pls.mtex3( "xp", 3.0, 0.5, 0.5, "Arbitrarily displaced" );
391         pls.mtex3( "xp", 4.5, 0.5, 0.5, "primary X-axis label" );
392         pls.mtex3( "xs", -2.5, 0.5, 0.5, "Arbitrarily displaced" );
393         pls.mtex3( "xs", -1.0, 0.5, 0.5, "secondary X-axis label" );
394         pls.mtex3( "yp", 3.0, 0.5, 0.5, "Arbitrarily displaced" );
395         pls.mtex3( "yp", 4.5, 0.5, 0.5, "primary Y-axis label" );
396         pls.mtex3( "ys", -2.5, 0.5, 0.5, "Arbitrarily displaced" );
397         pls.mtex3( "ys", -1.0, 0.5, 0.5, "secondary Y-axis label" );
398         pls.mtex3( "zp", 4.5, 0.5, 0.5, "Arbitrarily displaced" );
399         pls.mtex3( "zp", 3.0, 0.5, 0.5, "primary Z-axis label" );
400         pls.mtex3( "zs", -2.5, 0.5, 0.5, "Arbitrarily displaced" );
401         pls.mtex3( "zs", -1.0, 0.5, 0.5, "secondary Z-axis label" );
402         // Draw minimal 3D grid to finish defining the 3D box.
403         pls.mesh( x, y, z, DRAW_LINEXY );
404 
405         pls.end();
406     }
407 
408 
409     public static void main( String[] args )
410     {
411         new x28( args );
412     }
413 };
414