Computing the Length & Area of a 3D Triangle

In summary, the program computes the total length and area of a 3D triangle and outputs the results to a text file called "cw2task1output.txt". The file is automatically created and written to, and can be found in either the source code directory or the directory of the executable, depending on how the program is run.
  • #1
Joon
85
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 cout. 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
  • #2
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.
 

1. How do you calculate the length of a 3D triangle?

The length of a 3D triangle can be calculated using the Pythagorean theorem, which states that the square of the length of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. In a 3D triangle, this formula can be applied to each of the three sides, and the lengths can then be added together to find the total length.

2. What is the formula for calculating the area of a 3D triangle?

The formula for calculating the area of a 3D triangle is (1/2) x base x height, where the base and height are the lengths of two sides of the triangle. This formula is based on the fact that the area of a triangle is equal to half the base times the height.

3. Can the length and area of a 3D triangle be calculated without knowing all three sides?

No, in order to accurately calculate the length and area of a 3D triangle, all three sides must be known. If only two sides are known, the third side can be found using the Pythagorean theorem, but without all three sides, the calculations will not be accurate.

4. How does the shape of a 3D triangle affect its length and area?

The shape of a 3D triangle can greatly affect its length and area. For example, a right triangle will have a longer hypotenuse and a smaller area compared to an equilateral triangle with the same side lengths. Additionally, the angles of a 3D triangle can also affect its area, as a triangle with acute angles will have a larger area than a triangle with obtuse angles and the same side lengths.

5. Are there any practical applications for calculating the length and area of 3D triangles?

Yes, there are many practical applications for calculating the length and area of 3D triangles. For example, in architecture and construction, knowing the length and area of 3D triangles is necessary for creating stable and accurate structures. In engineering, these calculations are important for designing and building various machines and structures. Additionally, in fields such as computer graphics and animation, 3D triangles are used to create realistic and visually appealing images and animations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Linear and Abstract Algebra
Replies
17
Views
4K
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • General Math
Replies
3
Views
882
  • Calculus and Beyond Homework Help
Replies
11
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • General Math
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
25
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
Back
Top