Page 1 of 1

DrawPolygon doesn't seem to work for me. Where is my error?

Posted: 2009-09-08T14:16:33-07:00
by JohnF
I'm trying to port an old program of mine to use MagickWand for drawing, but I can't get DrawPolygon to work.
The following code:

Code: Select all

#define WIDTH  4000
#define HEIGHT  3000

void mwtest(void)
  {
  MagickWand   *image;
  PixelWand    *colour;
  DrawingWand  *drawing;
  PointInfo ptest[5] = {{1000, 750}, {3000, 750}, {3000, 2250}, {1000, 2250}, {1000, 750}}; 

  MagickWandGenesis();
  image  = NewMagickWand(); 
  colour = NewPixelWand();
  PixelSetColor(colour, "rgb(200, 200, 255)");
  if (!MagickNewImage(image, WIDTH, HEIGHT, colour)) return;
  drawing = NewDrawingWand();
  PixelSetColor(colour, "black");
  DrawSetStrokeColor(drawing, colour);
  PixelSetColor(colour, "white");
  DrawSetFillColor(drawing, colour);
  DrawPolygon(drawing,5,ptest);
  MagickWriteImage(image, "mwtest.png");
  MagickWandTerminus();
  }
doesn't do what I expect. I get the image created and filled with the background colour, but I don't see the polygon.

I'm sure I'm making some fundamental mistake, but I can't see what it is ...

Re: DrawPolygon doesn't seem to work for me. Where is my error?

Posted: 2009-09-08T14:38:56-07:00
by JohnF
I was right - it was something fundamental :-(

There was no association between the drawing commands and the image.
A call to MagickDrawImage solved that.

Re: DrawPolygon doesn't seem to work for me. Where is my error?

Posted: 2009-09-09T00:12:16-07:00
by JohnF
Further update - there appears to be an n^2 algorithm somewhere in the DrawPolygon/MagickDrawImage process.

I've got several thousand polygons that I want to render (some with many thousands of points).
If I call DrawPolygon multiple times, and only call MagickDrawImage once at the end, it takes forever
(I don't know how long - I don't have the patience to find out).
If, however, I call MagickDrawImage once every couple of hundred polygons or so (and then
re-initialise the drawing wand) everything completes in a reasonable amount of time.

Is this to be expected? Is the subdivision approach that I use (breaking the full list into smaller chunks)
a reasonable workaround? And if so, are there any guidelines as to what is a reasonable number of
polygons (or line segments) to render with a call to MagickDrawImage?

Re: DrawPolygon doesn't seem to work for me. Where is my error?

Posted: 2009-09-09T05:30:31-07:00
by magick
The rendering algorithm is O(n^2). The only expected speed-ups are shorter point lists in the polygon or use a multi-core system where ImageMagick renders one point per core simultaneously. An alternative is to generate SVG polygons and render with rsvg which is likely O(n).