The SimpleCxxLib package


#include "gobjects.h"

class GPolygon : public GObject

This graphical object subclass represents a polygon bounded by line segments. The GPolygon constructor creates an empty polygon. To complete the figure, you need to add vertices to the polygon using the methods addVertex, addEdge, and addPolarEdge. As an example, the following code adds a filled red octagon to the center of the window:
   int main() {
      GWindow gw;
      cout << "This program draws a red octagon." << endl;
      double edge = 75;
      GPolygon *stopSign = new GPolygon();
      stopSign->addVertex(-edge / 2, edge / 2 + edge / sqrt(2.0));
      for (int i = 0; i < 8; i++) {
         stopSign->addPolarEdge(edge, 45 * i);
      }
      stopSign->setFilled(true);
      stopSign->setColor("RED");
      gw.add(stopSign, gw.getWidth() / 2, gw.getHeight() / 2);
      return 0;
   }

The program results in the following picture:

StopSign
Constructor
GPolygon() Constructs a new empty polygon at the origin.
Methods
addEdge(dx, dy) Adds an edge to the polygon whose components are given by the displacements dx and dy from the last vertex.
addPolarEdge(r, theta) Adds an edge to the polygon specified in polar coordinates.
addVertex(x, y) Adds a vertex at (x, y) relative to the polygon origin.
getFillColor() Returns the color used to display the filled region of this polygon.
getVertices() Returns a vector of the points in the polygon.
isFilled() Returns true if the polygon is filled.
setFillColor(color) Sets the color used to display the filled region of this polygon.
setFilled(flag) Sets the fill status for the polygon, where false is outlined and true is filled.

Constructor detail


GPolygon();
Constructs a new empty polygon at the origin.

Usage:

GPolygon *poly = new GPolygon();

Method detail


void addVertex(double x, double y);
Adds a vertex at (x, y) relative to the polygon origin.

Usage:

poly->addVertex(x, y);

void addEdge(double dx, double dy);
Adds an edge to the polygon whose components are given by the displacements dx and dy from the last vertex.

Usage:

poly->addEdge(dx, dy);

void addPolarEdge(double r, double theta);
Adds an edge to the polygon specified in polar coordinates. The length of the edge is given by r, and the edge extends in direction theta, measured in degrees counterclockwise from the +x axis.

Usage:

poly->addPolarEdge(r, theta);

Vector<GPoint> getVertices() const;
Returns a vector of the points in the polygon.

Usage:

Vector<GPoint> vec = poly->getVertices();

void setFilled(bool flag);
Sets the fill status for the polygon, where false is outlined and true is filled.

Usage:

poly->setFilled(flag);

bool isFilled() const;
Returns true if the polygon is filled.

Usage:

if (poly->isFilled()) ...

void setFillColor(string color);
void setFillColor(int rgb);
Sets the color used to display the filled region of this polygon.

Usage:

poly->setFillColor(color);

string getFillColor() const;
Returns the color used to display the filled region of this polygon. If none has been set, getFillColor returns the empty string.

Usage:

string color = poly->getFillColor();