gnuplot:如何从顶点绘制多边形/轮廓 [英] Gnuplot: how to draw polygon/contour from its vertices
问题描述
我的data.txt文件包含形成多边形段的点的2D坐标.这些坐标随着时间的推移而发展.该文件的结构如下:
My data.txt file contains the 2D coordinates of points forming a segment of a polygon. These coordinates are evolving over time. The file is structured like this:
itr nbr_pts p1.x p1.y ...... pk.x pk.y
(itr+1) ..........
.....
其中,pk是多边形的第k个点/顶点,而nb_pts是顶点数.
where pk is the k-th point/vertex of the polygon and nb_pts is the number vertices.
我的问题是如何以一定的迭代次数(行)从其顶点(p1,p2,... pk)绘制2D多边形?
My question is how to draw the 2D polygon from its vertices (p1, p2, ...pk) at a certain iteration (row)?
此外,请注意,不仅有一个数据文件/多边形,还有N个:data1.txt .... dataN.txt
In addition, note that there is not only one data file/polygon but N ones: data1.txt .... dataN.txt
我尝试了类似的操作,但是没有用(Nbr个文件= 6)
I tried something like this but did not work (Nbr of files =6)
N = 6
set multiplot
plot for [i=0:N-1] polygon_i = sprintf("%s/data%d.dat",filename, i) polygon_i val=$2 for [j=1:$2] u (j+1):(j+1+1) w lines
我知道有多少个多边形/文件(本例中为6个),但是我对每个文件中的列数没有先验知识.顶点的数量可以从一个多边形到另一个多边形.
I know how many polygones/files there is (6 in this cae), but I have no prior knowledge on the number of columns in each file; the number of vertices can vary from a polygone to another.
请问有什么主意吗?
推荐答案
我的想法是需要对文件结构进行修改.对于每个迭代时间,都有一个包含多边形顶点的x
和y
坐标的块:
The idea I have would need a modification in the structure of your files. For each iteration time, there is a block containing the x
and y
coordinates of the polygon's vertices:
# file: data1.txt
# itr 0
0 0
1 1
1 2
0 0
# itr 1
1 3
2 1
0 1
1 2
1 3
# itr 2
3 1
2 1
0 0
3 1
请注意,每个块由两条空行分隔.对于迭代0(块0或itr 0
),存在具有三个顶点的多边形,itr 1
具有四个顶点,而itr 2
具有三个顶点.要获得闭合曲线,需要指定终点,例如,对于itr 1
,我将点1 3
放置两次.
Notice that each block is separated by two empty lines. For iteration 0 (block 0 or itr 0
) there is a polygon with three vertices, itr 1
has four vertices, and itr 2
has three vertices. To obtain a closed curve, it is needed to specify the end point, for example, for itr 1
I put the point 1 3
twice.
对于此文件,我们可以在迭代iter
中将多边形绘制为
For this file, we can plot the polygon at iteration iter
as
iter = 1 # select block 1, or itr 1
plot "data1.txt" index iter w lp ps 2 pt 7
如果您有多个文件,请尝试
If you have several files, then try
# option 1
nbr = 6 # number of files
iter = 1 # select block 1, or itr 1
plot for [i=1:nbr] "data".i.".txt" index iter w lp ps 2 pt 7 title "".i
#option 2
files = system("ls data*.txt") # get all datafiles in folder
iter = 1 # select block 1, or itr 1
plot for [data in files] data index iter w lp ps 2 pt 7 title data
这篇关于gnuplot:如何从顶点绘制多边形/轮廓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!