我正在尝试使用 ubuntu 的 dot
和 convert
实用程序生成简单二叉树插入的动画 gif。但它并不完全像我想要的那样工作。我最后得到的动画 gif 没有显示完整的树,只显示了根节点。
要测试程序,只需放入一些随机整数并以 -1 停止输入部分。
#include<stdio.h>
#include<stdlib.h>
struct node{
int value;
struct node *left,*right;
};
typedef struct node node;
node *root;
FILE *out;
node *insert(node *,int);
node *new_node(int);
int main()
{
root = NULL;
int temp;
int k = 0;
while(1)
{
scanf("%d",&temp);
if( temp == -1) break;
root = insert(root,temp);
take_snap(k++); // this function writes the dot file and create a jpg image for the current tree
}
animate();
// this function use convert utility to combine all the images made earlier and create a animated gif
return 0;
}
node *new_node(int x)
{
node *new = (node *) malloc(sizeof(node));
new->left = new->right = NULL;
new->value = x;
return new;
}
node *insert(node *s,int temp)
{
if( s == NULL)
return new_node(temp);
else if( s->value < temp )
s->right = insert(s->right,temp);
else if( s->value > temp )
s->left = insert(s->left,temp);
return s;
}
take_snap(int index)
{
out = fopen("temp.dot","w");
fprintf(out,"graph {\n");
if( root )
fprintf(out,"%d\n",root->value);
dottify(root);
fprintf(out,"}\n");
fclose(out);
char tempstr[100];
sprintf(tempstr,"dot -Tjpg temp.dot -o image%d.jpg",index);
system(tempstr);
}
dottify(node *s)
{
if( s )
{
if(s->left)
{
fprintf(out,"%d -- %d\n",s->value,(s->left)->value);
dottify(s->left);
}
if(s->right)
{
fprintf(out,"%d -- %d\n",s->value, (s->right)->value);
dottify(s->right);
}
}
}
animate()
{
//system("convert *.jpg -resize 200x200 *.jpg");
system("convert -delay 100 -loop 0 image*.jpg animation.gif");
system("eog ./animation.gif");
}
我做错了什么?
我尝试使用 resize
运算符,但没有得到我想要的结果。
http://i.stack.imgur.com/e1rPD.gif
编辑:
我已经注释掉了调整大小的部分,不知何故它制作了超过必要的 jpg。但问题仍然存在。
最佳答案
convert *.jpg -resize 200x200 *.jpg
有两个问题:
- 您需要添加感叹号以强制
转换
为困惑的纵横比 convert
无法进行批量转换,一次只能转换一张图片。
像这样:
for i in *.jpg ; do convert $i -resize 200x200! $i ; done
但由于缩放,结果不会很好。
您可以做的另一件事是强制 graphviz 生成固定大小的图像,只需添加一些图形属性:ratio=fill, size="2,2!", resolution=100
。
像这样:
take_snap(int index)
{
out = fopen("temp.dot","w");
fprintf(out,"graph {\n");
fprintf(out,"graph [ratio=fill, size=\"2,2!\", resolution=100];\n");
问题是所有图像的大小都是 200x200px,单节点图除外。
我不知道为什么会这样。但是您可以使用 convert
修复此图像。
convert image0.jpg -resize 200x200! image0.jpg
结果是这样的:
关于c - 二叉树插入动画GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21967153/