I'm working on a problem for school to calculate the area of a triangle given a set of numbers from an input file. I've got everything working, but im not getting the correct output. I've narrowed the issue down to not getting the right distance valuse, so the problem must be with how i've written those:
1 2 3 4 5
// Get perimeter
double distanceA = sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2) * 1.0); //FIXME:
double distanceB = sqrt(pow(x1 - x3, 2) + pow(y1 - y3, 2)); //THERE IS A PROBLEM HERE I CANNOT
double distanceC = sqrt(pow(x2 - x3, 2) + pow(y2 - y3, 2)); //FIGURE OUT
double s = (distanceA + distanceB + distanceC) / 2;
been trying to figure this out for hours and I'm tired of wasting time and I've tried multiple different forms of the formula. I can post the entire program if that'd help.
The first triangle has vertices: (3,4) (12,20) (22,23) and im getting 9 for the area and 1.0 for distanceA
yeah, I've verified the vector that those are in before hitting this step. here's the whole program. I know it looks like a child wrote it, but we're not supposed to use functions yet.
Well I've found a way to get the numbers into a vector but only as strings not integers. Is there a direction you can point me in that might help me with this block?
My professor would rather be a researcher. I can never get a hold of him and his office hours are impossible for me to make.
Thanks for your help. I replaced that loop with something I'd tried earlier but could'nt get to work where I was using [] to populate the vector instead of ()...:
1 2
// Read a line from input file and conver to vector
while (fin >> triangleVerts.at(0) >> triangleVerts.at(1) >> triangleVerts.at(2) >> triangleVerts.at(3) >> triangleVerts.at(4) >> triangleVerts.at(5)) {
This only works because I know each line in the input file only has 6 values...
Post the contents of /Users/MoKK/School/CPT 182/LAB 2/input.txt.
I suspect that lines 18-30 should be:
1 2
int x1, y1, x2, y2, x3, y3;
fin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
Line 35: You don't need * 1.0
been trying to figure this out for hours and I'm tired of wasting time and I've tried multiple different forms of the formula.
There's an old saying in programming: garbage in, garbage out. It means that if your input is bad, the output will be bad, no matter what you do to the code. So whenever you have a problem like this, the first thing to do is verify that the input values (x1,y1,x2,y2,x3,y3) are correct by examining them in a debugger or printing them out just before the problematic code executes.
yeah, that * 1.0 was me thinking the issue was in the distance calculations not converting to a double, or something. just a hopeless attempt to fix it.
The title of this thread is now misleading as my formulas weren't the problem. I tried outputting the vector at line 23 to troubleshoot, and i think i remember getting the right values, but i'm not sure.
The contents of the input are something like:
3 4 22 23 20 19
65 5 35 55 12 1
0 18 0 5 14 4
etc.
in the form x1 y1 x2 y2 x3 y3
I was kind of worried that my instructor might not appreciate me posting a working program of one of his assignments, but I already posted an almost-working one, LMK if it wouldn't be a big deal.
And you're right, the fix was to get rid of lines 14-23 and replace with while (fin >> vector.at(0) >> vector.at(1) >> etc)
then assign point variables with corresponding indices.