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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include <iostream>
#include <math.h>
using namespace std;
struct point
{
double x, y;
};
struct line
{
double a,b,c;
};
point solve(line v1, line v2);
double distance(point A, point B);
double area(double a, double b, double c);
double d(line v1, line v2);
double d(line v1, line v2, line v3);
int main()
{
int ok;
do
{
line _a = {0,1,2};
line _b = {1,0,2};
line _c = {1,1,2};
cout<<"line a:\n";
cin>>_a.a>>_a.b>>_a.c;
cout<<"line b:\n";
cin>>_b.a>>_b.b>>_b.c;
cout<<"line c:\n";
cin>>_c.a>>_c.b>>_c.c;
//checks if its triangle
if (d(_a,_b) * d(_a,_c) * d(_b,_c) * d(_a,_b,_c) !=0)
{
//determines all the equation points
point A = solve(_a, _b);
cout<<"point A("<<A.x<<", "<<A.y<<")\n";
point B = solve(_a, _c);
cout<<"point B("<<B.x<<", "<<B.y<<")\n";
point C = solve(_b, _c);
cout<<"point C("<<C.x<<", "<<C.y<<")\n";
double a = distance(A,B);
cout<<"from A to B is "<<a<<" units\n";
double b = distance(B,C);
cout<<"from B to C is "<<b<<" units\n";
double c = distance(C,A);
cout<<"from C to A is "<<c<<" units\n";
double tArea = area(a, b, c);
cout<<"Area of triangle: "<<tArea<<endl;
}
else
{
cout<<"lines does not make triangle\n";
if (d(_a,_b)==0) cout<<"Collinear: A and B\n";
if (d(_a,_c)==0) cout<<"Collinear: A and C\n";
if (d(_b,_c)==0) cout<<"Collinear: B and C\n";
}
cout << "To continue (1) or not (0)?" << endl;
cin >> ok;
}
while (ok == 1);
return 0;
}
double d(line v1, line v2)
{
return v1.a * v2.b - v1.b * v2.a;
}
double d(line v1, line v2, line v3)
{
double a1 = v1.a * ((v2.b * v3.c)-(v2.c * v3.b));
double a2 = -1 * v1.b * ((v2.a * v3.c)-(v2.c * v3.a));
double a3 = v1.c * ((v2.a * v3.b)-(v2.b * v3.a));
return a1+a2+a3;
}
point solve(line v1, line v2)
{
point a;
a.x = 0;
a.y = 0;
double vd = d(v1,v2);
double d2 = v1.c * v2.b - v1.b * v2.c;
double d3 = v1.a * v2.c - v1.c * v2.a;
if (vd!=0)
{
a.x = d2 / vd;
a.y = d3 / vd;
}
return a;
}
double distance(point A, point B)
{
double dx = A.x - B.x;
double dy = A.y - B.y;
return sqrt(dx*dx + dy*dy);
}
double area(double a, double b, double c)
{
double p = (a + b +c)/2;
return sqrt(p * (p-a) * (p-b) * (p-c));
}
|