// // This is a GUI support code to the chapters 12-16 of the book // "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup // #include #include #include "Graph.h" //------------------------------------------------------------------------------ namespace Graph_lib { //------------------------------------------------------------------------------ Shape::Shape() : lcolor(fl_color()), // default color for lines and characters ls(0), // default style fcolor(Color::invisible) // no fill {} //------------------------------------------------------------------------------ void Shape::add(Point p) // protected { points.push_back(p); } //------------------------------------------------------------------------------ void Shape::set_point(int i,Point p) // not used; not necessary so far { points[i] = p; } //------------------------------------------------------------------------------ void Shape::draw_lines() const { if (color().visibility() && 1 line_intersect(Point p1, Point p2, Point p3, Point p4, bool& parallel) { double x1 = p1.x; double x2 = p2.x; double x3 = p3.x; double x4 = p4.x; double y1 = p1.y; double y2 = p2.y; double y3 = p3.y; double y4 = p4.y; double denom = ((y4 - y3)*(x2-x1) - (x4-x3)*(y2-y1)); if (denom == 0){ parallel= true; return pair(0,0); } parallel = false; return pair( ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3))/denom, ((x2-x1)*(y1-y3) - (y2-y1)*(x1-x3))/denom); } //------------------------------------------------------------------------------ //intersection between two line segments //Returns true if the two segments intersect, //in which case intersection is set to the point of intersection bool line_segment_intersect(Point p1, Point p2, Point p3, Point p4, Point& intersection){ bool parallel; pair u = line_intersect(p1,p2,p3,p4,parallel); if (parallel || u.first < 0 || u.first > 1 || u.second < 0 || u.second > 1) return false; intersection.x = p1.x + u.first*(p2.x - p1.x); intersection.y = p1.y + u.first*(p2.y - p1.y); return true; } //------------------------------------------------------------------------------ void Polygon::add(Point p) { int np = number_of_points(); if (1= len && s.substr(s.length()-len, len) == smap[i].extension) return smap[i].suffix; } return Suffix::none; } //------------------------------------------------------------------------------ // somewhat over-elaborate constructor // because errors related to image files can be such a pain to debug Image::Image(Point xy, string s, Suffix::Encoding e) :w(0), h(0), fn(xy,"") { add(xy); if (!can_open(s)) { // can we open s? fn.set_label("cannot open \""+s+'\"'); p = new Bad_image(30,20); // the "error image" return; } if (e == Suffix::none) e = get_encoding(s); switch(e) { // check if it is a known encoding case Suffix::jpg: p = new Fl_JPEG_Image(s.c_str()); break; case Suffix::gif: p = new Fl_GIF_Image(s.c_str()); break; default: // Unsupported image encoding fn.set_label("unsupported file type \""+s+'\"'); p = new Bad_image(30,20); // the "error image" } } //------------------------------------------------------------------------------ void Image::draw_lines() const { if (fn.label()!="") fn.draw_lines(); if (w&&h) p->draw(point(0).x,point(0).y,w,h,cx,cy); else p->draw(point(0).x,point(0).y); } //------------------------------------------------------------------------------ } // of namespace Graph_lib