The following is an example of using the Drawable subclasses with the one-by-one approach to draw the following figure:
#include <string>
#include <iostream>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int /*argc*/,char **/*argv*/)
{
try {
// Create base image (white
image of 300 by 200 pixels)
Image image( Geometry(300,200),
Color("white") );
// Set draw options
image.strokeColor("red");
// Outline color
image.fillColor("green");
// Fill color
image.strokeWidth(5);
// Draw a circle
image.draw( DrawableCircle(100,100,
50,100) );
// Draw a rectangle
image.draw( DrawableRectangle(200,200,
270,170) );
// Display the result
image.display( );
}
catch( exception &error_ )
{
cout <<
"Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
Since Drawable is an object it may be saved in an array or a list for later (perhaps repeated) use. The following example shows how to draw the same figure using the list-based approach
#include <string>
#include <iostream>
#include <list>
#include <Magick++.h>
using namespace std;
using namespace Magick;
int main(int /*argc*/,char **/*argv*/)
{
try {
// Create base image (white
image of 300 by 200 pixels)
Image image( Geometry(300,200),
Color("white") );
// Construct drawing list
std::list<Magick::Drawable>
drawList;
// Add some drawing options
to drawing list
drawList.push_back(DrawableStrokeColor("red"));
// Outline color
drawList.push_back(DrawableStrokeWidth(5));
// Stroke width
drawList.push_back(DrawableFillColor("green"));
// Fill color
// Add a Circle to drawing
list
drawList.push_back(DrawableCircle(100,100,
50,100));
// Add a Rectangle to drawing
list
drawList.push_back(DrawableRectangle(200,100,
270,170));
// Draw everything using
completed drawing list
image.draw(drawList);
// Display the result
image.display( );
}
catch( exception &error_ )
{
cout <<
"Caught exception: " << error_.what() << endl;
return 1;
}
return 0;
}
Drawable depends on the simple Coordinate structure
which represents a pair of x,y coodinates. The methods provided by
the Coordinate structure are shown in the following table:
|
|
|
|
void | Default Constructor |
double x_, double y_ | Constructor, setting first & second | |
|
double x_ | x coordinate member |
|
double y_ | y coordinate member |
The Drawable classes are shown in the following
table:
|
|
|
|
double sx_, double sy_, double rx_, double ry_, double tx_, double ty_ | Set scaling, rotation, and translation (coordinate transformation). |
|
double angle_ | Set drawing angle |
|
double startX_, double startY_, double endX_, double endY_, double startDegrees, double endDegrees_ | Draw an arc using the stroke color and based on the circle starting at coordinates startX_,startY_, and ending with coordinates endX_,endY_, and bounded by the rotational arc startDegrees_,endDegrees_ |
|
const std::list<Magick::Coordinate> &coordinates_ | Draw a bezier curve using the stroke color and based on the coordinates specified by the coordinates_ list. |
|
double originX_, double originY_, double perimX_, double perimY_ | Draw a circle using the stroke color and thickness using specified origin and perimeter coordinates. If a fill color is specified, then the object is filled. |
|
double x_, double y_, PaintMethod paintMethod_ | Color image according to paintMethod. The point method recolors the target pixel. The replace method recolors any pixel that matches the color of the target pixel. Floodfill recolors any pixel that matches the color of the target pixel and is a neighbor, whereas filltoborder recolors any neighbor pixel that is not the border color. Finally, reset recolors all pixels. |
|
double x_, double y_, const std::string &image_ | Composite current image with contents of specified image file, at specified coordinates. If the matte attribute is set to true, then the image composition will consider an alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. |
double x_, double y_, double width_, double height_, const std::string &image_ | Composite current image with contents of specified image file, rendered with specified width and height, at specified coordinates. If the matte attribute is set to true, then the image composition will consider an alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. If the specified width or height is zero, then the image is composited at its natural size, without enlargement or reduction. | |
double x_, double y_, double width_, double height_, const std::string &image_, CompositeOperator composition_ | Composite current image with contents of specified image file, rendered with specified width and height, using specified composition algorithm, at specified coordinates. If the matte attribute is set to true, then the image composition will consider an alpha channel, or transparency, present in the image file so that non-opaque portions allow part (or all) of the composite image to show through. If the specified width or height is zero, then the image is composited at its natural size, without enlargement or reduction. | |
|
DecorationType decoration_ | Set decoration to apply to text. |
|
const unsigned int* dasharray_ | Specify the pattern of dashes and gaps used to stroke paths. The strokeDashArray represents a zero-terminated array of numbers that specify the lengths of alternating dashes and gaps in pixels. If an odd number of values is provided, then the list of values is repeated to yield an even number of values. A typical strokeDashArray_ array might contain the members 5 3 2 0, where the zero value indicates the end of the pattern array. |
|
unsigned int offset_ | Specify the distance into the dash pattern to start the dash. See documentation on SVG's stroke-dashoffset property for usage details. |
|
double originX_, double originY_, double radiusX_, double radiusY_, double arcStart_, double arcEnd_ | Draw an ellipse using the stroke color and thickness, specified origin, x & y radius, as well as specified start and end of arc in degrees. If a fill color is specified, then the object is filled. |
|
const Magick::Color &color_ | Set drawing object fill color. |
|
FillRule fillRule_ | Specify the algorithm which is to be used to determine what parts of the canvas are included inside the shape. See documentation on SVG's fill-rule property for usage details. |
|
double opacity_ | Set opacity to use when drawing using fill color. |
|
const std::string &font_ | Set font to use when drawing text. |
|
GravityType gravity_ | Set text positioning gravity. |
|
double startX_, double startY_, double endX_, double endY_ | Draw a line using stroke color and thickness using starting and ending coordinates |
|
double x_, double y_, PaintMethod paintMethod_ | Change the pixel matte value to transparent. The point method changes the matte value of the target pixel. The replace method changes the matte value of any pixel that matches the color of the target pixel. Floodfill changes the matte value of any pixel that matches the color of the target pixel and is a neighbor, whereas filltoborder changes the matte value of any neighbor pixel that is not the border color, Finally reset changes the matte value of all pixels. |
|
unsigned int miterLimit_ | Specify miter limit. When two line segments meet at a sharp angle and miter joins have been specified for 'lineJoin', it is possible for the miter to extend far beyond the thickness of the line stroking the path. The miterLimit' imposes a limit on the ratio of the miter length to the 'lineWidth'. The default value of this parameter is 4. |
|
const std::list<Magick::VPath> &path_ | Draw on image using vector path. |
|
double x_, double y_ | Draw a point using stroke color and thickness at coordinate |
|
double pointSize_ | Set font point size. |
|
const std::list<Magick::Coordinate> &coordinates_ | Draw an arbitrary polygon using stroke color and thickness consisting of three or more coordinates contained in an STL list. If a fill color is specified, then the object is filled. |
|
const std::list<Magick::Coordinate> &coordinates_ | Draw an arbitrary polyline using stroke color and thickness consisting of three or more coordinates contained in an STL list. If a fill color is specified, then the object is filled. |
|
void | Pop Graphic Context. Removing the current graphic context from the graphic context stack returns the options to the values they had prior to the preceding DrawablePushGraphicContext operation. |
|
void | Push Graphic Context. When a graphic context is pushedcdc, options set after the context is pushed (such as coordinate transformations, color settings, etc.) are saved to a new graphic context. This allows related options to be saved on a graphic context "stack" in order to support heirarchical nesting of options. When DrawablePopGraphicContext is used to pop the current graphic context, the options in effect during the last DrawablePushGraphicContext operation. |
|
double upperLeftX_, double upperLeftY_, double lowerRightX_, double lowerRightY | Draw a rectangle using stroke color and thickness from upper-left coordinates to lower-right coordinates. If a fill color is specified, then the object is filled. |
|
double angle_ | Set rotation to use when drawing (coordinate transformation). |
|
double centerX_, double centerY_, double width_, double hight_, double cornerWidth_, double cornerHeight_ | Draw a rounded rectangle using stroke color and thickness, with specified center coordinate, specified width and height, and specified corner width and height. If a fill color is specified, then the object is filled. |
|
double x_, double y_ | Apply scaling in x and y direction while drawing objects (coordinate transformation). |
|
double angle_ | Apply Skew in X direction (coordinate transformation) |
|
double angle_ | Apply Skew in Y direction |
|
bool flag_ | Antialias while drawing lines or object outlines. |
|
const Color &color_ | Set color to use when drawing lines or object outlines. |
|
LineCap linecap_ | Specify the shape to be used at the end of open subpaths when they are stroked. Values of LineCap are UndefinedCap, ButtCap, RoundCap, and SquareCap. |
|
Linejoin linejoin_ | Specify the shape to be used at the corners of paths (or other vector shapes) when they are stroked. Values of LineJoin are UndefinedJoin, MiterJoin, RoundJoin, and BevelJoin. |
|
double opacity_ | Opacity to use when drawing lines or object outlines. |
|
double width_ | Set width to use when drawing lines or object outlines. |
|
double x_, double y_, std::string text_ | Annotate image with text using stroke color, font, font pointsize, and box color (text background color), at specified coordinates. If text contains special format characters the image filename, type, width, height, or other image attributes may be incorporated in the text (see label()). |
|
double x_, double y_ | Apply coordinate translation (set new coordinate origin). |
|
bool flag_ | Antialias while drawing text. |
PathCurvetoArgs( double x1_, double y1_,
double x2_, double y2_,
double x_, double y_ );
The commands are as follows:
PathQuadraticCurvetoArgs( double x1_, double y1_,
double x_, double y_ );
The quadratic Bézier commands are as follows:
PathArcArgs( double radiusX_, double radiusY_,
double xAxisRotation_, bool largeArcFlag_,
bool sweepFlag_, double x_, double y_ );
The elliptical arc commands are as follows: