In: Computer Science
c++problem
Amelia is a computational geometry scientist. Her research has a broad range of applications – from pure geometry applications, to applied fields such as computer mathematics, or physical simulations. As her research assistant, you’re going to help her analyze some triangles in 2d space that she generated from a sample program. Amelia is aware of the fact that you’re new to C++ so the task is easy this week (Based on what Amelia’s more experienced assistants say, this means ”the math is easy, and she doesn’t remember how hard it was to learn C++ for the first time). Your task in the program is to compute the average area of all the triangles. Because every time the triangles generated can be different, you’re going to read them from input and do computation accordingly. To compute the area of a triangle formed by 3 vertices (x1, y1),(x2, y2),(x3, y3):
1. Compute 2 helper vectors ~m = (mx, my) = (x2−x1, y2−y1), ~n = (nx, ny) = (x3−x1, y3−y1); 2. Compute the area A = 1 2 |~m × ~n| = 1 2 |mx · ny − nx · my|. (For those of us to whom vector math is a distant memory, or an exciting discovery for the future): Area = abs(x1y2 + x2y3 + x3y1 − y1x2 − y2x3 − y3x1)/ 2
Example 1
Next triangle: j
Error: the command has to be q or t.
Example 2
Next triangle: t 0 0 3 0 0 4
Next triangle: j
Error: the command has to be q or t.
Example 3
Next triangle: q
Error: there’s no triangle recorded.
Example 5
Next triangle: t 0.4 4.1 121.1 343.1 11.1 3
Next triangle: t 3 2312.1 12.55 66 6 9.654
Next triangle: t 4.1 131 323 1.1 1.2 22
Next triangle: q
The average is: 9024.49!
Example 6
Next triangle: t 0 0 0 1 0 -1
Error: the 3 points you’ve just typed cannot form a triangle.
Here is the complete program:
#include <bits/stdc++.h>
using namespace std;
int main(){
char c;
//vector for storing final area of valid triangle
vector<double> vec;
// variables for vertices and to store area
double x1,x2,x3,y1,y2,y3,area;
//run infinite till user wants to quit
while(true){
//ask user to input
cout<<"Next triangle: ";
cin>>c;
//if user enters q then simply break from loop
if(c=='q'){cout<<"\n";
break;
}
else if(c!='t'){cout<<"\n"; //if command is other then t or
q
cout<<"Error: The command has to be q or t\n"; // show error
message
return 0; //directly exit the program
}
else if(c=='t'){ //if command is t
cin>>x1>>y1>>x2>>y2>>x3>>y3;
//scan all the vertices
/*check if the vertices forms a valid triangle */
area = abs((x1*y2)+(x2*y3)+(x3*y1)-(y1*x2)-(y2*x3)-(y3*x1));
area =(double) (area/2.0);
//give error message that 3 points does not form a triangle
if(area==0){ //if area is zero
cout<<"Error : The 3 points you've just typed cannot form a
triangle";
return 0; //exit the program
}
vec.push_back(area); //if triangle is valid then store in
vector
}
}
if(vec.size()==0){ //if there is no valid triangle entered
cout<<"Error : There's no triangle recorded";
return 0; //exit program
}
int i,n=vec.size(); //declare helping variables
double ans=0; // to store the sum of area of the triangle
for(i=0;i<n;i++){
ans+=vec[i]; //calculate total area to calculate average
}
//display average area
cout<<"The average is :
"<<ans/(double)n<<"!";
return 0; //exit
}
The sample output as below: