1 //      Polar plot demo.
2 //
3 
4 #include "plcdemos.h"
5 
6 //--------------------------------------------------------------------------
7 // main
8 //
9 // Generates polar plot, with 1-1 scaling.
10 //--------------------------------------------------------------------------
11 
12 int
main(int argc,char * argv[])13 main( int argc, char *argv[] )
14 {
15     int          i;
16     PLFLT        dtr, theta, dx, dy, r, offset;
17     char         text[4];
18     static PLFLT x0[361], y0[361];
19     static PLFLT x[361], y[361];
20 
21     dtr = M_PI / 180.0;
22     for ( i = 0; i <= 360; i++ )
23     {
24         x0[i] = cos( dtr * i );
25         y0[i] = sin( dtr * i );
26     }
27 
28 // Parse and process command line arguments
29 
30     (void) plparseopts( &argc, argv, PL_PARSE_FULL );
31 
32 // Set orientation to portrait - note not all device drivers
33 // support this, in particular most interactive drivers do not
34     plsori( 1 );
35 
36 // Initialize plplot
37 
38     plinit();
39 
40 // Set up viewport and window, but do not draw box
41 
42     plenv( -1.3, 1.3, -1.3, 1.3, 1, -2 );
43     // Draw circles for polar grid
44     for ( i = 1; i <= 10; i++ )
45     {
46         plarc( 0.0, 0.0, 0.1 * i, 0.1 * i, 0.0, 360.0, 0.0, 0 );
47     }
48 
49     plcol0( 2 );
50     for ( i = 0; i <= 11; i++ )
51     {
52         theta = 30.0 * i;
53         dx    = cos( dtr * theta );
54         dy    = sin( dtr * theta );
55 
56         // Draw radial spokes for polar grid
57 
58         pljoin( 0.0, 0.0, dx, dy );
59         sprintf( text, "%d", ROUND( theta ) );
60 
61         // Write labels for angle
62 
63         if ( theta < 9.99 )
64         {
65             offset = 0.45;
66         }
67         else if ( theta < 99.9 )
68         {
69             offset = 0.30;
70         }
71         else
72         {
73             offset = 0.15;
74         }
75 
76 // Slightly off zero to avoid floating point logic flips at 90 and 270 deg.
77         if ( dx >= -0.00001 )
78             plptex( dx, dy, dx, dy, -offset, text );
79         else
80             plptex( dx, dy, -dx, -dy, 1. + offset, text );
81     }
82 
83 // Draw the graph
84 
85     for ( i = 0; i <= 360; i++ )
86     {
87         r    = sin( dtr * ( 5 * i ) );
88         x[i] = x0[i] * r;
89         y[i] = y0[i] * r;
90     }
91     plcol0( 3 );
92     plline( 361, x, y );
93 
94     plcol0( 4 );
95     plmtex( "t", 2.0, 0.5, 0.5, "#frPLplot Example 3 - r(#gh)=sin 5#gh" );
96 
97 // Close the plot at end
98 
99     plend();
100     exit( 0 );
101 }
102