• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

plarc.c

Go to the documentation of this file.
00001 // plarc()
00002 //
00003 // Copyright (C) 2009  Hezekiah M. Carty
00004 //
00005 // This file is part of PLplot.
00006 //
00007 // PLplot is free software; you can redistribute it and/or modify
00008 // it under the terms of the GNU Library General Public License as published
00009 // by the Free Software Foundation; either version 2 of the License, or
00010 // (at your option) any later version.
00011 //
00012 // PLplot is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU Library General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU Library General Public License
00018 // along with PLplot; if not, write to the Free Software
00019 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020 //
00021 
00022 #include "plplotP.h"
00023 
00024 #define CIRCLE_SEGMENTS    ( PL_MAXPOLY - 1 )
00025 #define DEG_TO_RAD( x )    ( ( x ) * M_PI / 180.0 )
00026 
00027 //--------------------------------------------------------------------------
00028 // plarc_approx : Plot an approximated arc with a series of lines
00029 //
00030 // Takes the same arguments, with the same units, as c_plarc below.
00031 //
00032 //--------------------------------------------------------------------------
00033 void
00034 plarc_approx( PLFLT x, PLFLT y, PLFLT a, PLFLT b, PLFLT angle1, PLFLT angle2, PLFLT rotate, PLBOOL fill )
00035 {
00036     PLINT i;
00037     PLFLT theta0, theta_step, theta, d_angle;
00038     PLINT segments;
00039     PLFLT xs[CIRCLE_SEGMENTS + 1], ys[CIRCLE_SEGMENTS + 1];
00040     PLFLT cphi, sphi, ctheta, stheta;
00041 
00042     // The difference between the start and end angles
00043     d_angle = DEG_TO_RAD( angle2 - angle1 );
00044     if ( fabs( d_angle ) > M_PI * 2.0 )
00045         d_angle = M_PI * 2.0;
00046 
00047     // Calculate cosine and sine of angle of major axis wrt the x axis
00048     cphi = cos( DEG_TO_RAD( rotate ) );
00049     sphi = sin( DEG_TO_RAD( rotate ) );
00050 
00051     // The number of line segments used to approximate the arc
00052     segments = fabs( d_angle ) / ( 2.0 * M_PI ) * CIRCLE_SEGMENTS;
00053     // Always use at least 2 arc points, otherwise fills will break.
00054     if ( segments < 2 )
00055         segments = 2;
00056     // The start angle in radians and number of radians in each approximating
00057     // segment.
00058     theta0 = DEG_TO_RAD( angle1 );
00059 
00060     theta_step = d_angle / ( segments - 1 );
00061 
00062     // The coordinates for the circle outline
00063     for ( i = 0; i < segments; i++ )
00064     {
00065         theta  = theta0 + theta_step * (PLFLT) i;
00066         ctheta = cos( theta );
00067         stheta = sin( theta );
00068         xs[i]  = x + a * ctheta * cphi - b * stheta * sphi;
00069         ys[i]  = y + a * ctheta * sphi + b * stheta * cphi;
00070     }
00071 
00072     if ( fill )
00073     {
00074         // Add the center point if we aren't drawing a circle
00075         if ( fabs( d_angle ) < M_PI * 2.0 )
00076         {
00077             xs[segments] = x;
00078             ys[segments] = y;
00079             segments++;
00080         }
00081         // Draw a filled arc
00082         plfill( segments, xs, ys );
00083     }
00084     else
00085     {
00086         // Draw the arc outline
00087         plline( segments, xs, ys );
00088     }
00089 }
00090 
00091 //--------------------------------------------------------------------------
00092 // plarc : Plot an arc
00093 //
00094 // Takes the following arguments:
00095 //
00096 //   x, y:
00097 //      x and y coordinates for the center of the arc
00098 //
00099 //   a, b:
00100 //      Radius of the arc's major and minor axes
00101 //
00102 //   angle1:
00103 //      Start angle (degrees)
00104 //
00105 //   angle2:
00106 //      End angle (degrees)
00107 //
00108 //   fill:
00109 //      Should the arc be filled?
00110 //
00111 //--------------------------------------------------------------------------
00112 void
00113 c_plarc( PLFLT x, PLFLT y, PLFLT a, PLFLT b, PLFLT angle1, PLFLT angle2, PLFLT rotate, PLBOOL fill )
00114 {
00115     PLINT      xscl[2], yscl[2];
00116     PLINT      clpxmi, clpxma, clpymi, clpyma;
00117     arc_struct *arc_info;
00118 
00119     // TODO: For now, only unrotated plots use the driver-accelerated path.
00120     if ( plsc->dev_arc && plsc->diorot == 0 )
00121     {
00122         arc_info = (arc_struct *) malloc( (size_t) sizeof ( arc_struct ) );
00123 
00124         xscl[0] = plP_wcpcx( x - a );
00125         xscl[1] = plP_wcpcx( x + a );
00126         yscl[0] = plP_wcpcy( y - b );
00127         yscl[1] = plP_wcpcy( y + b );
00128         difilt( xscl, yscl, 2, &clpxmi, &clpxma, &clpymi, &clpyma );
00129 
00130         arc_info->x = 0.5 * ( xscl[1] + xscl[0] );
00131         arc_info->y = 0.5 * ( yscl[1] + yscl[0] );
00132         arc_info->a = 0.5 * ( xscl[1] - xscl[0] );
00133         arc_info->b = 0.5 * ( yscl[1] - yscl[0] );
00134 
00135         arc_info->angle1 = angle1;
00136         arc_info->angle2 = angle2;
00137         arc_info->rotate = rotate;
00138         arc_info->fill   = fill;
00139 
00140         plP_esc( PLESC_ARC, arc_info );
00141 
00142         free( arc_info );
00143     }
00144     else
00145     {
00146         plarc_approx( x, y, a, b, angle1, angle2, rotate, fill );
00147     }
00148 }
00149 

Generated on Wed Oct 12 2011 20:42:22 for PLplot by  doxygen 1.7.1