2

So I have made this program where you can give in the parameters of a circle or a line and it will display said object by drawing an array on the display.

It works by "projecting" a coordinate-system onto an array. (The program also asks you to give the resolution of the array, the number of columns and rows are the same.) Then for every cell of the array it checks if the circle/line intersects the cell. If it does, or it is within a given range, the cell will get a value of 1. If it is out of range, it will be 0. When all the cells have been given a value, the program displays the array. So in the end you will see a circle or line made of ones, the rest of the arrays will show up in zeroes.

The problem is that it takes a relatively long time (7 to 10s) to print the array, while the actual calculations take like no time.

My question is as said in the title, can the process of displaying the array be sped up somehow? Or am I doing something wrong? I am using Code::Blocks as my compiler.

I know that my code is probably very poorly optimized, but I've only started programming like a week ago. So please forgive me if the code is hard to understand.

Thank you in advance!

#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;
}
  • In another stack overflow question which was related on "why does it take longer to print A character than B?" (not those specific chars, but two different) probably has some info you might find useful. As soon as I find that question I'll link you to it. – Some random IT boy Nov 17 '18 at 21:03
  • The first question is "What optimizations are you telling the compiler to use?" (`-Ofast` or `-O3` for gcc/clang or `/Ox` for VS) (those are all "Oh's", not "zeros") – David C. Rankin Nov 17 '18 at 21:10
  • Also -- what *resolution* are you attempting to use. For basic terminal size (e.g. `Resolution: 50 x 50`) the rendering of the circle is instantaneous (well almost) This will also be dependent on your terminal handling of output. Linux xterms are historically fast, while windows terminals (Win7 and earlier) are notoriously slow -- Win10 makes good improvement in that area.. – David C. Rankin Nov 17 '18 at 21:18
  • So how much of the array you can actually see after it is all done? Usually not much and the time it took to write and scroll all that text was completely wasted. So simple solution, just print the tail end of it. Or the real solution, write to a text file instead of the screen so you can see all of it. – Hans Passant Nov 17 '18 at 21:51

1 Answers1

0

As I stated in my comment maybe it has something to do with this stack overflow question and answer

After reading a bit, you could also try to buffer your stdout in order to make it faster.

Some random IT boy
  • 7,569
  • 2
  • 21
  • 47