Thread: Need help with a bouncing ball.

  1. #1
    Registered User
    Join Date
    Nov 2019
    Posts
    3

    Need help with a bouncing ball.

    insert
    Code:
    #include <stdio.h>
    
    
    int main()
    {
        double H,h;
        printf("Give a starting height\n");
        scanf("%lf",&H);
        if(H>0.2)
    {
           do
        {h=(2./3)*H;
        printf("h=%d",h);}while(h>0.2);
        }
        
        
    }
    We drop a ball from a starting height,i want to find all the heights the ball will reach and show it to me except below 0.2 m where it stops bouncing.

    At this point i am pressing random things,any help would be appreciated.

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    From IOCCC:

    Code:
    /* ball.c */
    /* Compile with: 'gcc -ansi -o ball ball.c -lm' */
    /* Works on linux */
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
     
                 main() {
             short a[4];ioctl
          (0,TIOCGWINSZ,&a);int
        b,c,d=*a,e=a[1];float f,g,
      h,i=d/2+d%2+1,j=d/5-1,k=0,l=e/
     2,m=d/4,n=.01*e,o=0,p=.1;while (
    printf("\x1b[H\x1B[?25l"),!usleep(
    79383)){for (b=c=0;h=2*(m-c)/i,f=-
    .3*(g=(l-b)/i)+.954*h,c<d;c+=(b=++
    b%e)==0)printf("\x1B[%dm ",g*g>1-h
    *h?c>d-j?b<d-c||d-c>e-b?40:100:b<j
    ||b>e-j?40:g*(g+.6)+.09+h*h<1?100:
     47:((int)(9-k+(.954*g+.3*h)/sqrt
      (1-f*f))+(int)(2+f*2))%2==0?107
        :101);k+=p,m+=o,o=m>d-2*j?
          -.04*d:o+.002*d;n=(l+=
             n)<i||l>e-i?p=-p
                 ,-n:n;}}

  3. #3
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    I have windows

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    That's a work of art. Anyone with linux needs to run it.

    As for the original question, you keep taking 2/3rds of the original height.
    It needs to be 2/3rds of the current (changing) height.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  5. #5
    Registered User
    Join Date
    Nov 2019
    Posts
    3
    How you do it,thats the main problem i encounter basically.

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    639
    Quote Originally Posted by john.c View Post
    That's a work of art. Anyone with linux needs to run it.
    Exactly!

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Code:
    // ball.c
     
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
     
    #define CURSOR_HOME "\x1b[H"      // cursor to top left corner
    #define SETVT52     "\x1b[?25l"   // Not sure what this does
     
    int main() {
        enum {DarkGrey=40, MediumGrey=100, LightGrey=47, White=107, Red=101};
     
        // Get terminal window size
        short winsize[4];
        ioctl(0, TIOCGWINSZ, &winsize);
        const int Rows = winsize[0], Cols = winsize[1];
     
        const float Diameter = Rows / 2 + Rows % 2 + 1;   // ball diameter
        const float Border   = Rows / 5 - 1;              // border size
     
        // Position and rotation variables.
        float rot  = 0,
              horz = Cols / 2,
              vert = Rows / 4;
     
        // These values change only by being negated
        // (their absolute values are constant)
        float horz_step = .01 * Cols,  // horizontal step
              rot_step  = .1;          // rotation step
        // The vertical step changes continuously (simulating gravity)
        float vert_step = 0;
     
        for (;;) {
            printf(CURSOR_HOME SETVT52);
     
            usleep(79383); // about 12.5 fps
             
            // draw frame
            for (int col = 0, row = 0; row < Rows; ) {
     
                const float V = 2 * (vert - row) / Diameter;
                const float H =     (horz - col) / Diameter;
                const float X = -.3 * H + .954 * V;
     
                int color = 0;
                if (H * H > 1 - V * V) { // if not in the ball
                    if (row > Rows - Border) {
                        if (col < Rows-row || Rows-row > Cols-col)
                            color = DarkGrey;
                        else
                            color = MediumGrey;
                    }
                    else {
                        if (col < Border || col > Cols - Border)
                            color = DarkGrey;
                        else if (H * (H + .6) + .09 + V * V < 1)
                            color = MediumGrey;
                        else
                            color = LightGrey;
                    }
                }
                else {
                    int a = 9 - rot + (.954 * H + .3 * V) / sqrt(1 - X * X);
                    int b = 2 + X * 2;
                    if ((a + b) % 2 == 0)
                        color = White;
                    else
                        color = Red;
                }
     
                printf("\x1B[%dm ", color);
     
                col = (col + 1) % Cols;
                row += (col == 0);
            }
     
            rot  += rot_step;
            vert += vert_step;
     
            if (vert > Rows - 2 * Border)
                vert_step = -.04 * Rows;
            else
                vert_step += .002 * Rows;
     
            horz += horz_step;
            if (horz < Diameter || horz > Cols - Diameter) {
                rot_step  = -rot_step;
                horz_step = -horz_step;
            }
        }
    }
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  8. #8
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    90
    Quote Originally Posted by john.c View Post
    Code:
    // ball.c
     
    #include <stdio.h>
    #include <math.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
     
    #define CURSOR_HOME "\x1b[H"      // cursor to top left corner
    #define SETVT52     "\x1b[?25l"   // Not sure what this does
     
    int main() {
        enum {DarkGrey=40, MediumGrey=100, LightGrey=47, White=107, Red=101};
     
        // Get terminal window size
        short winsize[4];
        ioctl(0, TIOCGWINSZ, &winsize);
        const int Rows = winsize[0], Cols = winsize[1];
     
        const float Diameter = Rows / 2 + Rows % 2 + 1;   // ball diameter
        const float Border   = Rows / 5 - 1;              // border size
     
        // Position and rotation variables.
        float rot  = 0,
              horz = Cols / 2,
              vert = Rows / 4;
     
        // These values change only by being negated
        // (their absolute values are constant)
        float horz_step = .01 * Cols,  // horizontal step
              rot_step  = .1;          // rotation step
        // The vertical step changes continuously (simulating gravity)
        float vert_step = 0;
     
        for (;;) {
            printf(CURSOR_HOME SETVT52);
     
            usleep(79383); // about 12.5 fps
             
            // draw frame
            for (int col = 0, row = 0; row < Rows; ) {
     
                const float V = 2 * (vert - row) / Diameter;
                const float H =     (horz - col) / Diameter;
                const float X = -.3 * H + .954 * V;
     
                int color = 0;
                if (H * H > 1 - V * V) { // if not in the ball
                    if (row > Rows - Border) {
                        if (col < Rows-row || Rows-row > Cols-col)
                            color = DarkGrey;
                        else
                            color = MediumGrey;
                    }
                    else {
                        if (col < Border || col > Cols - Border)
                            color = DarkGrey;
                        else if (H * (H + .6) + .09 + V * V < 1)
                            color = MediumGrey;
                        else
                            color = LightGrey;
                    }
                }
                else {
                    int a = 9 - rot + (.954 * H + .3 * V) / sqrt(1 - X * X);
                    int b = 2 + X * 2;
                    if ((a + b) % 2 == 0)
                        color = White;
                    else
                        color = Red;
                }
     
                printf("\x1B[%dm ", color);
     
                col = (col + 1) % Cols;
                row += (col == 0);
            }
     
            rot  += rot_step;
            vert += vert_step;
     
            if (vert > Rows - 2 * Border)
                vert_step = -.04 * Rows;
            else
                vert_step += .002 * Rows;
     
            horz += horz_step;
            if (horz < Diameter || horz > Cols - Diameter) {
                rot_step  = -rot_step;
                horz_step = -horz_step;
            }
        }
    }
    Nice! Blocky as hell but still looks pretty cool.

  9. #9
    Informer -Adrian's Avatar
    Join Date
    Jan 2013
    Posts
    830
    Quote Originally Posted by flp1969 View Post
    From IOCCC: ()
    Way cool!

  10. #10
    Registered User
    Join Date
    Dec 2017
    Posts
    836
    Quote Originally Posted by Sir Galahad View Post
    Nice! Blocky as hell but still looks pretty cool.
    That's my rewrite (for readability) of the code that ftp posted from the obfuscated code competition.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bouncing a ball
    By ammad in forum C++ Programming
    Replies: 16
    Last Post: 08-27-2009, 05:02 PM
  2. Bouncing Ball
    By The Brain in forum Windows Programming
    Replies: 12
    Last Post: 12-29-2006, 08:56 AM
  3. bouncing ball
    By planet_abhi in forum Game Programming
    Replies: 2
    Last Post: 11-10-2003, 07:18 AM
  4. The Old Bouncing Ball
    By bartybasher in forum Game Programming
    Replies: 3
    Last Post: 08-19-2003, 03:06 AM
  5. Bouncing ball
    By motocross95 in forum Game Programming
    Replies: 10
    Last Post: 01-07-2002, 09:25 AM

Tags for this Thread