Comp Sci Computing the Length & Area of a 3D Triangle

Click For Summary
SUMMARY

This discussion focuses on computing the total length and area of a 3D triangle using C++. The provided code reads vertex coordinates from a file named "triangle_cw2(1).txt" and outputs the results to a text file called "cw2task1output.txt". It is confirmed that there is no need to manually create the output file; the program handles this automatically. The output will be generated in the same directory as the source code when run in a debugger or in the executable's directory when run from a command prompt.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with file I/O operations in C++
  • Knowledge of geometric calculations for triangles
  • Experience with using development environments like Visual Studio
NEXT STEPS
  • Explore advanced file handling techniques in C++
  • Learn about geometric algorithms for 3D shapes
  • Investigate error handling in file I/O operations
  • Study the use of libraries for mathematical computations in C++
USEFUL FOR

This discussion is beneficial for C++ developers, students learning geometric computations, and anyone interested in file handling and output management in programming.

Joon
Messages
85
Reaction score
2
I have computed the total length of a 3D triangle and its area. The code is shown below.
I want to use file output instead of count. The file name, cw2task1output, was just given as part of the task, in this case should I make an empty text file named cw2task1output then attach it to the resource file? Would the results be then shown on the created text file? Thanks.

C:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void foutput(double length, double area);

int main()
{

    double total_length, area, x1, x2, x3, y1, y2, y3, z1, z2, z3, side12,
        side13, side23, halfparameter;
    int i, count;

    ifstream inFile("triangle_cw2(1).txt");
    string line, c;
    char ch;

    if (inFile.is_open()) {
        getline(inFile, line);
    }

    inFile >> ch >> x1 >> y1 >> z1;
    inFile >> ch >> x2 >> y2 >> z2;
    inFile >> ch >> x3 >> y3 >> z3;

    total_length = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1)) + sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1) + (z3 - z1) * (z3 - z1)) + sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2) + (z3 - z2) * (z3 - z2));
    side12 = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) + (z2 - z1) * (z2 - z1));
    side13 = sqrt((x3 - x1) * (x3 - x1) + (y3 - y1) * (y3 - y1) + (z3 - z1) * (z3 - z1));
    side23 = sqrt((x3 - x2) * (x3 - x2) + (y3 - y2) * (y3 - y2) + (z3 - z2) * (z3 - z2));
    halfparameter = (side12 + side13 + side23) / 2;
    area = sqrt(halfparameter * (halfparameter - side12) * (halfparameter - side13) * (halfparameter - side23));

    inFile.close();

    foutput(total_length, area); // You must use this function for output.

    // Do not use system("pause") for this task
    return 0;
}

////////  Using the funtion below for output. /////
void    foutput(double total_length, double area)
{

    ofstream cw2task1output;
    cw2task1output.open("cw2task1output.txt");

    cw2task1output << "The total length of sides = " << total_length << endl;
    cw2task1output << "The area of triangle = " << area << endl;

    cw2task1output.close();
}
 
Last edited:
Physics news on Phys.org
You don't need to create a text file for the output of your program -- the program will do that for you, and write its output to that file. Keep in mind that if you're running in the VS debugger, the file will be created in the same directory that contains your source code. If you run your program outside the debugger (i.e., from a command prompt) the file will be created in the directory where the executable is.
 

Similar threads

  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 17 ·
Replies
17
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 11 ·
Replies
11
Views
7K
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K