So I have made my first class.
It calculates the area of a triangle from the position of the vertices.
I was wondering if it is possible to have only one variable, and not three (ver1, ver2, ver3), in the constructor?
Code:
#include <iostream>
using namespace std;
class Triangle{
public:
float ver1[1][2];
float ver2[1][2];
float ver3[1][2];
private:
float area_formula(){
float x1 = ver1[0][0];
float y1 = ver1[0][1];
float x2 = ver2[0][0];
float y2 = ver2[0][1];
float x3 = ver3[0][0];
float y3 = ver3[0][1];
return .5*(x2*y3-x3*y2-x1*y3+x3*y1+x1*y2-x2*y1);
}
public:
float area(){
return area_formula();
}
};
int main(){
//float ver0[3][2] = { {0,0}, {1,0}, {1,1}};
//Triangle t1 = ver0;
Triangle t1 = {{0,0}, {1,0}, {1,1}};
cout << t1.area() <<endl;
}