Hello there,
I am only missing the nested loops for the "\" and "/" pattern and I am lost on how to code that. Any help will be greatly appreciated!
Collision detection is commonly performed in game programming. To determine if two circles collide with
each other, we can check if the distance between the centers of the circles is less than the sum of their radii.
In this problem, you will write a complete C++ program that determines if two circles collide with each other.
More specifically, write a program that does the following:
IGNORE THE lower case g's BECAUSE SPACE DOES NOT WORK!!!
displays the following pattern on the screen:
\
g\
gg\
ggg\
gggg\
gggggCOLLISION DETECTION PROGRAM - YOUR NAME
gggg/
ggg/
gg/
g/
/
YOU MUST USE NESTED LOOPS TO DISPLAY THE PATTERN ABOVE!
asks the user for the radius and the x and y coordinates of the center of circle 1
asks the user for the radius and the x and y coordinates of the center of circle 2
calculates the distance between the centers using the distance formula:
d = sqrt (x2-x1)^2 + (y2-y1)^2
where (x1, y1) and (x2, y2) are the centers of circle 1 and circle 2, respectively.
If the distance is less than the sum of the radii of the two circles, the two circles have collided. If so,
display the message “The two circles collided”; otherwise, display “The two circles did not collide”.
Note: You should allow the user to enter floating-point numbers for the radius and (x, y) coordinates.
Here's what it looks like:
\
g\
gg\
ggg\
gggg\
gggggMy Name
gggg/
ggg/
gg/
g/
/
Enter the coordinates of the center of circle 1: 5.5 6
Enter the radius of the circle 1: 5
Enter the coordinates of the center of circle 2: 5 6.8
Enter the radius of circle 2: 10
The two circles collided
Press any key to continue . . .
This is what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
|
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double x1, x2, y1, y2, r1, r2;
cout << "Enter the coordinates of the center of circle 1: ";
cin >> x1 >> y1;
cout << "Enter the radius of circle 1: ";
cin >> r1;
cout << "Enter the coordinates of the center of circle 2: ";
cin >> x2 >> y2;
cout << "Enter the radius of circle 2: ";
cin >> r2;
double distancex = (x2 - x1) * (x2 - x1);
double distancey = (y2 - y1) * (y2 - y1);
double distance = sqrt(distancex + distancey);
double sumRadius = r1 + r2;
if (distance < sumRadius)
{
cout << "The two circles collided" << endl;
}
else
{
cout << "The two circles did not collide" << endl;
}
return 0;
}
|