gnuplot:如何从文件中绘制一组三角形?

gnuplot: how to draw a set of triangles from a file?

在 gnuplot 中,我们可以set object polygon根据给定的坐标绘制多边形,包括三角形。

但是如何绘制一组三角形,其坐标存储在文件中,每行的格式为 <x1> <y1> <x2> <y2> <x3> <y3>

至于rectrangles/circles,这个任务可以使用plot/with circles选项来完成,但是在gnuplot中没有with triangles选项.

一种可能的解决方案是使用with vectors绘制每条边,但是有点复杂,而且这种方法不支持颜色填充。

我想不出一种方法可以一步完成;数据格式与任何 gnuplot 绘图风格都不匹配。

一种方法是通过临时文件转换数据。这是一个适用于 5.2 及更新版本的示例。如果您使用的是较新的 gnuplot,则可以将 with polygons 替换为 with filledcurves closed

$DATA << EOD
1 1 2 2 3 1
11 11 14 14 17 11
21 21 22 22 23 21
15 5 16 6 17 5
6 6 7 7 8 6
EOD

set table "temp.dat"
plot $DATA using (sprintf("%g %g\n %g %g\n %g %g\n \n",,,,,,)) with table
unset table

unset key
set style fill solid noborder
plot "temp.dat" using 1:2 with filledcurves closed fillcolor "forest-green"

注意:我原本打算展示临时数据块而不是中间临时文件的使用,但这不起作用,因为 [=14 的格式化输出=] 不会将换行符 \n 转换为空数据块行。

编辑(显示变量颜色)

包含 RGB 颜色的额外数据字段必须存在于重新格式化数据的每一行输入中,但仅使用每个多边形的第一个顶点的值。此示例中的 sprintf 格式已被修改以相应地从原始数据文件中再现颜色(注意:十六进制整数值),其余多边形顶点中的虚拟值为零。

$DATA << EOD
1 1 2 2 3 1             0x00ffff
11 11 14 14 17 11       0x191970
21 21 22 22 23 21       0x2e8b57
15 5 16 6 17 5          0xffc020
6 6 7 7 8 6             0x8b000
EOD

set table "temp.dat"
plot $DATA using (sprintf("%g %g 0x%x\n %g %g 0\n %g %g 0\n \n",,,int(),,,,)) with table
unset table

unset key
set style fill solid noborder
plot "temp.dat" using 1:2:3 with filledcurves closed fillcolor rgb variable

我的建议与@Ethan 的建议相同。因此,这是使用 set object polygon 的替代方法。 它还需要 gnuplot>=5.2,因为它使用数据块行的索引。因此,数据应该已经在没有空行或注释行的数据块中。但是如何将文件放入数据块?

或者是这样的:

set table $Data
    plot FILE u 1:2:3:4:5:6:7 w table
unset table

或者在这里查看:

脚本:

### draw some colored triangles from a datablock
reset session

$Data <<EOD
0   0   2   1   1   2   0xff0000
5   1   3   2   4   4   0x00ff00
3   3   2   5   1   4   0x0000ff
EOD

vx(n,t)  = word($Data[n],t*2-1)     # vertex x-coordinate
vy(n,t)  = word($Data[n],t*2)       # vertex y-coordinate
color(n) = word($Data[n],7)         # triangle color

set linetype 1 lc rgb "black"
do for [n=1:|$Data|] {
    set obj n polygon from vx(n,1),vy(n,1) to vx(n,2),vy(n,2) to vx(n,3),vy(n,3) to vx(n,1),vy(n,1)
    set obj n fc rgb color(n) fs solid 0.5 border lt 1 lw 3
}

set size square
set xrange[0:5]
set yrange[0:5]

plot NaN notitle    # or plot something else
### end of script

结果:

加法:

或者,类似于 Ethan 的解决方案,绘制 三角形而不是 绘制 三角形,但不使用磁盘上的临时文件(但数据块)。结果与上图相同。我还没有测试绘图或绘图是否更快 and/or 如果你想要 draw/plot 数千个三角形更有效。

脚本:

### plot some colored triangles from a datablock
reset session

$Data <<EOD
0   0   2   1   1   2   0xff0000
5   1   3   2   4   4   0x00ff00
3   3   2   5   1   4   0x0000ff
EOD

vx(n,t)  = word($Data[n],t*2-1)     # vertex x-coordinate
vy(n,t)  = word($Data[n],t*2)       # vertex y-coordinate
color(n) = word($Data[n],7)         # triangle color

set print $Triangles
   do for [n=1:|$Data|]  {
       print sprintf("%s %s %s\n%s %s 0\n%s %s 0\n%s %s 0\n\n", \
                     vx(n,1),vy(n,1),color(n), vx(n,2),vy(n,2), vx(n,3),vy(n,3), vx(n,1),vy(n,1))
   }
set print

set size square
set xrange[0:5]
set yrange[0:5]
set linetype 1 lc rgb "black" lw 3
set style fill solid 0.5

plot $Triangles u 1:2:3 w filledcurves lc rgb var notitle
### end of script