【问题标题】:Making a diamond with specific widht and height in c (gnuplot)在c中制作具有特定宽度和高度的钻石(gnuplot)
【发布时间】:2022-01-21 12:12:13
【问题描述】:

所以我正在尝试制作一个程序,输出 n 宽和 n 高的菱形的坐标,然后表示使用 gnuplot 程序完成的 figure.out。我已经完成了这段代码,但我想让它更容易。

#include <stdio.h> 
#include <stdlib.h> 
#define DEF 10
int main() 
{ 
   float def_an,def_al,slope,i,j,height,width; 
   FILE *out = fopen ( "diamond.out", "w" );
   
   if(out==NULL)
   {
       printf("Wrong output");
       return 1;
   }

   printf("Introduce the height: "); 
   scanf("%f", &height); 
   printf("Introduce the width: "); 
   scanf("%f", &width); 
   def_al=(height/DEF);
   def_an=(width/DEF);
   slope=(height/width);


   for(j=0;j<= height/2;j+=def_al) 
   { 
       for(i=((j-(height/2))/slope);i<=(((height/2)-j)/slope);i+=def_an) 
       {   
           printf("%.2f %.2f\n",i,j); 
           fprintf (out, "%.2f %.2f\n", i,j );
       } 
   } 

   for(j=(0-def_al);j>= -height/2;j-=def_al) 
   { 
       for(i=(((-height/2)-j)/slope);i<=((j-(-height/2))/slope);i+=def_an) 
       {   
           printf("%.2f %.2f\n",i,j); 
           fprintf (out, "%.2f %.2f\n", i,j );
       } 
   } 
   
   fclose(out);
   
   return 0; 
}                                                                                               

【问题讨论】:

  • gnuplot 知道如何在点之间画线。所以你可以只输出5分;你根本不需要循环。
  • 你是认真的吗?我整晚都在制作那个代码,哈哈。好的,但是那我该怎么做呢?我的意思是你能给我举个例子说明 gnuplot 代表 5 分吗?
  • 好吧,但是钻石应该填充在里面,而不是不透明的
  • 我已经有一段时间没有使用 gnuplot 了。我必须找到并阅读它的文档。你也可以。
  • @Tenko 我建议在 gnuplot 中对 plot with polygonsset object polygon 进行一些研究;手册、SO 和 google 中有很多示例...

标签: c ubuntu gnuplot


【解决方案1】:

绘制一堆定制的钻石并不像您想象的那么简单。 当然,您可以使用 cmets 中提到的 Eldrad 建议,但绘图样式 with polygons 用于 splot 和“3D”绘图(可能也可以用于 2D)。

因此,您可以在 for 循环中从数据块中的数据绘制对象(检查 help polygon),而不是绘制数据。 下面的示例只是 gnuplot 代码。您必须使用 C 或任何其他语言创建此文本并将其发送到 gnuplot。

代码:

### plotting diamonds in custom size
reset session

# x,  y, width, height
$Diamonds <<EOD
 0   0   3   5
 5   0   2   1
 4   4   3   2
-7   5   4   4
-6  -6   3   5
 6  -6   7   2
EOD

x(n) = word($Diamonds[n],1)
y(n) = word($Diamonds[n],2)
w(n) = word($Diamonds[n],3)
h(n) = word($Diamonds[n],4)

set size square
set xrange [-10:10]
set yrange [-10:10]

do for [i=1:|$Diamonds|] {
    set object i polygon from x(i),y(i)+h(i)/2. to x(i)+w(i)/2.,y(i) to x(i),y(i)-h(i)/2. to x(i)-w(i)/2.,y(i) to x(i),y(i)+h(i)/2.
    set object i fc lt i fillstyle solid 1.0 border
}

plot NaN notitle    # empty dummy plot or plot something else
### end of code

结果:

【讨论】:

  • 看起来不错,但我应该可以用 plot "diamond.out" pt 5 ps 8 做到这一点
  • @Tenko 你没有在你的问题中提到pt 5 ps 8。我很迷惑。在 gnuplot 中,pt 5 是一个实心正方形,而不是一个实心菱形(这将是 pt 13)。你只想用一条线绘制钻石的周长吗?或者您想绘制钻石的周长(坐标存储在'diamond.out' 中的方点pt 5 ps 8?您在哪里定义钻石的中心x 和y 位置?请澄清,理想情况下添加(手)草图图所需的输出。
猜你喜欢
  • 1970-01-01
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多