1 // Copyright (C) 2004 Joao Cardoso
2 //
3 // This file is part of PLplot.
4 //
5 // PLplot is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU Library General Public License as published
7 // by the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // PLplot is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU Library General Public License for more details.
14 //
15 // You should have received a copy of the GNU Library General Public License
16 // along with PLplot; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 //
19
20 #include "pltk.h"
21 #include <math.h>
22 #include <string.h>
23
24 static int
25 AppInit( Tcl_Interp *interp );
26
27 int stuff( tclMatrix *pm, Tcl_Interp *interp, int argc, const char *argv[] );
28
29 //--------------------------------------------------------------------------
30 // A pithy little proc to show off how to install and use a tclMatrix
31 // extension subcommand. This example is silly--only intended to show
32 // how to do it. What to do with it is your problem. Could implement
33 // subcommands for filling a matrix with special functions, performing
34 // fft's, etc.
35 //--------------------------------------------------------------------------
36
37 int stuff( tclMatrix *pm, Tcl_Interp *interp,
38 int PL_UNUSED( argc ), const char * PL_UNUSED( argv [] ) )
main(int argc,const char ** argv)39 {
40 int i;
41 PLFLT x, y;
42
43 // Should check that matrix is right type, size, etc.
44
45 for ( i = 0; i < pm->n[0]; i++ )
46 {
47 x = (PLFLT) i / pm->n[0];
48 y = sin( 6.28 * 4. * i / pm->n[0] ) * x * ( 1. - x ) * 2 +
49 2. * x * ( 1. - x );
50 pm->fdata[i] = y;
51 }
52
53 Tcl_SetResult( interp, (char *) "Things are cool in gumbyville.", TCL_STATIC );
54 return TCL_OK;
55 }
56
57
58 //--------------------------------------------------------------------------
59 // main --
60 //
61 // Just a stub routine to call pltkMain. The latter is nice to have
62 // when building extended wishes, since then you don't have to rely on
63 // sucking the Tk main out of libtk (which doesn't work correctly on all
64 // systems/compilers/linkers/etc). Hopefully in the future Tk will
65 // supply a sufficiently capable tkMain() type function that can be used
66 // instead.
67 //--------------------------------------------------------------------------
68
69 int
70 main( int argc, const char **argv )
71 {
72 exit( pltkMain( argc, argv, NULL, AppInit ) );
73 }
74
75 //
76 //--------------------------------------------------------------------------
77 //
78 // AppInit --
79 //
80 // This procedure performs application-specific initialization.
81 // Most applications, especially those that incorporate additional
82 // packages, will have their own version of this procedure.
83 //
84 // Results:
85 // Returns a standard Tcl completion code, and leaves an error
86 // message in interp->result if an error occurs.
AppInit(Tcl_Interp * interp)87 //
88 // Side effects:
89 // Depends on the startup script.
90 //
91 // Taken from tkAppInit.c --
92 //
93 // Copyright (c) 1993 The Regents of the University of California.
94 // All rights reserved.
95 //
96 // Permission is hereby granted, without written agreement and without
97 // license or royalty fees, to use, copy, modify, and distribute this
98 // software and its documentation for any purpose, provided that the
99 // above copyright notice and the following two paragraphs appear in
100 // all copies of this software.
101 //
102 // IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
103 // DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
104 // OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
105 // CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
106 //
107 // THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
108 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
109 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
110 // ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
111 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
112 //--------------------------------------------------------------------------
113 //
114
115 int myplotCmd( ClientData, Tcl_Interp *, int, char ** );
116
117 static int
118 AppInit( Tcl_Interp *interp )
119 {
120 Tk_Window mainWin;
121
122 mainWin = Tk_MainWindow( interp );
123
124 //
125 // Call the init procedures for included packages. Each call should
126 // look like this:
127 //
128 // if (Mod_Init(interp) == TCL_ERROR) {
129 // return TCL_ERROR;
130 // }
131 //
132 // where "Mod" is the name of the module.
133 //
134
135 if ( Pltk_Init( interp ) == TCL_ERROR )
136 {
137 return TCL_ERROR;
138 }
myplot1()139
140 //
141 // Call Tcl_CreateCommand for application-specific commands, if
142 // they weren't already created by the init procedures called above.
143 //
144
145 Tcl_CreateCommand( interp, "myplot", (Tcl_CmdProc *) myplotCmd,
146 (ClientData) mainWin, (Tcl_CmdDeleteProc *) NULL );
147
148 Tcl_MatrixInstallXtnsn( "stuff", stuff );
149
150 return TCL_OK;
151 }
152
153 void myplot1( void );
myplot2()154 void myplot2( void );
155 void myplot3( void );
156 void myplot4( void );
157
158 // Plots several simple functions
159 // Note the compiler should automatically convert all non-pointer arguments
160 // to satisfy the prototype, but some have problems with constants.
161
162 static PLFLT x[101], y[101];
163 static PLFLT xscale, yscale, xoff, yoff, xs[6], ys[6];
164 static PLINT space0 = 0, mark0 = 0, space1 = 1500, mark1 = 1500;
165
166 void plot1( void );
167 void plot2( void );
168 void plot3( void );
169
170 // This has been superceeded by Tcl code in tk02
myplot3()171
172 void myplot1()
173 {
174 // Set up the data
175 // Original case
176
177 xscale = 6.;
178 yscale = 1.;
179 xoff = 0.;
180 yoff = 0.;
181
182 // Do a plot
183
184 plot1();
185 }
186
187 void myplot2()
188 {
189 PLINT digmax;
190
191 // Set up the data
192
193 xscale = 1.;
194 yscale = 0.0014;
195 yoff = 0.0185;
196
197 // Do a plot
198
199 digmax = 5;
200 plsyax( digmax, 0 );
201 plot1();
202 }
203
204 void myplot3()
205 {
206 plot2();
207 }
208
209 void myplot4()
210 {
211 plot3();
212 }
213
214 //--------------------------------------------------------------------------
215
216 void
217 plot1( void )
218 {
219 int i;
220 PLFLT xmin, xmax, ymin, ymax;
221
222 for ( i = 0; i < 60; i++ )
223 {
224 x[i] = xoff + xscale * ( i + 1 ) / 60.0;
225 y[i] = yoff + yscale * pow( x[i], 2. );
226 }
227
228 xmin = x[0];
229 xmax = x[59];
plot2(void)230 ymin = y[0];
231 ymax = y[59];
232
233 for ( i = 0; i < 6; i++ )
234 {
235 xs[i] = x[i * 10 + 3];
236 ys[i] = y[i * 10 + 3];
237 }
238
239 // Set up the viewport and window using PLENV. The range in X is
240 // 0.0 to 6.0, and the range in Y is 0.0 to 30.0. The axes are
241 // scaled separately (just = 0), and we just draw a labelled
242 // box (axis = 0).
243
244 plcol0( 1 );
245 plenv( xmin, xmax, ymin, ymax, 0, 0 );
246 plcol0( 6 );
247 pllab( "(x)", "(y)", "#frPLplot Example 1 - y=x#u2" );
248
249 // Plot the data points
250
251 plcol0( 9 );
252 plpoin( 6, xs, ys, 9 );
253
254 // Draw the line through the data
255
256 plcol0( 4 );
257 plline( 60, x, y );
258 }
259
260 //--------------------------------------------------------------------------
261
plot3(void)262 void
263 plot2( void )
264 {
265 int i;
266
267 // Set up the viewport and window using PLENV. The range in X is -2.0 to
268 // 10.0, and the range in Y is -0.4 to 2.0. The axes are scaled separately
269 // (just = 0), and we draw a box with axes (axis = 1).
270
271 plcol0( 1 );
272 plenv( -2.0, 10.0, -0.4, 1.2, 0, 1 );
273 plcol0( 2 );
274 pllab( "(x)", "sin(x)/x", "#frPLplot Example 1 - Sinc Function" );
275
276 // Fill up the arrays
277
278 for ( i = 0; i < 100; i++ )
279 {
280 x[i] = ( i - 19.0 ) / 6.0;
281 y[i] = 1.0;
282 if ( x[i] != 0.0 )
283 y[i] = sin( x[i] ) / x[i];
284 }
285
286 // Draw the line
287
288 plcol0( 3 );
289 plline( 100, x, y );
290 }
291
292 //--------------------------------------------------------------------------
293
294 void
295 plot3( void )
296 {
297 int i;
298
299 // For the final graph we wish to override the default tick intervals, and
300 // so do not use PLENV
301
302 pladv( 0 );
303
304 // Use standard viewport, and define X range from 0 to 360 degrees, Y range
305 // from -1.2 to 1.2.
306
307 plvsta();
308 plwind( 0.0, 360.0, -1.2, 1.2 );
309
310 // Draw a box with ticks spaced 60 degrees apart in X, and 0.2 in Y.
311
312 plcol0( 1 );
313 plbox( "bcnst", 60.0, 2, "bcnstv", 0.2, 2 );
314
315 // Superimpose a dashed line grid, with 1.5 mm marks and spaces. plstyl
316 // expects a pointer!!
317
318 plstyl( 1, &mark1, &space1 );
319 plcol0( 2 );
320 plbox( "g", 30.0, 0, "g", 0.2, 0 );
321 plstyl( 0, &mark0, &space0 );
322
323 plcol0( 3 );
324 pllab( "Angle (degrees)", "sine", "#frPLplot Example 1 - Sine function" );
325
326 for ( i = 0; i < 101; i++ )
327 {
328 x[i] = 3.6 * i;
329 y[i] = sin( x[i] * 3.141592654 / 180.0 );
330 }
331
332 plcol0( 4 );
333 plline( 101, x, y );
334 }
335
336 int myplotCmd( ClientData PL_UNUSED( cd ), Tcl_Interp *PL_UNUSED( interp ), int PL_UNUSED( argc ), char **argv )
337 {
338 if ( !strcmp( argv[1], "1" ) )
339 myplot1();
340
341 if ( !strcmp( argv[1], "2" ) )
mypltr(PLFLT xloc,PLFLT yloc,PLFLT * tx,PLFLT * ty,void * PL_UNUSED (pltr_data))342 myplot2();
343
344 if ( !strcmp( argv[1], "3" ) )
345 myplot3();
346
347 if ( !strcmp( argv[1], "4" ) )
348 myplot4();
349
350 plflush();
351 return TCL_OK;
352 }
353