Listing 2
#include <cmath> //For M_PI and similar constants
#include <mingpp.h>
class Shape{
private:
unsigned short line_sz;
int line_col,line_op;
int fill_col,fill_op;
public:
Shape(unsigned short s=1,int c1=0x000000,int c2=0x000000,int o1=100,
int o2=100)
:line_sz(s),line_col(c1),line_op(o1),fill_col(c2),fill_op(o2){}
/** Splits a RGB colour into its components */
static void get_rgb(const int col,int &r,int &g,int &b){
r=(col&0xFF0000)>>16;
g=(col&0xFF00)>>8;
b=(col&0xFF);
}
/** Convert a 0..100 opacity to 0x00 to 0xff*/
static int get_opacity(const int op){
if(op>=100)return 0xff;
if(op<=0)return 0;
return (op*256)/100;
}
/** Initialize pen : called before other drawing functions */
void init_pen(SWFShape *shape,float sx,float sy)const{
int r,g,b;get_rgb(line_col,r,g,b);
shape->setLine(line_sz,r,g,b,get_opacity(line_op));
shape->movePenTo(sx,sy);
}
/** Draw arc, anticlockwise */
void draw_arc_helper_ac(SWFShape *shape,float sx,float sy,double sa,
double ea,float xr,float yr)const{
const double PIBy4=M_PI_4;
const double PIBy8=M_PI_4/2.0;
double control_xr=xr/cos(PIBy8);
double control_yr=yr/cos(PIBy8);
while(ea>sa){
double ma;
if((ea-sa)>PIBy4){ //Draw a 45 degree segment
ma=sa+PIBy8; //Add 22.5 degrees
sa+=PIBy4; //Add 45 degrees
}
else{ //Final segment
ma=(sa+ea)/2;
control_xr=xr/cos((ea-sa)/2);
control_yr=yr/cos((ea-sa)/2);
sa=ea;
}
double cx=sx+(cos(ma)*control_xr);
double cy=sy-(sin(ma)*control_yr);
double ex=sx+(cos(sa)*xr);
double ey=sy-(sin(sa)*yr);
shape->drawCurveTo(cx,cy,ex,ey);
}
}
/** Draw a pie-slice */
void draw_pie_slice(SWFSprite *movieclip,const float sx,const float sy,
const double sa,const double ea,const float xr,const float yr)const{
float x=sx+(cos(sa)*xr);
float y=sy-(sin(sa)*yr);
SWFShape *shape=new SWFShape;
int r,g,b;get_rgb(fill_col,r,g,b);
shape->setLeftFill(shape->addSolidFill(r,g,b,get_opacity(fill_op)));
init_pen(shape,sx,sy);
shape->drawLineTo(x,y);
draw_arc_helper_ac(shape,sx,sy,sa,ea,xr,yr);
shape->drawLineTo(sx,sy);
movieclip->add(shape);
}
};