Page 1 of 1

### Drawing different shapes with Imagemagick

Posted: 2019-06-08T18:27:28-07:00
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!

### Re: Drawing different shapes with Imagemagick

Posted: 2019-06-08T18:41:41-07:00
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

``````num=6
rad=150
diam=\$((2*rad))
offset=30
ptArr=()
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:`
ptArr[\$i]="\${xx[\$i]},\${yy[\$i]}"
done
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.