c - 二叉树插入动画GIF

标签 c dot imagemagick-convert

我正在尝试使用 ubuntu 的 dotconvert 实用程序生成简单二叉树插入的动画 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 image

编辑:

我已经注释掉了调整大小的部分,不知何故它制作了超过必要的 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

结果是这样的:
enter image description here

关于c - 二叉树插入动画GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21967153/

相关文章:

链表删除元素函数的C实现

c - 如何修改数组中超过 1 个字节?

graphviz - 如何引导点使用较短的边缘路径?

graph - 绘制非常大的图形

php - 使用 ImageMagick 生成图像而不保存到文件但仍显示在网站上

python - Py_initialize/Py_Finalize 不能与 numpy 一起工作两次

c - 我的堆栈实现有什么问题?

graphviz - 如何在graphviz中隐藏节点边界?

scons:如何处理动态目标?

shell - 为现有png图片添加标签,使用convert