Drawing different shapes with Imagemagick

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Posts: 1
Joined: 2019-06-08T17:38:39-07:00
Authentication code: 1152

Drawing different shapes with Imagemagick

Post by AlishDipani »

Hello everyone,

I am new to ImageMagick and am working on Rubyplot, an advanced plotting library for Ruby. I am adding ImageMagick as a backend for this library using rmagick.

I have to create a function which takes x and y coordinate as input and draws the shapes pentagon, hexagon, heptagon, octagon, stars (with the number of sides as 4/5/6/7/8). To do this polyline can be used but that would involve too much geometry as I would have to use points relative to x and y.

Is there any other way of doing this? Please suggest.

Thank you in advance!
User avatar
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Drawing different shapes with Imagemagick

Post by fmw42 »

In ImageMagick command line (last line of code) and bash shell scripting, this draws polygons, in this case 6 sides (hexagon). It does so by drawing x,y coordinates along the perimeter of a circle (given radius) equally spaced. The offset controls the starting vertex. In this case 30 deg so that one vertex is at the top. Each number of sided polygon, will need its own offset. The radius controls the size of the polygon (i.e. where the vertices lie on the circle)

Code: Select all

for ((i=0; i<num; i++)); do
ang=$((i*360/num + offset))
xx[$i]=`convert xc: -format "%[fx:$rad*cos($ang*pi/180)+$rad-0.5]" info:`
yy[$i]=`convert xc: -format "%[fx:$rad*sin($ang*pi/180)+$rad-0.5]" info:`
convert -size ${diam}x${diam} xc:black -fill white -draw "polygon ${ptArr[*]}" -alpha off \
-trim +repage -bordercolor black -border 1 hexagon.gif

Sorry, I do not know RMagick, but it is only relevant for the last convert command.
Post Reply