Solved 24-780 Engineering Computation Problem Set 04

$30.00

Original Work ?

Download Details:

  • Name: Assignment-4-w37k4v.zip
  • Type: zip
  • Size: 318.92 KB

Category: Tags: , , You will Instantly receive a download link upon Payment||Click Original Work Button for Custom work

Description

Rate this product

PS4-1 Visualization of Shuffling (ps4-1.cpp)[30pts]
In PS4-1, you write a program that visualizes how the ordered array is shuffled to make a set of nonrepeating random numbers. Your program should open 400×400 pixel window and visualize
intermediate state the array contents during shuffling by a stack of blue rectangles, except the lines that
have just been swapped which must be drawn by red rectangles. Each rectangle must be 20 pixels high,
and the width must be 20 times the value of an item stored in the array in pixels.
Every time the lines are swapped, the program shows the current state (just before swap) and wait for a
key stroke. In the final picture, all lines must be drawn by blue rectangles.
The program must close any time the user presses the ESC key.
Partially written program is given as a starting point (from Canvas or cut & paste base code below). Fill
missing lines and correct errors to complete the program.
The program must be saved as ps4-1.cpp and included in the Zip file.
#include <stdio.h>
#include “fssimplewindow.h”
// There are some erros. Fix them.
void VisualizeArrayContents(int n,int x[],int movedIndex1,int movedIndex2)
{
// Fill this function.
}
void SwapInt(int a,int b)
{
// Fill this function and use from your Shuffle function.
}
void Shuffle(int n,int x[])
{
int i=0,movedIndex1=-1,movedIndex2=-2;
for(;;)
{
FsPollDevice();
auto key=FsInkey();
if(FSKEY_ESC==key)
{
break;
}
if(FSKEY_SPACE==key)
{
// Fill this part. If i<n, shuffle one step and remember which ones were swapped.
// Otherwise make sure no red lines are drawn in VisualizeArrayContents function.
}
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
VisualizeArrayContents(n,x,movedIndex1,movedIndex2);
FsSwapBuffers();
}
}
int mian(void)
{
int x[20]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
FsOpenWindow(16,16,400,400,1);
Shuffle(20,x);
return 0;
}
PS4-2 Cannonball Game (ps4-2.cpp) [70pts]
When you start the program, it opens a graphics window of the size 800×600 pixels. This window
represents an 80×60-meters rectangular region on a vertical plane. The origin of the physical coordinate
system is at the lower left corner of the window — (0,600) –, and 1.0 meter in the physical coordinate
system corresponds to 10 pixels in the window coordinate system.
In the window are the following objects drawn with different colors:
• a cannon (blue square and a piece of line),
• a cannonball (randomly changing color),
• a target (a red square), and
• five obstacles (five green rectangles).
The goal of the game is to hit the target using the smallest possible number of cannonballs. If there is
no clear path to hit the target, you may hit an obstacle to destroy it, but you waste one cannonball. The
game ends when you hit the target. A cannonball is subject to the gravitational force, but there is no
other force applied to the cannonball – this makes the trajectory a perfect parabola.
Cannon is placed near the lower left corner, 5 meters to the right from the left edge and 5 meters up
from the bottom edge, and initially points to the right 30 degrees up from the horizon. The direction of
the cannon continuously rotates by 1 degree counter clockwise every time the screen is re-drawn.
When the angle reaches 90 degrees (straight up), the angle needs to be brought back to 0 degree
(horizontal).
You shoot a cannonball by pressing the space bar (FSKEY_SPACE). The initial speed of the cannonball is
40 m/sec. The trajectory of the cannonball should be animated. Your program needs to calculate the
trajectory by a numerical integration scheme, such as Euler’s method. Use time step of 25 milliseconds.
Use FsSleep(25); every time you draw the picture so that the motion of the cannonball on the screen is
roughly real time. The aim should keep moving for the next shot while the cannon ball is flying, however,
the user cannot shoot another cannonball until the ball disappears. The cannonball must disappear
when it goes below the bottom edge or to the right of the right edge of the window.
The radius of the ball must be 5 pixels, and considered collision only when the center of the ball hits a
target or an obstacle. The ball must change color randomly every frame.
When the game starts, the program places five rectangular obstacles at random locations. The width
and height of each obstacle should range between 8m and 15m, randomly selected. Note that the
effective size of the obstacle needs to comply with this requirement. If a part of an obstacle extends
outside of the window, the effective size may become smaller.
Obstacles must be drawn by green filled rectangles initially. When the ball hits an obstacle, the obstacle
is destroyed, and the destroyed obstacles must be drawn by blue empty rectangles.
The target should be located initially at x=70m, y=60m, and its size is 10m x 10m. It moves downward at
10 meters per second. It can move through the obstacles, i.e., you don’t have to consider collision
between the target and an obstacle. Since the time step is 25 milliseconds, the target moves 0.25m
each step. When it reaches the bottom of the window, it jumps back to y=60m. It can overlap with the
obstacles, but the target should always be visible.
In PS4-2, you write a C++ program called ps4-2.cpp, which must be included in the Zip file.
Assuming that the time required for the numerical integration, managing key strokes, and drawing all
the objects is negligible, let your program sleep 25 millisecond each iteration so that the program
simulates the motion of the ball roughly real time.
You can cut & paste some functions from the sample code used in class to reduce the coding time.
Extra points: If you can draw a trajectory of the cannonball behind it, you get extra 5 point.
Trajectory must be calculated by a numerical integration scheme. The trajectory must be drawn
behind the ball, and stay visible until the user shoots the next cannonball.
Extra points: Add a visual effect of the explosion when the cannonball hits the target. Be creative and
come up with your own way of rendering it. 5 points.
If you go for extra points, make sure you do not end up with adding an error while coding for extra
points. Same penalty apply for not passing the compiler server, even if the error is within lines
written exclusively for extra points!