c-有没有办法加快打印阵列的速度



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

它的工作原理是将坐标系"投影"到阵列上。(该程序还要求您给出数组的分辨率,列和行的数量相同。(然后,对于数组的每个单元格,它会检查圆/线是否与单元格相交。如果是,或者在给定范围内,则单元格将获得值1。如果超出范围,则为0。当所有单元格都已给定值时,程序将显示数组。所以最后你会看到一个由1组成的圆或线,其余的数组将显示为零。

问题是打印数组需要相对较长的时间(7到10秒(,而实际计算几乎不需要时间。

我的问题是,正如标题中所说,阵列的显示过程能否以某种方式加快?还是我做错了什么?我正在使用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 - circlen1 - linenDo 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;
}

正如我在评论中所说,也许这与这个堆栈溢出问答有关

在读了一点之后,您还可以尝试缓冲stdout,以使其更快。

最新更新