# star class colored [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 19
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 19

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

struct Star : Closed_polyline
{
{
s = (s < 3) ? segments : 3;
generate_points();
};
Star(Point origin, int radius, int segments)
{
s = (s < 3) ? segments : 3;
generate_points();
};

void generate_points();
void draw_lines() const;

private:
Point o;
int r;
int ir;
int s;
Fl_Color lc;
};

void Star::generate_points()
{
for (int i = 1; i < s + 1; i++) {
add(Point(r * sin(((2 * M_PI) / s) * i) + o.x, r * cos(((2 * M_PI) / s) * i) + o.y));
add(Point(ir * sin(((2 * M_PI) / s) * i + ((2 * M_PI) / (s * 2))) + o.x, ir * cos(((2 * M_PI) / s) * i + ((2 * M_PI) / (s * 2))) + o.y));
}
};

void Star::draw_lines() const
{
fl_color(lc);
Closed_polyline::draw_lines();
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

Star star1(center, 50, 20, 8, FL_RED);
star1.set_fill_color(Color::red);

Star star2(Point(100,300), 70, 20, 10, FL_RED);
star2.set_fill_color(Color::blue);

Star star3(Point(600, 200), 20, 100, 16, FL_RED);
star3.set_fill_color(Color::green);

win.attach(star1);
win.attach(star2);
win.attach(star3);

win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# polygon class point check [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 18
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 18

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

struct Poly : Closed_polyline
{
Poly(vector<Point> pts, Fl_Color linecolor)
: pts(pts), lc(linecolor)
{
if (pts.size() <= 2) {
error("Not enough points", pts.size());
}
else {
generate_points();
}
};

void generate_points();
void draw_lines() const;

private:
vector<Point> pts;
Fl_Color lc;
};

void Poly::generate_points()
{
for (int i = 0; i < pts.size(); i++) {
}
};

void Poly::draw_lines() const
{
fl_color(lc);
Closed_polyline::draw_lines();
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

vector<Point> pts = { Point(100, 100), Point(150, 200) };

Poly p(pts, FL_RED);

win.attach(p);
win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# hexagon tiling colored [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 17
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 17

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

struct Regular_hexagon : Closed_polyline
{
Regular_hexagon(Point origin, int radius, Fl_Color lc)
{
generate_points();
};

void generate_points();
void draw_lines() const;
vector<Point> get_pts() { return pts; };

private:
Fl_Color lc;
Point o;
vector<Point> pts;
int r;
};

void Regular_hexagon::generate_points()
{
int n = 6;
for (int i = 0; i < n; i++) {
pts.push_back(Point(o.x + r * cos(2 * M_PI * i / n), o.y + r * sin(2 * M_PI * i / n)));
}
};

void Regular_hexagon::draw_lines() const
{
fl_color(lc);
Closed_polyline::draw_lines();
};

struct rh_tiling : Shape
{
rh_tiling(Point basepoint, double radius, int num_xdir, int num_ydir)
{
nx = (num_xdir == 0) ? 1 : num_xdir;
ny = (num_ydir == 0) ? 1 : num_ydir;
generate_pts();
}

void generate_pts();
void draw_lines() const;

private:
vector<Point> pts;
Point bp;
double r;
int nx;
int ny;
};

void rh_tiling::generate_pts()
{
double triangle_h = sqrt(pow(r, 2) - pow(r / 2, 2));
cout << triangle_h << endl;
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
if (j % 2 == 0 || j == 0) {
pts.push_back(Point(bp.x + (r * 3) * i, bp.y + triangle_h * j));
}
else {
pts.push_back(Point(bp.x + (r * 3) * i + (1.5 * r), bp.y + j * triangle_h));
}
}
}
};

void rh_tiling::draw_lines() const
{
for (int i = 0; i < pts.size(); i++) {
Regular_hexagon(pts[i], r, Fl_Color(i)).draw_lines();
}
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

rh_tiling myTiling(Point(50, 100), 50, 5, 6);
myTiling.set_color(Color::red);

win.attach(myTiling);
win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# hexagon tiling again [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 16
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).
For some reason Exercise 16 in Chapter 13 is exactly the same as Exercise 9, so here you go again: (except I missunderstood, if so please let me know)

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 16

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

struct Regular_hexagon : Closed_polyline
{
{
generate_points();
};

void generate_points();
void draw_lines() const;
vector<Point> get_pts() { return pts; };

private:
Point o;
vector<Point> pts;
int r;
};

void Regular_hexagon::generate_points()
{
int n = 6;
for (int i = 0; i < n; i++) {
pts.push_back(Point(o.x + r * cos(2 * M_PI * i / n), o.y + r * sin(2 * M_PI * i / n)));
}
};

void Regular_hexagon::draw_lines() const
{
Closed_polyline::draw_lines();
};

struct rh_tiling : Shape
{
rh_tiling(Point basepoint, double radius, int num_xdir, int num_ydir)
{
nx = (num_xdir == 0) ? 1 : num_xdir;
ny = (num_ydir == 0) ? 1 : num_ydir;
generate_pts();
}

void generate_pts();
void draw_lines() const;

private:
vector<Point> pts;
Point bp;
double r;
int nx;
int ny;
};

void rh_tiling::generate_pts()
{
double triangle_h = sqrt(pow(r, 2) - pow(r / 2, 2));
cout << triangle_h << endl;
for (int i = 0; i < nx; i++) {
for (int j = 0; j < ny; j++) {
if (j % 2 == 0 || j == 0) {
pts.push_back(Point(bp.x + (r * 3) * i, bp.y + triangle_h * j));
}
else {
pts.push_back(Point(bp.x + (r * 3) * i + (1.5 * r), bp.y + j * triangle_h));
}
}
}
};

void rh_tiling::draw_lines() const
{
for (int i = 0; i < pts.size(); i++) {
Regular_hexagon(pts[i], r).draw_lines();
}
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

rh_tiling myTiling(Point(50,100),50,5,6);
myTiling.set_color(Color::red);

win.attach(myTiling);
win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# right triangle tiling [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 15
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 15

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

Point operator-(Point& p1, Point& p2) {
Point p3 = Point(p1.x - p2.x, p1.y - p2.y);
return p3;
}

Point operator+(Point& p1, Point& p2) {
Point p3 = Point(p1.x + p2.x, p1.y + p2.y);
return p3;
}

double pointVecMag(Point vec) {
return sqrt(pow(vec.x, 2) + pow(vec.y, 2));;
}

struct Regular_triangle : Closed_polyline
{
Regular_triangle(Point origin, int edge, int height, Fl_Color linecolor)
: o(origin), e(edge), h(height), lc(linecolor)
{
generate_points();
};

void generate_points();
void draw_lines() const;

private:
Point o;
int e;
int h;
Fl_Color lc;
};

void Regular_triangle::generate_points()
{
add(Point(o.x + e / 2, o.y - h));
};

void Regular_triangle::draw_lines() const
{
fl_color(lc);
Closed_polyline::draw_lines();
};

{
Point origin_Pt1 = Regular_triangle::point(0) - Regular_triangle::point(2);
Point origin_Pt2 = Regular_triangle::point(1) - Regular_triangle::point(2);

Regular_triangle::set_point(0, rotated_Pt1 + Regular_triangle::point(2));
Regular_triangle::set_point(1, rotated_Pt2 + Regular_triangle::point(2));
};

struct rt_tiling : Shape
{
rt_tiling(Point basepoint, int edge, int height, int xcount, int ycount)
:bp(basepoint), e(edge), h(height)
{
xc = (xcount == 0) ? 1 : xcount;
yc = (ycount == 0) ? 1 : ycount;
generate_pts();
}

void draw_lines() const;
void generate_pts();

private:
vector<Point> pts;
Point bp;
int e;
int h;
int xc;
int yc;
};

void rt_tiling::generate_pts() {
for (int i = 0; i < yc; i++) {
for (int j = 0; j < xc; j++) {
if (i % 2 == 0) {
if (j % 2 == 0) {
pts.push_back(Point(bp.x + (e / 2) * j, bp.y + h * i));
}
else {
pts.push_back(Point(bp.x + (e / 2) * j, bp.y + h * i + h));
}
}
else {
if (j % 2 == 0) {
pts.push_back(Point(bp.x + (e / 2) * j, bp.y + h * i + h));
}
else {
pts.push_back(Point(bp.x + (e / 2) * j, bp.y + h * i));
}
}
}
}
}

void rt_tiling::draw_lines() const
{
for (int i = 0; i < xc; i++) {
for (int j = 0; j < yc; j++) {
Regular_triangle rt(pts[i + j * xc], e, h, Color::red);
if (j % 2 == 0) {
if (i % 2 != 0 && j % 2 == 0) {
rt.rotate_triangle(M_PI);
}
}
else {
if (i % 2 == 0 && j % 2 != 0) {
rt.rotate_triangle(M_PI);
}
}
rt.draw_lines();
}
}
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

int edge = 50;
int height = 0.5 * (1 + sqrt(2.0)) * edge; //r = 1/2 * ( 1 + sqrt(2)) * s

rt_tiling myRtTiling(Point(10, 100), edge, height, 20, 5);

win.attach(myRtTiling);
win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# right triangle class octagon [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 14
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 14

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

Point operator-(Point& p1, Point& p2) {
Point p3 = Point(p1.x - p2.x, p1.y - p2.y);
return p3;
}

Point operator+(Point& p1, Point& p2) {
Point p3 = Point(p1.x + p2.x, p1.y + p2.y);
return p3;
}

double pointVecMag(Point vec) {
return sqrt(pow(vec.x, 2) + pow(vec.y, 2));;
}

struct Regular_triangle : Closed_polyline
{
Regular_triangle(Point origin, int edge, int height, Fl_Color linecolor)
: o(origin), e(edge), h(height), lc(linecolor)
{
generate_points();
};

void generate_points();
void draw_lines() const;

private:
Point o;
int e;
int h;
Fl_Color lc;
};

void Regular_triangle::generate_points()
{
add(Point(o.x + e / 2, o.y - h));
};

void Regular_triangle::draw_lines() const
{
fl_color(lc);
Closed_polyline::draw_lines();
};

{
Point origin_Pt1 = Regular_triangle::point(0) - Regular_triangle::point(2);
Point origin_Pt2 = Regular_triangle::point(1) - Regular_triangle::point(2);

Regular_triangle::set_point(0, rotated_Pt1 + Regular_triangle::point(2));
Regular_triangle::set_point(1, rotated_Pt2 + Regular_triangle::point(2));
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

int edge = 50;
int height = 0.5 * (1 + sqrt(2.0)) * edge; //r = 1/2 * ( 1 + sqrt(2)) * s

Vector_ref<Regular_triangle> rt;

for (int i = 0; i < 8; i++) {
rt.push_back(new Regular_triangle(center, edge, height, Fl_Color(i)));
rt[i].rotate_triangle(((2 * M_PI) / 8) * i);
win.attach(rt[i]);
}

win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# color matrix from 13.10 [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 13
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 13

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

Vector_ref<Graph_lib::Rectangle> rect;
Graph_lib::Rectangle x(Point(100, 200), Point(200, 300));

rect.push_back(x);
rect.push_back(new Graph_lib::Rectangle(Point(50, 60), Point(80, 90)));

for (int i = 0; i < rect.size(); i++) rect[i].move(10, 10);

Vector_ref<Graph_lib::Rectangle> vr;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
vr.push_back(new Graph_lib::Rectangle(Point(i * 20, j * 20), 20, 20));
vr[vr.size() - 1].set_fill_color(Color(i * 16 + j));
win.attach(vr[vr.size() - 1]);
}
}

win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# point on circle [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 12
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 12

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

Point ptOnCircle(Graph_lib::Circle& c, double p) {
return Point(c.radius() * cos(M_PI * 2 * p) + c.center().x, c.radius() * sin(M_PI * 2 * p) + c.center().y);
}

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

//Circle
Circle myCircle(center, 50);
myCircle.set_color(Color::red);
win.attach(myCircle);

//Pt on Circle
double parameter = 0;
while (true) {
Mark myMark(ptOnCircle(myCircle, parameter), 'x');
myMark.set_color(Color::red);
win.attach(myMark);
win.wait_for_button();
parameter += 0.1;
}
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# ellipse foci class [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 11
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 11

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

Point ptOnEllipse(Graph_lib::Ellipse& e, double p) {
int a = e.major(); //width
int b = e.minor(); //height

int x = e.center().x + a * cos(M_PI * 2 * p);
int y = e.center().y - b * sin(M_PI * 2 * p);

return Point(x, y);
}

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 1000, 800, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

//Ellipse
Graph_lib::Ellipse myEllipse(center,300, 200);
myEllipse.set_color(Color::red);
//Foci
Mark focus1(myEllipse.focus1(), 'x');
focus1.set_color(Color::red);
Mark focus2(myEllipse.focus2(), 'x');
focus2.set_color(Color::red);
//Pt on Ellipse
double ptParameter = 0.7;
Point pt = ptOnEllipse(myEllipse, ptParameter);
Mark markOnEllipse(pt, 'x');
markOnEllipse.set_color(Color::red);

//Lines
Lines fociLines;
fociLines.set_color(Color::blue);

//xyAxis
Axis xa(Axis::x, center, 400, 20, "x axis");
xa.set_color(Color::black);
Axis ya(Axis::y, center, 300, 15, "y axis");
ya.set_color(Color::black);

win.attach(myEllipse);
win.attach(focus1);
win.attach(focus2);
win.attach(markOnEllipse);
win.attach(fociLines);
win.attach(xa);
win.attach(ya);

win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```

# regular polygon class [fltk]

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 13 Exercise 10
Using GUI library called FLTK (Fast Light Tool Kit, “full tick”).

```Output:

```
```//  Philipp Siedler
//  Bjarne Stroustrup's PP
//  Chapter 13 Exercise 10

#define _USE_MATH_DEFINES
#include "Simple_window.h"
#include "Graph.h"
#include <cmath>

using namespace Graph_lib;

ostream& operator<<(ostream& out, Point& a) {
out << "(" << a.x << "," << a.y << ")";
return out;
}

struct Regular_polygon : Closed_polyline
{
Regular_polygon(Point origin, int radius, int segments, Fl_Color linecolor)
{
s = (s < 3) ? segments : 3;
generate_points();
};
Regular_polygon(Point origin, int radius, int segments)
{
s = (s < 3) ? segments : 3;
generate_points();
};

void generate_points();
void draw_lines() const;

private:
Point o;
int r;
int s;
Fl_Color lc;
};

void Regular_polygon::generate_points()
{
for (int i = 1; i < s + 1; i++) {
add(Point(r * sin(((2 * M_PI) / s) * i) + o.x, r * cos(((2 * M_PI) / s) * i) + o.y));
}
};

void Regular_polygon::draw_lines() const
{
fl_color(lc);
Closed_polyline::draw_lines();
};

int main()
try
{
Point tl(100, 100);
Simple_window win(tl, 720, 400, "Simple Window");
Point center(win.x_max() / 2, win.y_max() / 2);

Regular_polygon h(center, 50, 8, FL_RED);

win.attach(h);
win.wait_for_button();
}
catch (exception& e) {
cout << e.what() << endl;
return 1;
}
catch (...) {
cout << "Exiting" << endl;
return 2;
}
```