Gnuplot: how to draw polygon/contour from its vertices

大憨熊 提交于 2019-12-12 03:39:26

问题


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) ..........
.....

where pk is the k-th point/vertex of the polygon and nb_pts is the number vertices.

My question is how to draw the 2D polygon from its vertices (p1, p2, ...pk) at a certain iteration (row)?

In addition, note that there is not only one data file/polygon but N ones: data1.txt .... dataN.txt

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 

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.

Any idea please?


回答1:


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

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.

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


来源:https://stackoverflow.com/questions/32781536/gnuplot-how-to-draw-polygon-contour-from-its-vertices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!