Computing Bounds

Provided in this section are tasks for computing the geometric bounds of a graphic.

Computing Bounds Using looseFitBounds

The looseFitBounds member function takes the drawing port as a parameter and returns an IGRect2D that encloses the entire graphic, taking into account the pen information. The function estimates the geometric bounds by calling the member function IMGraphic::geometricBounds(). It alters them through IGrafBundle::alterBounds() if the graphic has a non-NIL attribute bundle. LooseFitBounds returns a bounding rectangle that is either the same or larger than the geometricBounds bounding rectangle. Use this member function to speed up quick rejects for hit testing and quick redrawing.

This is a virtual function and all concrete subclasses of IMGraphic must provide an implementation:

virtual IGRect2D looseFitBounds(const IGrafPort* port = 0) const;

Only subclasses that need to cache the data need to override the member function. Use the port if you are computing looseFitBounds for that particular port rather than the default port.

The figure shows loose fit bounds of an IMGraphic curve:

Because the implementation for looseFitBounds calls bounds on the geometry first, a small amount of inaccuracy can occur. The returned IGRect2D might enclose extra points beyond the graphic and its bundle information. The rectangle is greater than or equal to the pixels touched when you render the IMGraphic.

The function estimates bounds by taking the bounds of the convex hull of the geometry and altering the bounds by the attributes used to render the IMGraphic. The convex hull for a 2D curve is the convex polygon formed by the control points of the curve such that it completely encloses the curve. The convex hull for a simple geometry is the geometry itself. For example, the pen width of the frame or the end caps for the geometry changes the bounds.

Infinite miters can cause problems in estimating bounds. The length of the miter is a function of the angle of the joint. As the angle of the joint approaches zero degrees, the miter length approaches infinity. In such cases IMGraphic returns the miter limit instead of infinity. Infinite geometries return infinite bounds.

Computing Bounds Using geometricBounds

The geometricBounds member function represents the pure geometric bounds of the graphic. This function returns the IGRect2D for the rectangular bounds of the image without the influence of IGrafBundle. It computes the bounds in a method similar to loose fit bounds (by calculating the convex hull) without the effects of attributes. Implementation of this member function generally entails computing the bounds of the geometry held by the IMGraphic. This member function is pure virtual function.

virtual IGRect2D geometricBounds() const = 0;

The figure shows the rectangular geometric bounds of an IMGraphic curve:

 

Implementing IGraphicGroup