SlideShare a Scribd company logo
Chapter 13 Graphics classes Bjarne Stroustrup www.stroustrup.com/Programming
Abstract Chapter 12 demonstrates how to create simple windows and display basic shapes: square, circle, triangle, and ellipse; It showed how to manipulate such shapes: change colors and line style, add text, etc. Chapter 13 shows how these shapes and operations are implemented, and shows a few more examples.  In Chapter 12, we were basically tool users; here we become tool builders. Stroustrup/Programming
Overview Graphing Model  Code organization Interface classes Point Line Lines Grid Open Polylines Closed Polylines Color Text Unnamed objects Stroustrup/Programming
Display model Objects (such as graphs) are “attached to” (“placed in”) a window. The “display engine” invokes  display commands (such as “draw line from x to y”) for the objects in a window Objects such as Rectangle add vectors of lines to the window to draw Open_polyline Rectangle “ window” Display Engine attach() attach() draw() draw() draw() Stroustrup/Programming
Code organization Gui.h Point.h: Stroustrup/Programming //  Graphing interface: struct Point { … }; … //  window interface: class Window {…}; … FLTK headers Graph code Window code FLTK code chapter12.cpp: Window.h: Window.cpp: #include "Graph.h" #include "Window.h" int main() { … } Graph.cpp: Graph.h: struct Point { … }; //  GUI interface: struct In_box { … }; … GUI code GUI.cpp:
Source files Header File that contains interface information (declarations) #include  in user and implementer .cpp (“code file” / “implementation file”) File that contains code implementing interfaces defined in headers and/or uses such interfaces #include s headers Read the  Graph.h  header And later the  graph.cpp  implementation file Don’t read the  Window.h  header or the  window.cpp  implementation file Naturally, some of you will take a peek Beware: heavy use of yet unexplained C++ features Stroustrup/Programming
Design note The ideal of program design is to represent concepts directly in code We take this ideal very seriously For example: Window  – a window as we see it on the screen Will look different on different operating systems (not our business) Line  – a line as you see it in a window on the screen Point  – a coordinate point Shape  – what’s common to shapes (imperfectly explained for now; all details in Chapter 14) Color  – as you see it on the screen Stroustrup/Programming
Point namespace Graph_lib  //  our graphics interface is in Graph_lib { struct Point    //  a Point is simply a pair of ints (the coordinates) {  int x, y; Point(int xx, int yy) : x(xx), y(yy) { } }; //  Note the  ';' } Stroustrup/Programming
Line struct Shape { //   hold lines represented as pairs of points //  knows how to display lines }; struct Line : Shape  //  a  Line  is a  Shape  defined by just two  Points { Line(Point p1, Point p2); }; Line::Line(Point p1, Point p2) //  construct a line from p1 to p2 { add(p1); //  add p1 to this shape (add() is provided by Shape) add(p2); //  add p2 to this shape } Stroustrup/Programming
Line example //  draw two lines: using namespace Graph_lib; Simple_window win(Point(100,100),600,400,"Canvas");  //  make a   window Line horizontal(Point(100,100),Point(200,100));  //  make a horizontal line Line vertical(Point(150,50),Point(150,150)); //  make a vertical line win.attach(horizontal); //  attach the lines to the window win.attach(vertical); win.wait_for_button(); //  Display! Stroustrup/Programming
Line example Stroustrup/Programming
Line example Individual lines are independent horizontal.set_color(Color::red); vertical.set_color(Color::green); Stroustrup/Programming
Lines struct Lines : Shape { //  a  Lines  object is a set lines //   We use Lines when we want to manipulate //  all the lines as one shape, e.g. move them all void add(Point p1, Point p2); //  add line from p1 to p2 void draw_lines() const; //  to be called by  Window  to draw  Lines }; Terminology: Lines is “derived from” Shape Lines “inherit from” Shape Lines is “a kind of” Shape Shape is “the base” of Lines This is the key to what is called “object-oriented programming” We’ll get back to this in Chapter 14 Stroustrup/Programming
Lines Example Lines x; x.add(Point(100,100), Point(200,100)); //  horizontal line x.add(Point(150,50), Point(150,150)); //  vertical line win.attach(x); //  attach Lines x to Window win win.wait_for_button(); //  Draw! Stroustrup/Programming
Lines example Looks exactly like the two  Line s example Stroustrup/Programming
Implementation: Lines void Lines::add(Point p1, Point p2) //  use Shape’s add() { Shape::add(p1); Shape::add(p2); } void Lines::draw_lines() const //  to somehow be called from Shape { for (int i=1; i<number_of_points(); i+=2) fl_line(point(i-1).x,point(i-1).y,point(i).x,point(i).y); } Note fl_line is a basic line drawing function from FLTK FLTK is used in the  implementation , not in the  interface  to our classes We could replace FLTK with another graphics library Stroustrup/Programming
Draw Grid (Why bother with  Lines  when we have  Line ?) //  A  Lines  object may hold many related lines //  Here we construct a grid: int x_size = win.x_max(); int y_size = win.y_max();  int x_grid = 80; int y_grid = 40; Lines grid; for (int x=x_grid; x<x_size; x+=x_grid) //  veritcal lines grid.add(Point(x,0),Point(x,y_size)); for (int y = y_grid; y<y_size; y+=y_grid) //  horizontal lines grid.add(Point(0,y),Point(x_size,y)); win.attach(grid);  //  attach our grid to our window (note grid is one object Stroustrup/Programming
Grid Stroustrup/Programming
Color struct Color { //   Map FLTK colors and scope them; //  deal with visibility/transparency enum Color_type { red=FL_RED, blue=FL_BLUE, /*  …  */ }; enum Transparency { visible=0, invisible=255 }; Color(Color_type cc) :c(Fl_Color(cc)), v(visible) { } Color(int cc) :c(Fl_Color(cc)), v(visible) { } Color(Color_type cc, Transparency t) :c(Fl_Color(cc)), v(t) { } int as_int() const { return c; } Transparency visibility() { return v; } void set_visibility(Transparency t) { v = t; } private: Fl_Color c; Transparancy v; //  visibility (transparency not yet implemented) }; Stroustrup/Programming
Draw red grid grid.set_color(Color::red); Stroustrup/Programming
Line_style struct Line_style { enum Line_style_type { solid=FL_SOLID, //  ------- dash=FL_DASH, //  - - - - dot=FL_DOT, //  .......  dashdot=FL_DASHDOT, //  - . - .  dashdotdot=FL_DASHDOTDOT, //  -..-.. }; Line_style(Line_style_type ss) :s(ss), w(0) { } Line_style(Line_style_type lst, int ww) :s(lst), w(ww) { } Line_style(int ss) :s(ss), w(0) { } int width() const { return w; } int style() const { return s; } private: int s; int w; }; Stroustrup/Programming
Example: colored fat dash grid grid.set_style(Line_style(Line_style::dash,2)); Stroustrup/Programming
Polylines struct Open_polyline : Shape { //  open sequence of lines void add(Point p) { Shape::add(p); } }; struct Closed_polyline : Open_polyline { //  closed sequence of lines void draw_lines() const { Open_polyline::draw_lines(); //  draw lines (except the closing one) //  draw the closing line: fl_line(point(number_of_points()-1).x, point(number_of_points()-1).y, point(0).x,point(0).y  ); } void add(Point p) { Shape::add(p); } }; Stroustrup/Programming
Open_polyline Open_polyline opl; opl.add(Point(100,100)); opl.add(Point(150,200)); opl.add(Point(250,250)); opl.add(Point(300,200));  Stroustrup/Programming
Closed_polyline Closed_polyline cpl; cpl.add(Point(100,100)); cpl.add(Point(150,200)); cpl.add(Point(250,250)); cpl.add(Point(300,200));  Stroustrup/Programming
Closed_polyline cpl.add(Point(100,250)); A   Closed_polyline  is not a polygon some closed_polylines look like polygons A  Polygon  is a  Closed_polyline  where no lines cross A  Polygon  has a stronger invariant than a  Closed_polyline Stroustrup/Programming
Text struct Text : Shape { Text(Point x, const string& s)  //  x  is the bottom left of the first letter : lab(s), fnt(fl_font()), //  default character font fnt_sz(fl_size()) //  default character size { add(x); } //  store x in the Shape part of the Text void draw_lines() const; //  … the usual “getter and setter” member functions … private: string lab; //  label Font fnt; //  character font of label int fnt_sz; //  size of characters }; Stroustrup/Programming
Add text Text t(Point(200,200), &quot;A closed polyline that isn’t a polygon&quot;); t.set_color(Color::blue); Stroustrup/Programming
Implementation: Text void Text::draw_lines() const { fl_draw(lab.c_str(), point(0).x, point(0).y); } //  fl_draw() is a basic text drawing function from FLTK Stroustrup/Programming
Color matrix Let’s draw a color matrix To see some of the colors we have to work with To see how messy two-dimensional addressing can be See Chapter 24 for real matrices To see how to avoid inventing names for hundreds of objects Stroustrup/Programming
Color Matrix (16*16) Simple_window win20(pt,600,400,&quot;16*16 color matrix&quot;); Vector_ref<Rectangle> vr;  //  use like vector   //  but imagine that it holds references to objects for (int i = 0; i<16; ++i) { //  i is the horizontal coordinate for (int j = 0; j<16; ++j) { //  j is the vertical coordinate vr.push_back(new Rectangle(Point(i*20,j*20),20,20)); vr[vr.size()-1].set_fill_color(i*16+j); win20.attach(vr[vr.size()-1]); } //  new  makes an object that you can give to a  Vector_ref  to hold //   Vector_ref  is built using  std::vector , but is not in the standard library Stroustrup/Programming
Color matrix (16*16) More examples and graphics classes in the book (chapter 13) Stroustrup/Programming
Next lecture What is class Shape? Introduction to object-oriented programming Stroustrup/Programming

More Related Content

PPT
13 graph classes_2
PPT
14 class design (1)
PPT
14 class design
PPT
Oop lec 3(structures)
PDF
CP Handout#8
DOCX
Ecs40 winter 2017 homework 3
PPTX
graphics programming in java
PDF
ASqueo Houdini Exercise 2
13 graph classes_2
14 class design (1)
14 class design
Oop lec 3(structures)
CP Handout#8
Ecs40 winter 2017 homework 3
graphics programming in java
ASqueo Houdini Exercise 2

What's hot (20)

PDF
CP Handout#7
PDF
L10
PPTX
Applet and graphics programming
PDF
Auto cad commands.
PDF
Auto cad tutorial
PDF
Introduction to programming c and data structures
PDF
Introduction to programming c and data-structures
PDF
[ITP - Lecture 16] Structures in C/C++
PPTX
Autocad Commands
PDF
CP Handout#2
PPT
Mesics lecture 5 input – output in ‘c’
PDF
[ITP - Lecture 17] Strings in C/C++
PPTX
Bsc cs i pic u-4 function, storage class and array and strings
PDF
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Diploma ii cfpc u-4 function, storage class and array and strings
PPTX
Module 5-Structure and Union
PPTX
Chap 2 input output dti2143
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PPTX
Cad ppt
PPTX
Module 4- Arrays and Strings
CP Handout#7
L10
Applet and graphics programming
Auto cad commands.
Auto cad tutorial
Introduction to programming c and data structures
Introduction to programming c and data-structures
[ITP - Lecture 16] Structures in C/C++
Autocad Commands
CP Handout#2
Mesics lecture 5 input – output in ‘c’
[ITP - Lecture 17] Strings in C/C++
Bsc cs i pic u-4 function, storage class and array and strings
MANAGING INPUT AND OUTPUT OPERATIONS IN C MRS.SOWMYA JYOTHI.pdf
Diploma ii cfpc u-4 function, storage class and array and strings
Module 5-Structure and Union
Chap 2 input output dti2143
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
Cad ppt
Module 4- Arrays and Strings
Ad

Viewers also liked (20)

PDF
Code Refactoring - Live Coding Demo (JavaDay 2014)
PPTX
Manipulating file in Python
PDF
Git hooks For PHP Developers
ODP
Servicios web con Python
PDF
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
PDF
FLTK Summer Course - Part VI - Sixth Impact - Exercises
PDF
FLTK Summer Course - Part II - Second Impact - Exercises
PDF
Using Git on the Command Line
PPT
Introduction to Git Commands and Concepts
PDF
FLTK Summer Course - Part VIII - Eighth Impact
PDF
Advanced Git
PDF
"Git Hooked!" Using Git hooks to improve your software development process
PPT
Creating Custom Drupal Modules
PDF
Blisstering drupal module development ppt v1.2
PDF
TMS - Schedule of Presentations and Reports
PDF
FLTK Summer Course - Part III - Third Impact
PDF
FLTK Summer Course - Part II - Second Impact
PDF
FLTK Summer Course - Part I - First Impact - Exercises
PDF
FLTK Summer Course - Part VII - Seventh Impact
PPTX
Linux GIT commands
Code Refactoring - Live Coding Demo (JavaDay 2014)
Manipulating file in Python
Git hooks For PHP Developers
Servicios web con Python
EuroPython 2013 - FAST, DOCUMENTED AND RELIABLE JSON BASED WEBSERVICES WITH P...
FLTK Summer Course - Part VI - Sixth Impact - Exercises
FLTK Summer Course - Part II - Second Impact - Exercises
Using Git on the Command Line
Introduction to Git Commands and Concepts
FLTK Summer Course - Part VIII - Eighth Impact
Advanced Git
"Git Hooked!" Using Git hooks to improve your software development process
Creating Custom Drupal Modules
Blisstering drupal module development ppt v1.2
TMS - Schedule of Presentations and Reports
FLTK Summer Course - Part III - Third Impact
FLTK Summer Course - Part II - Second Impact
FLTK Summer Course - Part I - First Impact - Exercises
FLTK Summer Course - Part VII - Seventh Impact
Linux GIT commands
Ad

Similar to 13 Graph Classes (20)

PPT
Some examples of shapes are Lines, Polygons, Axes, and Text
PDF
Im having trouble figuring out how to code these sections for an a.pdf
PPTX
Input Output function in c programing language.pptx
PPT
Client Side Programming with Applet
PDF
Intake 37 6
PDF
Intake 38 6
DOCX
What is C Programming LAnguage and Why to Learn.docx
PPT
Fundamental of C Programming Language and Basic Input/Output Function
PDF
Computer Graphics Concepts
PPTX
Programming in C.pptx
PPT
An imperative study of c
PPTX
C programming
PDF
Functions And Header Files In C++ | Bjarne stroustrup
PPTX
Data Input and Output
PPS
C programming session 01
PPTX
Managing input and output operations in c
ODP
C prog ppt
PPSX
Concepts of C [Module 2]
PPS
C++ Language
DOCX
string , pointer
Some examples of shapes are Lines, Polygons, Axes, and Text
Im having trouble figuring out how to code these sections for an a.pdf
Input Output function in c programing language.pptx
Client Side Programming with Applet
Intake 37 6
Intake 38 6
What is C Programming LAnguage and Why to Learn.docx
Fundamental of C Programming Language and Basic Input/Output Function
Computer Graphics Concepts
Programming in C.pptx
An imperative study of c
C programming
Functions And Header Files In C++ | Bjarne stroustrup
Data Input and Output
C programming session 01
Managing input and output operations in c
C prog ppt
Concepts of C [Module 2]
C++ Language
string , pointer

Recently uploaded (20)

PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Electronic commerce courselecture one. Pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
cuic standard and advanced reporting.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
Empathic Computing: Creating Shared Understanding
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
MIND Revenue Release Quarter 2 2025 Press Release
Electronic commerce courselecture one. Pdf
Network Security Unit 5.pdf for BCA BBA.
cuic standard and advanced reporting.pdf
Machine Learning_overview_presentation.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Chapter 3 Spatial Domain Image Processing.pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Assigned Numbers - 2025 - Bluetooth® Document
Empathic Computing: Creating Shared Understanding
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Spectral efficient network and resource selection model in 5G networks
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
20250228 LYD VKU AI Blended-Learning.pptx
sap open course for s4hana steps from ECC to s4
“AI and Expert System Decision Support & Business Intelligence Systems”

13 Graph Classes

  • 1. Chapter 13 Graphics classes Bjarne Stroustrup www.stroustrup.com/Programming
  • 2. Abstract Chapter 12 demonstrates how to create simple windows and display basic shapes: square, circle, triangle, and ellipse; It showed how to manipulate such shapes: change colors and line style, add text, etc. Chapter 13 shows how these shapes and operations are implemented, and shows a few more examples. In Chapter 12, we were basically tool users; here we become tool builders. Stroustrup/Programming
  • 3. Overview Graphing Model Code organization Interface classes Point Line Lines Grid Open Polylines Closed Polylines Color Text Unnamed objects Stroustrup/Programming
  • 4. Display model Objects (such as graphs) are “attached to” (“placed in”) a window. The “display engine” invokes display commands (such as “draw line from x to y”) for the objects in a window Objects such as Rectangle add vectors of lines to the window to draw Open_polyline Rectangle “ window” Display Engine attach() attach() draw() draw() draw() Stroustrup/Programming
  • 5. Code organization Gui.h Point.h: Stroustrup/Programming // Graphing interface: struct Point { … }; … // window interface: class Window {…}; … FLTK headers Graph code Window code FLTK code chapter12.cpp: Window.h: Window.cpp: #include &quot;Graph.h&quot; #include &quot;Window.h&quot; int main() { … } Graph.cpp: Graph.h: struct Point { … }; // GUI interface: struct In_box { … }; … GUI code GUI.cpp:
  • 6. Source files Header File that contains interface information (declarations) #include in user and implementer .cpp (“code file” / “implementation file”) File that contains code implementing interfaces defined in headers and/or uses such interfaces #include s headers Read the Graph.h header And later the graph.cpp implementation file Don’t read the Window.h header or the window.cpp implementation file Naturally, some of you will take a peek Beware: heavy use of yet unexplained C++ features Stroustrup/Programming
  • 7. Design note The ideal of program design is to represent concepts directly in code We take this ideal very seriously For example: Window – a window as we see it on the screen Will look different on different operating systems (not our business) Line – a line as you see it in a window on the screen Point – a coordinate point Shape – what’s common to shapes (imperfectly explained for now; all details in Chapter 14) Color – as you see it on the screen Stroustrup/Programming
  • 8. Point namespace Graph_lib // our graphics interface is in Graph_lib { struct Point // a Point is simply a pair of ints (the coordinates) { int x, y; Point(int xx, int yy) : x(xx), y(yy) { } }; // Note the ';' } Stroustrup/Programming
  • 9. Line struct Shape { // hold lines represented as pairs of points // knows how to display lines }; struct Line : Shape // a Line is a Shape defined by just two Points { Line(Point p1, Point p2); }; Line::Line(Point p1, Point p2) // construct a line from p1 to p2 { add(p1); // add p1 to this shape (add() is provided by Shape) add(p2); // add p2 to this shape } Stroustrup/Programming
  • 10. Line example // draw two lines: using namespace Graph_lib; Simple_window win(Point(100,100),600,400,&quot;Canvas&quot;); // make a window Line horizontal(Point(100,100),Point(200,100)); // make a horizontal line Line vertical(Point(150,50),Point(150,150)); // make a vertical line win.attach(horizontal); // attach the lines to the window win.attach(vertical); win.wait_for_button(); // Display! Stroustrup/Programming
  • 12. Line example Individual lines are independent horizontal.set_color(Color::red); vertical.set_color(Color::green); Stroustrup/Programming
  • 13. Lines struct Lines : Shape { // a Lines object is a set lines // We use Lines when we want to manipulate // all the lines as one shape, e.g. move them all void add(Point p1, Point p2); // add line from p1 to p2 void draw_lines() const; // to be called by Window to draw Lines }; Terminology: Lines is “derived from” Shape Lines “inherit from” Shape Lines is “a kind of” Shape Shape is “the base” of Lines This is the key to what is called “object-oriented programming” We’ll get back to this in Chapter 14 Stroustrup/Programming
  • 14. Lines Example Lines x; x.add(Point(100,100), Point(200,100)); // horizontal line x.add(Point(150,50), Point(150,150)); // vertical line win.attach(x); // attach Lines x to Window win win.wait_for_button(); // Draw! Stroustrup/Programming
  • 15. Lines example Looks exactly like the two Line s example Stroustrup/Programming
  • 16. Implementation: Lines void Lines::add(Point p1, Point p2) // use Shape’s add() { Shape::add(p1); Shape::add(p2); } void Lines::draw_lines() const // to somehow be called from Shape { for (int i=1; i<number_of_points(); i+=2) fl_line(point(i-1).x,point(i-1).y,point(i).x,point(i).y); } Note fl_line is a basic line drawing function from FLTK FLTK is used in the implementation , not in the interface to our classes We could replace FLTK with another graphics library Stroustrup/Programming
  • 17. Draw Grid (Why bother with Lines when we have Line ?) // A Lines object may hold many related lines // Here we construct a grid: int x_size = win.x_max(); int y_size = win.y_max(); int x_grid = 80; int y_grid = 40; Lines grid; for (int x=x_grid; x<x_size; x+=x_grid) // veritcal lines grid.add(Point(x,0),Point(x,y_size)); for (int y = y_grid; y<y_size; y+=y_grid) // horizontal lines grid.add(Point(0,y),Point(x_size,y)); win.attach(grid); // attach our grid to our window (note grid is one object Stroustrup/Programming
  • 19. Color struct Color { // Map FLTK colors and scope them; // deal with visibility/transparency enum Color_type { red=FL_RED, blue=FL_BLUE, /* … */ }; enum Transparency { visible=0, invisible=255 }; Color(Color_type cc) :c(Fl_Color(cc)), v(visible) { } Color(int cc) :c(Fl_Color(cc)), v(visible) { } Color(Color_type cc, Transparency t) :c(Fl_Color(cc)), v(t) { } int as_int() const { return c; } Transparency visibility() { return v; } void set_visibility(Transparency t) { v = t; } private: Fl_Color c; Transparancy v; // visibility (transparency not yet implemented) }; Stroustrup/Programming
  • 20. Draw red grid grid.set_color(Color::red); Stroustrup/Programming
  • 21. Line_style struct Line_style { enum Line_style_type { solid=FL_SOLID, // ------- dash=FL_DASH, // - - - - dot=FL_DOT, // ....... dashdot=FL_DASHDOT, // - . - . dashdotdot=FL_DASHDOTDOT, // -..-.. }; Line_style(Line_style_type ss) :s(ss), w(0) { } Line_style(Line_style_type lst, int ww) :s(lst), w(ww) { } Line_style(int ss) :s(ss), w(0) { } int width() const { return w; } int style() const { return s; } private: int s; int w; }; Stroustrup/Programming
  • 22. Example: colored fat dash grid grid.set_style(Line_style(Line_style::dash,2)); Stroustrup/Programming
  • 23. Polylines struct Open_polyline : Shape { // open sequence of lines void add(Point p) { Shape::add(p); } }; struct Closed_polyline : Open_polyline { // closed sequence of lines void draw_lines() const { Open_polyline::draw_lines(); // draw lines (except the closing one) // draw the closing line: fl_line(point(number_of_points()-1).x, point(number_of_points()-1).y, point(0).x,point(0).y ); } void add(Point p) { Shape::add(p); } }; Stroustrup/Programming
  • 24. Open_polyline Open_polyline opl; opl.add(Point(100,100)); opl.add(Point(150,200)); opl.add(Point(250,250)); opl.add(Point(300,200)); Stroustrup/Programming
  • 25. Closed_polyline Closed_polyline cpl; cpl.add(Point(100,100)); cpl.add(Point(150,200)); cpl.add(Point(250,250)); cpl.add(Point(300,200)); Stroustrup/Programming
  • 26. Closed_polyline cpl.add(Point(100,250)); A Closed_polyline is not a polygon some closed_polylines look like polygons A Polygon is a Closed_polyline where no lines cross A Polygon has a stronger invariant than a Closed_polyline Stroustrup/Programming
  • 27. Text struct Text : Shape { Text(Point x, const string& s) // x is the bottom left of the first letter : lab(s), fnt(fl_font()), // default character font fnt_sz(fl_size()) // default character size { add(x); } // store x in the Shape part of the Text void draw_lines() const; // … the usual “getter and setter” member functions … private: string lab; // label Font fnt; // character font of label int fnt_sz; // size of characters }; Stroustrup/Programming
  • 28. Add text Text t(Point(200,200), &quot;A closed polyline that isn’t a polygon&quot;); t.set_color(Color::blue); Stroustrup/Programming
  • 29. Implementation: Text void Text::draw_lines() const { fl_draw(lab.c_str(), point(0).x, point(0).y); } // fl_draw() is a basic text drawing function from FLTK Stroustrup/Programming
  • 30. Color matrix Let’s draw a color matrix To see some of the colors we have to work with To see how messy two-dimensional addressing can be See Chapter 24 for real matrices To see how to avoid inventing names for hundreds of objects Stroustrup/Programming
  • 31. Color Matrix (16*16) Simple_window win20(pt,600,400,&quot;16*16 color matrix&quot;); Vector_ref<Rectangle> vr; // use like vector // but imagine that it holds references to objects for (int i = 0; i<16; ++i) { // i is the horizontal coordinate for (int j = 0; j<16; ++j) { // j is the vertical coordinate vr.push_back(new Rectangle(Point(i*20,j*20),20,20)); vr[vr.size()-1].set_fill_color(i*16+j); win20.attach(vr[vr.size()-1]); } // new makes an object that you can give to a Vector_ref to hold // Vector_ref is built using std::vector , but is not in the standard library Stroustrup/Programming
  • 32. Color matrix (16*16) More examples and graphics classes in the book (chapter 13) Stroustrup/Programming
  • 33. Next lecture What is class Shape? Introduction to object-oriented programming Stroustrup/Programming