|
-
September 17th, 2012, 11:59 PM
#1
[Beginner] Incorrect Output for a Simple Code
The program prompts for 3 circles' center coordinates and their radii.
The program then prompts for a point which is then evaluated for inclusion/exclusion in the three circles' areas.
Code:
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
// Variable declarations
double x1; // x coordinate circle A
double y1; // y coordinate circle A
double x2; // x coordinate circle B
double y2; // y coordinate circle B
double x3; // x coordinate circle C
double y3; // y coordinate circle B
double r1; // radii circle A
double r2; // radii circle B
double r3; // radii circle C
double X; // query x coordinate
double Y; // query y coordinate
double dx1 = X - x1; // x distance between query and circle A
double dy1 = Y - y1; // y distance between query and circle A
double dx2 = X - x2; // x distance between query and circle B
double dy2 = Y - y2; // y distance between query and circle B
double dx3 = X - x3; // x distance between query and circle C
double dy3 = Y - y3; // y distance between query and circle C
bool c1; // query point in circle A y/n
bool c2; // query point in circle B y/n
bool c3; // query point in circle C y/n
// Prompt and read in circle A center coordinates
cout << "Enter x and y coordinates of circle A (2 values): ";
cin >> x1;
cin >> y1;
// Prompt and read in circle A radius
cout << "Enter radius of circle A: ";
cin >> r1;
// Prompt and read in circle B center coordinates
cout << "Enter x and y coordinates of circle B (2 values): ";
cin >> x2;
cin >> y2;
// Prompt and read in circle B radius
cout << "Enter radius of circle B: ";
cin >> r2;
// Prompt and read in circle C center coordinates
cout << "Enter x and y coordinates of circle C (2 values): ";
cin >> x3;
cin >> y3;
// Prompt and read in circle C radius
cout << "Enter radius of circle C: ";
cin >> r3;
// Prompt and read in query point
cout << "Enter x and y coordinates of query point (2 values): ";
cin >> X;
cin >> Y;
// Determine location of query point relative to the circles
if ((dx1*dx1)+(dy1*dy1) <= (r1*r1)) {
c1 = 1;
}
else {
c1 = 0;
}
if ((dx2*dx2)+(dy2*dy2) <= (r2*r2)) {
c2 = 1;
}
else {
c2 = 0;
}
if ((dx3*dx3)+(dy3*dy3) <= (r3*r3)) {
c3 = 1;
}
else {
c3 = 0;
}
// Determine whether circles contain query point
if (c1 == 0 && c2 == 0 && c3 == 0) {
cout << "No circle contains point (" << X << "," << Y << ")." ;
}
else if (c1 == 1 && c2 == 0 && c3 == 0) {
cout << "Circle A contains point (" << X << "," << Y << ")." ;
}
else if (c1 == 0 && c2 == 1 && c3 == 0) {
cout << "Circle B contains point (" << X << "," << Y << ")." ;
}
else if (c1 == 0 && c2 == 0 && c3 == 1) {
cout << "Circle C contains point (" << X << "," << Y << ")." ;
}
else if (c1 == 1 && c2 == 1 && c3 == 0) {
cout << "Circles A and B contain point (" << X << "," << Y << ")." ;
}
else if (c1 == 1 && c2 == 0 && c3 == 1) {
cout << "Circles A and C contain point (" << X << "," << Y << ")." ;
}
else if (c1 == 0 && c2 == 1 && c3 == 1) {
cout << "Circles B and C contain point (" << X << "," << Y << ")." ;
}
else cout << "Circles A, B and C contain point (" << X << "," << Y << ")." ;
}
For circles A(0,0) r=5, B(0,0) r=5, C(0,0) r=5, Point (-10,-10), I somehow get the output "Circles A, B, and C contain point (-10,-10). This happens for all values I have tried, whether it is a valid output statement or not. What am I doing wrong?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|