Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3. Your program should ask the user for an integer N between 1 and 50. The progr

ID: 3757674 • Letter: 3

Question

3. Your program should ask the user for an integer N between 1 and 50. The program should use that number to determine the number of triangles to draw and also to seed the random number generator.

4. You should declare at least three constants (or more) using #define. These should be HEIGHT, WIDTH and ACCEL which should have the values 500, 600 and -0.750 respectively.

5. Your program should create a named structure (Hint: use typedef) that contains slots for the X and Y position, the X and Y velocity, the R,G, and B color levels, the base length and the height of an isosceles triangle.

6. You should create an array of your structures (max size 50).

7. Your program should then open a window WIDTH by HEIGHT in size and draw N filled triangles in the window placing each triangle initially randomly in the window but the base of the triangle should be at least 30 pixels from any edge, having random x and y velocities between -5.000 and 5.000 (with fractional velocities of at least two digits to the right of the decimal) and integer base length and height between 5 and 30.

8. Your program should then animate the triangles. If the triangle is moving up, then it should have its point facing up, if it is moving down, then the point should be down. If a triangle hits an edge then its velocity should reverse so it bounces off that edge.

9. All triangles should have their Y velocity affected by the ACCEL. This should act like gravity, slowing triangles as they move up, and speeding them as they move down. Vy = Vy0 + adt where dt is the elapsed time since the previous graphics frame. The library kipr/graphics.h contains the function seconds() which takes no arguments and returns a number of type double representing the current uptime of the computer accurate to a precision of a millisecond or smaller.

Explanation / Answer

You have not mentioned the initial portion of your problem statement. Although, it would have been better to provide complete problem statement to make context clearer, however from rest of the points mentioned it is clear that it is a graphics program in C. You can use following tips, as applicable, to develop this program further:

1. If you are developing your program in Windows environment, graphics.h header file provides required functions to develop a graphics program. Following steps are required, while developing such a program:

    a) Include graphics.h - #include <graphics.h>

    b) Initialize graphics system by calling function initgraph() as

         int gd = DETECT; //gd - graphics driver to be used, DETECT tells to autodetect the driver

        int gm; //gm - graphics mode

        initgraph(&gd, &gm, <path-to-BGI-files-on-your-system>); //this function load graphics driver and puts the system in graphics mode.

     c) Call desired graphics related functions. As drawpoly() can be used to draw polygon shapes like triangle, rectangle etc.

      d) Call closegraph(), when done to close graphics mode.

          closegraph(); //closes the graphics mode.

2. It is required to input a number N (Not greater than 50) specifying the no. of triangles to be displayed. This number can be used to set the seed for random number generator and gererates random numbers further:

#include <stdlib.h>

srand((unsigned) N);

int r=rand();

Your will use rand() to specify points corresponding to the random location where triangle is to be displayed.

3. Constants can be defined as:

#define HEIGHT 500

#define WIDTH 600

#define ACCEL -0.75

4. Structure for the triangle can be defined as:

   typedef isosceles_triangle

   {

     int X; //starting X position

     int Y; //starting Y position

     int vX; //X direction velocity

     int vY; //Y direction velocity

     int R; //Red color level

     int G; //Green color level

     int B; //Blue color level

     int width; //width(base) of triangle

     int height; //height of triangle

   };

5. Array of N triangles can be defined as:

isosceles_triangle* triangles[N];

6. rectangle() function can be used to create a window like area of WIDTH X HEIGHT inside which triangles can be drawn.

7. To create animation, you will need to apply a loop that will control the movement of triangles. In each iteration of the triangle, new position of the triangle will be calculated by using its previous position and current velocity. Remember velocity in any direction specifies the shift of triangle from its previous position against an elapsed time. And current velocity can be calculated based on the equation vY=vY0+ACCEL*dt, where dt is elapsed time. Once the new position is calculated, triangle will be redrawn. This type of calculation will be performed for all triangles in the rectangular area.

One kipr/graphics.h library is mentioned in the last point. But its overall usage is not clear as initial portion of problem statement is missed. If it had been there, I would be able to provide some more tips.

However I expect above tips also will help you develop the program further.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote