Graph_paper class

19 views
Skip to first unread message

tfmccarthy_verizon_mail

unread,
May 4, 2012, 3:16:04 PM5/4/12
to ppp-p...@googlegroups.com
Here's a utility class I put together for the graphics chapters. It displays
"graph paper" on a window. I use it to help me visually verify the
exercises:

struct Graph_paper : Shape
{
int x_size; // get the size of our window
int y_size;
int x_grid; // grid increments
int y_grid;
Lines grid; // collection of grid lines
Color color_grid; // grid color
//Rectangle rect; // for background paper color

// custom initialization constructor
Graph_paper(const Window & win, int incr_x, int incr_y)
: x_size(win.x_max())
, y_size(win.y_max())
, x_grid(incr_x)
, y_grid(incr_y)
, color_grid(Color::dark_cyan)
//, rect(Point(0, 0), win.x_max(), win.y_max())
{
//rect.set_fill_color(Color::white); // background color

for (int x = x_grid; x < x_size; x += x_grid) {
grid.add(Point(x, 0), Point(x, y_size)); // vertical line
}
for (int y = y_grid; y < y_size; y += y_grid) {
grid.add(Point(0, y), Point(x_size, y)); // horizontal line
}
grid.set_color(color_grid); // lines colors
}

// enable/disable grid display
void enable(bool show)
{
Color clr(grid.color());
clr.set_visibility(show ? Color::visible : Color::invisible);
grid.set_color(clr);
}

// draw grid lines and increments
void draw_lines() const
{
if (Color::invisible == grid.color().visibility()) {
return;
}

//rect.draw_lines();
fl_color(grid.color().as_int());
grid.draw_lines();

// draw increment values
for (int x = x_grid; x < x_size; x += (x_grid * 4)) {
ostringstream os;
os << x;
Text t(Point(x, y_grid), os.str().c_str());
t.draw_lines();
}
for (int y = y_grid; y < y_size; y += (y_grid * 4)) {
ostringstream os;
os << y;
Text t(Point(x_grid, y), os.str().c_str());
t.draw_lines();
}
}
};

The class is used as the first graphics object attached to a window,

Simple_window win(Point(100,100),600,400, "Exercise 13.2.1");
Graph_paper graph_paper(win, 20, 20);
win.attach(graph_paper);

You can turn the grid on and off with the enable() function,

graph_paper.enable(false); // off
win.wait_for_button(); // Display!
graph_paper.enable(true); // on
win.wait_for_button(); // Display!

- tim


Reply all
Reply to author
Forward
0 new messages