-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangle.cpp
More file actions
128 lines (114 loc) · 3.61 KB
/
Triangle.cpp
File metadata and controls
128 lines (114 loc) · 3.61 KB
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
//**********************************************************************
// Triangle.cpp
// Polymorphism_Project
//
// Created by Karlina Beringer on July 13, 2014.
//
// This source file defines methods of the Triangle class.
// Triangle is a descendant of the abstract class, Polygon.
//**********************************************************************
#include "Triangle.h"
// Helper method uses the Triangle Inequality to determine
// whether side legnths a, b, and c form a triangle:
// If the following conditions are true...
// a + b > c
// a + c > b
// b + c > a
// Then a, b, and c form the side lengths of a triangle.
bool Triangle::isTriangle()
{
// Get side lengths.
double a = C.getDistance( B );
double b = A.getDistance( C );
double c = A.getDistance( B );
// Compare side lengths.
if ( (a + b) < c ) return false;
if ( (a + c) < b ) return false;
if ( (b + c) < a ) return false;
return true;
}
// Default constructor calls Polygon's constructor.
// Initializes color to "orange" and points to
// A(0,0), B(0,3), C(4,0).
Triangle::Triangle(): Polygon()
{
A = Point( 0, 0 );
B = Point( 0, 3 );
C = Point( 4, 0 );
}
// Normal constructor sets the vertices and color.
Triangle::Triangle( string color, Point A, Point B, Point C )
{
if ( isTriangle() )
{
this -> A = A;
this -> B = B;
this -> C = C;
}
else
{
this -> A = Point( 0, 0 );
this -> B = Point( 0, 3 );
this -> C = Point( 4, 0 );
}
this -> color = color;
}
// Copy constructor creates a clone of given Triangle.
Triangle::Triangle( Triangle & copy )
{
this -> A = copy.A;
this -> B = copy.B;
this -> C = copy.C;
this -> color = copy.color;
}
// This method overrides Polygon's print method.
// Getter method prints a description of the Triangle.
// If no parameter is supplied, output goes to console.
void Triangle::print( ostream & output )
{
output << "\t" << TYPE << "\n\t{\n";
output << "\t\tcolor = " << color << "\n";
output << "\t\tA = " << A << "\n";
output << "\t\tB = " << B << "\n";
output << "\t\tC = " << C << "\n";
output << "\t\tside a = BC = " << B.getDistance(C) << "\n";
output << "\t\tside b = AC = " << A.getDistance(C) << "\n";
output << "\t\tside c = AB = " << A.getDistance(B) << "\n";
output << "\t\tperimeter = " << getPerimeter() << "\n";
output << "\t\tarea = " << getArea() << "\n\t}\n\n";
}
// Friend function alternative to the print method.
// Overloads the ostream operator.
// Friend is NOT a member of this class, but has access.
ostream & operator << ( ostream & out, Triangle & T )
{
T.print( out );
return out;
}
// Getter returns the area of the triangle using Heron's Formula:
// Area = sqrt( s * (s - a) * (s - b) * (s - c) ),
// where s = (1/2) * (a + b + c)
double Triangle::getArea()
{
// Get side lengths.
double a = C.getDistance( B );
double b = A.getDistance( C );
double c = A.getDistance( B );
// Compute the area using Heron's Formula.
double s = 0.5 * ( a + b + c );
return sqrt( s * (s - a) * (s - b) * (s - c) );
}
// Getter returns the perimeter of the triangle.
// Perimeter = sum of all side lengths
double Triangle::getPerimeter()
{
// Get side lengths.
double a = C.getDistance( B );
double b = A.getDistance( C );
double c = A.getDistance( B );
// Add up the side lengths.
return a + b + c;
}
//**********************************************************************
// End of File
//**********************************************************************