Search code examples
gnuplot

How to draw a rect in the diagonal using gnuplot


I draw a rect in gnuplot but i want it to appear in the diagonal

set object 1 rect from 53.1035,126.27 to 133.104,146.27

I have :
     _____
    |     |
    |_____|

I want :
        _____
       /    /
      /____/

Solution

  • As far as I know there is not (yet) a simple option to draw a rotated rectangle. And as @TomSolid already pointed out that in the general case you can draw a polygon "by hand".

    So, if you want to rotate your rectangle you have to apply the rotation matrix to your rectangle coordinates. In the example below the rotation angle is 45 degrees. If you want to rotate it by the angle of the diagonal, define the rotation angle as a = atan(dy/dx)

    However, keep in mind the rectangle will only appear as rectangle and in the same proportions if the x- and y-axes have the same scaling factors. You can enforce this by set size ratio -1 (check help size). If x- and y-axes do not have the same scaling factors, you can also keep the (visual) proportions for your rotated rectangle, but it will require a bit more extra effort.

    Edit: Script changed to a more general solution. Rotation center can be defined. Furthermore, the functions for the rotated corner coordinates (x1r(n), y1r(n), ...) are actually not a function of n, but this way the functions will take the current values of x0,y0,x1,y1 at the time of calling.

    Script:

    ### draw a rotated rectangle
    reset session
    
    x0 = 53.1035
    y0 = 126.27
    x1 = 133.104
    y1 = 146.27
    
    set obj 1 rect from x0,y0 to x1,y1 
    set obj 1 fs empty border rgb "red"
    
    set size ratio -1
    
    set angle degrees
    a  = 45     # rotation angle
    xc = x0     # rotation around bottom...
    yc = y0     # ...left corner x0,y0
    
    # functions for rotation of rectangle corner coordinates
    x1r(n) = xc+(x0-xc)*cos(a)-(y0-yc)*sin(a)
    y1r(n) = yc+(x0-xc)*sin(a)+(y0-yc)*cos(a)
    x2r(n) = xc+(x1-xc)*cos(a)-(y0-yc)*sin(a)
    y2r(n) = yc+(x1-xc)*sin(a)+(y0-yc)*cos(a)
    x3r(n) = xc+(x1-xc)*cos(a)-(y1-yc)*sin(a)
    y3r(n) = yc+(x1-xc)*sin(a)+(y1-yc)*cos(a)
    x4r(n) = xc+(x0-xc)*cos(a)-(y1-yc)*sin(a)
    y4r(n) = yc+(x0-xc)*sin(a)+(y1-yc)*cos(a)
    
    set obj 2 polygon from x1r(0),y1r(0) to x2r(0),y2r(0) to x3r(0),y3r(0) to x4r(0),y4r(0) to x1r(0),y1r(0)
    set obj 2 fs empty border rgb "blue"
    
    a = atan(real(y1-y0)/(x1-x0))       # real() to avoid potential integer division
    xc = (x0+x1)/2.                     # rotation around...
    yc = (y0+y1)/2.                     # ...rectangle center
    
    set obj 3 polygon from x1r(0),y1r(0) to x2r(0),y2r(0) to x3r(0),y3r(0) to x4r(0),y4r(0) to x1r(0),y1r(0)
    set obj 3 fs empty border rgb "green"
    
    set xrange [0:150]
    set yrange [100:200]
    set key top left
    
    plot NaN notitle, \
         keyentry w l lc "red" ti "original rectangle", \
         keyentry w l lc "blue" ti "rotated by 45° around x0,y0", \
         keyentry w l lc "green" ti "rotated by diagonal around center"
    ### end of script
    

    Result:

    enter image description here