gpt4 book ai didi

c - 在c中制作具有特定宽度和高度的钻石(gnuplot)

转载 作者:行者123 更新时间:2023-12-04 19:26:31 24 4
gpt4 key购买 nike

所以我正在尝试制作一个程序,输出 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;
}

最佳答案

绘制一堆定制的钻石并不像您想象的那么简单。
当然,你可以使用评论中提到的 Eldrad 的建议,但绘图风格 with polygons适用于 splot和“3D”绘图(可能也可用于 2D)。
因此,您可以在 for 循环中从数据 block 中的数据绘制对象(检查 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
结果:
enter image description here

关于c - 在c中制作具有特定宽度和高度的钻石(gnuplot),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70412299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com