您好,欢迎访问三七文档
////ThisisaGUIsupportcodetothechapters12-16ofthebook//Programming--PrinciplesandPracticeUsingC++byBjarneStroustrup//#ifndefGRAPH_GUARD#defineGRAPH_GUARD1#includeFL/fl_draw.H#includeFL/Fl_Image.H#includePoint.h#include../std_lib_facilities.hnamespaceGraph_lib{//defenseagainstill-behavedLinuxmacros:#undefmajor#undefminor//------------------------------------------------------------------------------//Coloristhetypeweusetorepresentcolor.WecanuseColorlikethis://grid.set_color(Color::red);structColor{enumColor_type{red=FL_RED,blue=FL_BLUE,green=FL_GREEN,yellow=FL_YELLOW,white=FL_WHITE,black=FL_BLACK,magenta=FL_MAGENTA,cyan=FL_CYAN,dark_red=FL_DARK_RED,dark_green=FL_DARK_GREEN,dark_yellow=FL_DARK_YELLOW,dark_blue=FL_DARK_BLUE,dark_magenta=FL_DARK_MAGENTA,dark_cyan=FL_DARK_CYAN};enumTransparency{invisible=0,visible=255};Color(Color_typecc):c(Fl_Color(cc)),v(visible){}Color(Color_typecc,Transparencyvv):c(Fl_Color(cc)),v(vv){}Color(intcc):c(Fl_Color(cc)),v(visible){}Color(Transparencyvv):c(Fl_Color()),v(vv){}//defaultcolorintas_int()const{returnc;}charvisibility()const{returnv;}voidset_visibility(Transparencyvv){v=vv;}private:charv;//invisibleandvisiblefornowFl_Colorc;};//------------------------------------------------------------------------------structLine_style{enumLine_style_type{solid=FL_SOLID,//-------dash=FL_DASH,//----dot=FL_DOT,//.......dashdot=FL_DASHDOT,//-.-.dashdotdot=FL_DASHDOTDOT,//-..-..};Line_style(Line_style_typess):s(ss),w(0){}Line_style(Line_style_typelst,intww):s(lst),w(ww){}Line_style(intss):s(ss),w(0){}intwidth()const{returnw;}intstyle()const{returns;}private:ints;intw;};//------------------------------------------------------------------------------classFont{public:enumFont_type{helvetica=FL_HELVETICA,helvetica_bold=FL_HELVETICA_BOLD,helvetica_italic=FL_HELVETICA_ITALIC,helvetica_bold_italic=FL_HELVETICA_BOLD_ITALIC,courier=FL_COURIER,courier_bold=FL_COURIER_BOLD,courier_italic=FL_COURIER_ITALIC,courier_bold_italic=FL_COURIER_BOLD_ITALIC,times=FL_TIMES,times_bold=FL_TIMES_BOLD,times_italic=FL_TIMES_ITALIC,times_bold_italic=FL_TIMES_BOLD_ITALIC,symbol=FL_SYMBOL,screen=FL_SCREEN,screen_bold=FL_SCREEN_BOLD,zapf_dingbats=FL_ZAPF_DINGBATS};Font(Font_typeff):f(ff){}Font(intff):f(ff){}intas_int()const{returnf;}private:intf;};//------------------------------------------------------------------------------templateclassTclassVector_ref{vectorT*v;vectorT*owned;public:Vector_ref(){}Vector_ref(T&a){push_back(a);}Vector_ref(T&a,T&b);Vector_ref(T&a,T&b,T&c);Vector_ref(T*a,T*b=0,T*c=0,T*d=0){if(a)push_back(a);if(b)push_back(b);if(c)push_back(c);if(d)push_back(d);}~Vector_ref(){for(inti=0;iowned.size();++i)deleteowned[i];}voidpush_back(T&s){v.push_back(&s);}voidpush_back(T*p){v.push_back(p);owned.push_back(p);}T&operator[](inti){return*v[i];}constT&operator[](inti)const{return*v[i];}intsize()const{returnv.size();}private://preventcopyingVector_ref(constVectorT&);Vector_ref&operator=(constVectorT&);};//------------------------------------------------------------------------------typedefdoubleFct(double);classShape{//dealswithcolorandstyle,andholdssequenceoflinespublic:voiddraw()const;//dealwithcoloranddrawlinesvirtualvoidmove(intdx,intdy);//movetheshape+=dxand+=dyvoidset_color(Colorcol){lcolor=col;}Colorcolor()const{returnlcolor;}voidset_style(Line_stylesty){ls=sty;}Line_stylestyle()const{returnls;}voidset_fill_color(Colorcol){fcolor=col;}Colorfill_color()const{returnfcolor;}Pointpoint(inti)const{returnpoints[i];}//readonlyaccesstopointsintnumber_of_points()const{returnint(points.size());}virtual~Shape(){}protected:Shape();virtualvoiddraw_lines()const;//drawtheappropriatelinesvoidadd(Pointp);//addptopointsvoidset_point(inti,Pointp);//points[i]=p;private:vectorPointpoints;//notusedbyallshapesColorlcolor;//colorforlinesandcharactersLine_stylels;Colorfcolor;//fillcolorShape(constShape&);//preventcopyingShape&operator=(constShape&);};//------------------------------------------------------------------------------structFunction:Shape{//thefunctionparametersarenotstoredFunction(Fctf,doubler1,doubler2,Pointorig,intcount=100,doublexscale=25,doubleyscale=25);};//------------------------------------------------------------------------------structLine:Shape{//aLineisaShapedefinedbytwoPointsLine(Pointp1,Pointp2);//constructalinefromtwopoints};//------------------------------------------------------------------------------structRectangle:Shape{Rectangle(Pointxy,intww,inthh):w(ww),h(hh){add(xy);if(h=0||w=0)error(Badrectangle:non-positiveside);}Rectangle(Pointx,Pointy):w(y.x-x.x),h(y.y-x.y){add(x);if(h=0||w=0)error(Badrectangle:non-positivewidthorheight);}voiddraw_lines()const;intheight()const{returnh;}intwidth()const{returnw;}private:inth;//heightintw;//width};//------------------------------------------------------------------------------structOpen_polyline:Shape{//opensequenceoflinesvoidadd(Pointp){Shape::add(p);}voiddraw_lines()const;};//--------------------------------------------------------------
本文标题:graph.h
链接地址:https://www.777doc.com/doc-5279479 .html