### Creating a Graphics Class

The following sample class derived from IMGraphic creates a special 2D primitive to define the top view of a bolt. This class stores a transformation matrix for a local coordinate system. (This example does not take into account performance and efficiency.)

The figure shows the results of the class defining the top view of the bolt. (Dimensioning and labels are not part of IMGraphic.)

class IBoltTop : public IMGraphic {
public:
IBoltTop(GCoord BoltDiameter, GCoord outerRadius, IGPoint2D center);

IBoltTop(const IBoltTop&);
IBoltTop& operator= (const IBoltTop&);
virtual void draw(IGrafPort&) const;
virtual IGPoint2D alignmentBasePoint() const;
virtual IGRect2D geometricBounds() const;
virtual void transformBy(const IGrafMatrix& matrix);
private:
IBoltTop(); // For streaming purposes only.
IGrafMatrix fMatrix;
IGPolygon2D fPolygon; // This is the outer polygon
IGEllipse2D fCircle; // This is the inner circle
void computePolygon(GCoord outerRad, int numOfSides);
};
IBoltTop::IBoltTop()
{
}
IBoltTop::IBoltTop(GCoord boltDia, GCoord outerDia, IGPoint2D center)
: fCircle(boltDia, center)
// calculate the hexagon polygon from these paramters
// The side of the polygon = outerDiameter / 2.0
IGPoint2DArray polygonPoints(6);
IGPoint2D tmpPoint;
for (unsigned long i = 0, theta = 0.0; i < 6; i ++, theta += kPi/6)
tmpPoint.fX = center.fX + outerDia * sin(theta);
tmpPoint.fY = center.fY + outerDia * cos(theta);
polygonPoints.setPoint(i, tmpPoint);
}
}
void IBoltTop::draw(IGrafPort &port) const
{
/*
* draw the geometry with the grafGrafBundle and the matris
* associated with this primitive
*/
port.draw(fPolygon, fGrafBundle, fMatrix);
port.draw(fCircle, fGrafBundle, fMatrix);
}
IGPoint2D IBoltTop::alignmentBasePoint() const
{
// The allignment point is the center of the circle.
IGPoint2D point;

point.x = fCircle.centerX();
point.y = fCircle.centerY();
return point;
}
IGRect2D IBoltTop::geometricBounds() const
{
// Get bounds of the polygon
// pass the bounds to the bundle for altering.
bounds = fPolygon.bounds();
}

void IBoltTop::transformBy(const IGrafMatrix& matrix)|
{
fMatrix.concatWith(matrix);
}

Using IMGraphic Constructors

Using IMGraphic Destructors