有什么办法可以加快打印速度吗?

时间:2018-11-17 20:48:23

标签: c arrays matrix

因此,我制作了此程序,您可以在其中输入圆或线的参数,它将通过在显示器上绘制数组来显示所述对象。

通过将坐标系“投影”到数组上来工作。 (程序还要求您提供数组的分辨率,列数和行数是相同的。)然后,对于数组的每个单元格,它检查圆/线是否与该单元格相交。如果是,或者它在给定范围内,则该单元格将获得值1。如果它超出范围,则它将为0。当所有单元格都被赋予一个值时,程序将显示该数组。因此,最后您将看到由1组成的圆或直线,其余数组将显示为零。

问题在于打印数组需要较长的时间(7到10s),而实际的计算几乎不需要时间。

我的问题如标题中所述,可以某种方式加速显示数组的过程吗?还是我做错了什么?我正在使用Code :: Blocks作为编译器。

我知道我的代码可能没有很好的优化,但是我只是在一周前才开始编程。因此,如果代码难以理解,请原谅我。

提前谢谢!

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float x = 0, y = 0, ypos= 0 , xpos = 0, radius = 0, rsqrd = 0, rcheck = 0, thick = 0, grad = 0, offs = 0, lcheck = 0;
    int matsize = 0, i, j, branch = 0;
    char filled;


    printf("\n0 - circle\n1 - line\nDo you want to draw a circle or a line? (0/1) ");
    scanf("%d", &branch);
    if(branch == 0)
    {
        printf("Value of radius: ");
        scanf("%f", &radius);
        printf("Position of circle on the x axis: ");
        scanf("%f", &xpos);
        printf("Position of circle on the y axis: ");
        scanf("%f", &ypos);
        printf("Is the circle filled? (y/n) ");
        scanf(" %c", &filled);
        if(filled == 'n')
        {
            printf("The thickness of circle: ");
            scanf("%f", &thick);
        }
        if(filled == 'y' || filled == 'n')
        {
            printf("Resolution: ");
            scanf("%d" , &matsize);
            printf("\n");
        }


    rsqrd = radius*radius; //rsqrd is equal to radius squared.
    x = -1*(matsize/2); //with this I make sure that the x and y values start from the top right corner of the matrix, so that each x, y value corresponds to the correct cell position (i, j)
    y = matsize/2;
    int mat[matsize][matsize];


    if(filled == 'n')
    {
        for(i = 0; i < matsize; i++)
        {
            for(j = 0; j < matsize; j++)
            {
                rcheck = ((y - ypos)*(y - ypos)) + ((x - xpos)*(x - xpos)); // calculating the equation of the circle with the x and y values taking the offset into account
                if(abs(rcheck-rsqrd) <= (thick*thick))
                {
                    mat[i][j] = 1;
                }
                else
                {
                    mat[i][j] = 0;
                }
                x = x+1; //stepping the values of x and y so they stay with the corresponding cell
            }
            x = -1*(matsize/2);
            y = y-1;
        }
    }
    if(filled =='y')
    {
        for(i = 0; i < matsize; i++)
        {
            for(j = 0; j < matsize; j++)
            {
                rcheck = ((y - ypos)*(y - ypos)) + ((x - xpos)*(x - xpos)); // calculating the equation of the circle with the x and y values taking the offset into account
                if(rcheck <= rsqrd)
                {
                    mat[i][j] = 1;
                }
                else
                {
                    mat[i][j] = 0;
                }
                x = x+1; //stepping the values of x and y so they stay with the corresponding cell
            }
            x = -1*(matsize/2);
            y = y-1;
        }
    }


    if(filled == 'y' || filled == 'n')
    {
        for(i = 0; i < matsize; i++)     // displaying the matrix
        {                                //
            for(j = 0; j < matsize; j++) //
            {                            //
                printf("%d ",mat[i][j]); //
            }                            //
            printf("\n");                //
        }                                //
    }
}
if(branch == 1)
{
    printf("Value of gradient: ");
    scanf("%f", &grad);
    printf("Value of offset: ");
    scanf("%f", &offs);
    printf("Thickness of line: ");
    scanf("%f", &thick);
    printf("Resoultion: ");
    scanf("%d", &matsize);


    x = -1*(matsize/2); //with this I make sure that the x and y values start from the top right corner of the matrix, so that each x, y value corresponds to the correct cell position (i, j)
    y = matsize/2;
    int mat[matsize][matsize];


    for(i = 0; i < matsize; i++)
    {
            for(j = 0; j < matsize; j++)
            {
                lcheck = y - (x * grad); // calculating the equation of the circle with the x and y values taking the offset into account
                if(abs(lcheck-offs) <= thick)
                {
                    mat[i][j] = 1;
                }
                else
                {
                    mat[i][j] = 0;
                }
                x = x+1; //stepping the values of x and y so they stay with the corresponding cell
            }
            x = -1*(matsize/2);
            y = y-1;
    }


    if(branch == 1)
    {
        for(i = 0; i < matsize; i++)    // displaying the matrix
        {                               //
            for(j = 0; j < matsize; j++)//
            {                           //
                printf("%d ",mat[i][j]);// 
            }                           //
            printf("\n");               //
        }                               //
    }
}


return 0;
}

1 个答案:

答案 0 :(得分:0)

正如我在评论中所述,也许与this stack overflow question and answer

有关

稍作阅读后,您也可以尝试缓冲stdout以使其更快。