Saturday, October 1, 2011

Round corner rectangle in gnuplot

A simple rectangle always looks too hard while a round corner rectangle looks more comfortable. In this article I will talk about how to create an round corner rectangle in gnuplot.

There is rectangle object in gnuplot, but unfortunately there seems no option to set it a round corner one. So an absolutely new method will be used. I will use plot style "filledcurve" to create an round corner rectangle. Now come to the script.
###############################################
#Variables:
##(a,b) is the low left vertex of the rectangle
##(c,d) is the up right vertex of the rectangle
##rx is the radius along x-axis
##ry is the radius along y-axis
##x is the independent variable of the curve
f_low(a,b,c,d,rx,ry,x)=a<x && x<a+rx ? \
     -ry*sqrt(1-((x-a-rx)/rx)**2)+b+ry : \
     a+rx<x && x<c-rx ? b :c-rx<x && x<c ?\
     -ry*sqrt(1-((x-c+rx)/rx)**2)+b+ry : 1/0
#The low curve of a round corner rectangle
f_up(a,b,c,d,rx,ry,x)=a<x && x<a+rx ?\
     ry*sqrt(1-((x-a-rx)/rx)**2)+d-ry : \
     a+rx<x && x<c-rx ? d :c-rx<x && x<c ?\
     ry*sqrt(1-((x-c+rx)/rx)**2)+d-ry : 1/0
#The up curve of a round corner rectangle
###############################################
unset border    #no border
unset tics    #no tics
set sample 1000
set xrange [-10:10]
set yrange [-10:10]
set term png
set output "round_corner_rectangle.png"
plot '+' u 1:(f_low(-10,-10,10,10,1,2,$1)):\
     (f_up(-10,-10,10,10,1,2,$1)) w filledcurve\
     lc rgb"pink" notitle
#plot a round corner rectangle 
Load the plotting script, and we get picture file found_corner_rectangle.png like this.
Round corner rectangle created using filledcurve plot style in gunplot

1 comment:

Creative Commons License
Except as otherwise noted, the content of this page is licensed under a Creative Commons Attribution-NonCommercial-NoDerivs 3.0 Unported License.