SimpleCxxLib packageclass GPolygon : public GObjectGPolygon 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:
| Constructor | |
| Constructs a new empty polygon at the origin. | |
| Methods | |
Adds an edge to the polygon whose components are given by the displacements dx and dy from the last vertex. | |
| Adds an edge to the polygon specified in polar coordinates. | |
Adds a vertex at (x, y) relative to the polygon origin. | |
| Returns the color used to display the filled region of this polygon. | |
| Returns a vector of the points in the polygon. | |
Returns true if the polygon is filled. | |
| Sets the color used to display the filled region of this polygon. | |
Sets the fill status for the polygon, where false is outlined and true is filled. | |
GPolygon();
Usage:
GPolygon *poly = new GPolygon();
void addVertex(double x, double y);
x, y) relative to the polygon
origin.
Usage:
poly->addVertex(x, y);
void addEdge(double dx, double dy);
dx and dy from the last vertex.
Usage:
poly->addEdge(dx, dy);
void addPolarEdge(double r, double theta);
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;
Usage:
Vector<GPoint> vec = poly->getVertices();
void setFilled(bool flag);
false is
outlined and true is filled.
Usage:
poly->setFilled(flag);
bool isFilled() const;
true if the polygon is filled.
Usage:
if (poly->isFilled()) ...
void setFillColor(string color); void setFillColor(int rgb);
Usage:
poly->setFillColor(color);
string getFillColor() const;
getFillColor returns the empty string.
Usage:
string color = poly->getFillColor();