如何使用gnuplot在对角线中绘制矩

发布于 2025-01-22 05:03:06 字数 214 浏览 1 评论 0 原文

我在gnuplot中绘制了一个矩形,但我希望它出现在对角线中

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

I have :
     _____
    |     |
    |_____|

I want :
        _____
       /    /
      /____/

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 :
        _____
       /    /
      /____/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戈亓 2025-01-29 05:03:06

据我所知,还没有(尚未)绘制旋转矩形的简单选择。
正如@Tomsolid已经指出的那样,在一般情况下,您可以“手动”绘制多边形。

因此,如果要旋转矩形,则必须将 a = atan(dy/dx),

但是请记住,矩形只会以矩形和相同的比例出现。如果X轴和Y轴具有相同的缩放因子。您可以通过设置大小比例-1 (检查帮助> size )来执行此功能。
如果X和Y轴没有相同的缩放因素,则还可以保留旋转矩形的(视觉)比例,但这将需要更多的额外努力。

编辑:脚本更改为更通用的解决方案。旋转中心可以定义。
此外,旋转角坐标的功能( 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

结果:

”

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

残月升风 2025-01-29 05:03:06

您可以使用

set object polygon from <position> to <position> to <position>

You can use

set object polygon from <position> to <position> to <position>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文