如何使用 gnuplot 在对角线上绘制矩形
How to draw a rect in the diagonal using gnuplot
我在 gnuplot 中画了一个矩形,但我希望它出现在对角线上
set object 1 rect from 53.1035,126.27 to 133.104,146.27
I have :
_____
| |
|_____|
I want :
_____
/ /
/____/
您可以使用
set object polygon from <position> to <position> to <position>
据我所知,(还)没有一个简单的选项来绘制旋转的矩形。
正如@TomSolid 已经指出的那样,在一般情况下,您可以“手动”绘制多边形。
因此,如果您想旋转矩形,则必须将 rotation matrix 应用于矩形坐标。在下面的示例中,旋转角度为 45 度。如果要旋转对角线的角度,定义旋转角度为a = atan(dy/dx)
但是,请记住,只有当 x- 和 y-axes 具有相同的比例因子时,矩形才会以相同的比例显示为矩形。您可以通过 set size ratio -1
(检查 help size
)强制执行此操作。
如果 x- 和 y-axes 没有相同的比例因子,您也可以保持旋转矩形的(视觉)比例,但这需要更多的额外努力。
编辑: 脚本更改为更通用的解决方案。可以定义旋转中心。
此外,旋转角坐标(x1r(n), y1r(n), ...
)的函数实际上不是n
的函数,但这样函数在调用时将采用x0,y0,x1,y1
的当前值。
脚本:
### 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
结果:
我在 gnuplot 中画了一个矩形,但我希望它出现在对角线上
set object 1 rect from 53.1035,126.27 to 133.104,146.27
I have :
_____
| |
|_____|
I want :
_____
/ /
/____/
您可以使用
set object polygon from <position> to <position> to <position>
据我所知,(还)没有一个简单的选项来绘制旋转的矩形。 正如@TomSolid 已经指出的那样,在一般情况下,您可以“手动”绘制多边形。
因此,如果您想旋转矩形,则必须将 rotation matrix 应用于矩形坐标。在下面的示例中,旋转角度为 45 度。如果要旋转对角线的角度,定义旋转角度为a = atan(dy/dx)
但是,请记住,只有当 x- 和 y-axes 具有相同的比例因子时,矩形才会以相同的比例显示为矩形。您可以通过 set size ratio -1
(检查 help size
)强制执行此操作。
如果 x- 和 y-axes 没有相同的比例因子,您也可以保持旋转矩形的(视觉)比例,但这需要更多的额外努力。
编辑: 脚本更改为更通用的解决方案。可以定义旋转中心。
此外,旋转角坐标(x1r(n), y1r(n), ...
)的函数实际上不是n
的函数,但这样函数在调用时将采用x0,y0,x1,y1
的当前值。
脚本:
### 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
结果: