Not getting right values with distance formula

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

Any advice would be appreciated!
Last edited on
I'm working on a problem for school to calculate the area of a triangle ...
// Get perimeter

So are you calculating the area or the perimeter?
Area, but my instructor wants us to use Heron's formula. I think my problem is within my perimeter calculations
The first triangle has vertices: (3,4) (12,20) (22,23) and im getting 9 for the area and 1.0 for distanceA

Well, that doesn't look right. Are you sure x1, x2, x3, y1, y2 and y3 have the correct values?
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.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
int main() {
                // Set up and test file stream
    
    //string fileLocation;
    //cin >> fileLocation;
    //ifstream fin((fileLocation);
    ifstream fin("/Users/MoKK/School/CPT 182/LAB 2/input.txt");
    ofstream fout("/Users/MoKK/School/CPT 182/LAB 2/output.txt");
    if(fin.fail()) {
        cout << "Error in opening files." << endl;
        system("pause");
        return-1;}
    
    while (!fin.eof()) {

        
                // Read a line from input file and conver to vector
        string sTriangle;
        getline(fin, sTriangle);
        vector <int> triangle(6);
        for (int i = 0; i < sTriangle.size(); ++i) {
            triangle[i] = sTriangle[i];
        }
                //Assign point values (x,y)
        int x1 = triangle[0];
        int y1 = triangle[1];
        int x2 = triangle[2];
        int y2 = triangle[3];
        int x3 = triangle[4];
        int y3 = triangle[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;
        
        
                //USED THIS TO TRY TO CORRECT ISSUE ABOVE
        //cout << distanceA << endl << endl;
        //cout << distanceB << endl << endl;
        //cout << distanceC << endl << endl;
        
        
                // Calculate Area using Heron's Formula
        double area = sqrt(((s - distanceA) * (s - distanceB) * (s - distanceC)) * s);
        if (area == 0) {
            fout << "NOT A TRIANGLE" << endl;
        }
        else {
            
            fout << fixed;
            fout << setprecision(2);
            fout << area << endl;
        }
    }
    fout.close();
    fin.close();
    return 0;
This code is not correct.

1
2
3
4
5
6
string sTriangle;
getline(fin, sTriangle);
vector <int> triangle(6);
for (int i = 0; i < sTriangle.size(); ++i) {
    triangle[i] = sTriangle[i];
}
Last edited on
yeah you're right. So how it's written, is it taking each char, even spaces and assigning it to the variables?
Yeah, that's what's happening. You are probably also going out of bounds, unless no line has more than six characters.
Arright. well now I have to figure this out. Thanks for your help.
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.
Last edited on
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...


Just posting this so others can benefit.
Last edited on
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.
Last edited on
Topic archived. No new replies allowed.