## C++ Code Programming.

### computer science

##### Description

//
// There is a red particle and a green particle in a box.
// The box has (x,y) coordinates 0 <= x <= 10, 0 <= y <= 10.
// The user enters initial (x,y) coordinates for the two
// particles. The velocity of a particle is given as
// an (x,y) pair, where each coordinate is the distance
// the particle moves in that direction, in one cycle.
// Initially, the red particle starts with velocity (1,1),
// and the green particle starts with velocity (-1,-1).
//
// The program simulates the movement of the two particles.
// When a particle reaches a wall, it will bounce
// off the wall. When the two particles are too close together,
// they will collide and the simulation will end.
//
// Each particle is represented by an array of four ints.
// Element 0 and 1 are the (x,y) coordinates of the particle.
// Element 2 and 3 are the (x,y) velocity components of the
// particle.
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void updatePoint(int *);
int findDistance(int, int, int, int);
int main()
{
int redData, greenData;
int i,cycle=0, dist;
// set initial conditions
redData = 1;
redData = 1;
greenData = -1;
greenData = -1;
cout << "Enter x-coordinate for red particle (0 to 10):";
cin >> redData;
cout << "Enter y-coordinate for red particle (0 to 10):";
cin >> redData;
cout << "Enter x-coordinate for green particle (0 to 10):";
cin >> greenData;
cout << "Enter y-coordinate for green particle (0 to 10):";
cin >> greenData;
do {
// display state of particles
cout << "cycle " << cycle << endl;
cout << "red particle (x,y,xVel,yVel): " << redData << " " << redData
<< " " << redData << " " << redData << endl;
cout << "green particle (x,y,xVel,yVel): " << greenData << " "
<< greenData << " " << greenData << " " << greenData
<< endl << endl;
// update particle positions and velocities
updatePoint(redData);
updatePoint(greenData);
// check distance between particles
dist = findDistance(redData, redData, greenData, greenData);
cycle++;
} while ((dist > 2) && (cycle < 10));
if (dist <= 2) {
cout << "Collison: oops, end of simulation!\n";
cout << "red particle (x,y,xVel,yVel): " << redData << " " << redData
<< " " << redData << " " << redData << endl;
cout << "green particle (x,y,xVel,yVel): " << greenData << " "
<< greenData << " " << greenData << " " << greenData
<< endl << endl;
}
}
// update position and velocity of particle
// arg is array of data for particle
void updatePoint(int *arg)
{
int distance;
distance = findDistance(arg,arg,0,-1);
if ((distance < 1) && (arg < 0))
arg = - arg;
distance = findDistance(arg,arg,10,-1);
if ((distance < 1) && (arg > 0))
arg = - arg;
distance = findDistance(arg,arg,-1,0);
if ((distance < 1) && (arg < 0))
arg = - arg;
distance = findDistance(arg,arg,-1,10);
if ((distance < 1) && (arg > 0))
arg = - arg;
arg = arg + arg;
arg = arg + arg;
return;
}
// find Manhattan distance between two particles, or between
// a particle and a wall
// (arg0,arg1) are the (x,y) coordinates for first particle/wall
// (arg2,arg3) are the (x,y) coordinates for second particle/wall
int findDistance(int arg0, int arg1, int arg2, int arg3)
{
int distX, distY;
distX = arg0 - arg2;
if (distX < 0)
distX = - distX;
distY = arg1 - arg3;
if (distY < 0)
distY = - distY;
if ((arg0 < 0) || (arg2 < 0))
return distY;
else if ((arg1 < 0) || (arg3 < 0))
return distX;
else
return distX + distY;

## Related Questions in computer science category

##### Disclaimer
The ready solutions purchased from Library are already used solutions. Please do not submit them directly as it may lead to plagiarism. Once paid, the solution file download link will be sent to your provided email. Please either use them for learning purpose or re-write them in your own language. In case if you haven't get the email, do let us know via chat support.