簡體   English   中英

用gnuplot繪制一系列球體

[英]Plotting a chain of spheres with gnuplot

從C ++函數中我得到一個球體中心坐標(半徑為r )。 我想用gnuplot繪制這個鏈。 如何用真實半徑表示球體? 該解決方案實際上不起作用,因為pointsize的單位與軸的單位不同(並且也隨着軸限制而變化)。

這是一個稍微臟的解決方案,它使用parametric (以及Unix的一些命令)。 對於以下數據的每一行,我們將繪制一個半徑為r的球體,並以(x,y,z)

# points.dat :
# x y z radius 
0 0 0 0.5
1 2 2 1.0
3 4 5 0.7
2 5 7 1.0
1 3 4 0.75
2 0 1 1.5

換句話說,我們將使用以下形式運行命令:

splot x1+r1*cos(v)*cos(u), y1+r1*cos(v)*sin(u), z1+r1*sin(v) title "line 1",\
      x2+r2*cos(v)*cos(u), y2+r2*cos(v)*sin(u), z2+r2*sin(v) title "line 2", ...

以下代碼將執行操作(通過腳本注釋):

set view equal xyz           # to scale the axes of the plot
set hidden3d front           # draw opaque spheres
set parametric               # enable parametric mode with angles (u,v)
set urange [0:2*pi]          
set vrange [-pi/2.0:pi/2.0]

filename = 'spheres.dat'

# get number of data-lines in filename
nlines   = system(sprintf('grep -v ^# %s | wc -l', filename))

# this will save the plot commands
commands = 'splot '
do for [i=1:nlines] {
   # get the i-th line
   line = system( sprintf('grep -v ^# %s | awk "NR == %i {print; exit}" ', filename, i) )

   # extract the data
   x = word(line,1)
   y = word(line,2)
   z = word(line,3)
   r = word(line,4)

   # and save the instructions to plot the corresponding sphere 
   commands = commands . sprintf('%s + %s*cos(v)*cos(u), %s + %s*cos(v)*sin(u), %s + %s*sin(v)  t "line %i"', x, r, y, r, z, r, i)

   # if not EOF, add a comma to commands
   if(i<nlines) { commands = commands . ',  ' }
}

# commands is a string. We can run it into the command line through macros
set macros
@commands

這是我獲得的輸出:

鏈球

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM