As mentioned in Section 3.3, the
window's paint
method is responsible for drawing
graphical images. These images are rendered within the
virtual viewport. The SGL supports the following drawing
primitives:
plotting a point
drawing a straight line
drawing a dotted or dashed straight line
drawing a rectangle
drawing a filled rectangle
drawing a circle
drawing a filled circle
drawing a polygon
drawing a filled polygon
drawing graphical text
plotting an arbitrary mathematical function.
The details of these drawing functions are provided in the SGL documentation.
Example 3.2 draws a picture of a clock using several of the graphics drawing functions:
Example 3.2. Clock Picture
#include <GL/sgl.hpp> class ClockWindow: public sgl::Window { public: ClockWindow(): sgl::Window("3:00", 100, 100, 300, 300, -100.0, 100.0, -100.0, 100.0) {} void paint() override { set_color(sgl::BLUE); // Draw outline of clock sgl::draw_circle(0.0, 0.0, 45.0); // Center at (0,0), radius of 45 // Draw hour hand sgl::set_line_width(4); // Thicker line sgl::draw_line(0.0, 0.0, 25.0, 0.0); // Shorter line // Draw minute hand sgl::set_line_width(2); // Thinner line sgl::draw_line(0.0, 0.0, 0.0, 40.0); // Longer line } }; int main() { // Create a window instance and execute it sgl::run<ClockWindow>(); }
The clock consists of a circle and two lines. The circle is
centered at the center of the window. Figure 3.4
shows the running program.
Copyright ©2019 Richard L. Halterman | Version 0.9.5 | February 17, 2019 |