Loading...
Searching...
No Matches
geometry::Triangle Class Reference

A triangle defined by its three side lengths. More...

#include <shapes.hpp>

Inheritance diagram for geometry::Triangle:
[legend]
Collaboration diagram for geometry::Triangle:
[legend]

Public Member Functions

 Triangle (double a, double b, double c)
 Construct a triangle from three side lengths.
 
double area () const override
 Compute the area of the shape.
 
std::string name () const override
 Return the human-readable name of the shape.
 
double perimeter () const override
 Compute the perimeter of the shape.
 
- Public Member Functions inherited from geometry::Shape
virtual ~Shape ()=default
 Virtual destructor for safe polymorphic deletion.
 
std::string describe () const
 Pretty-print shape information.
 

Additional Inherited Members

- Static Public Member Functions inherited from geometry::Shape
static int instance_count ()
 Get the total count of Shape instances created.
 
- Protected Member Functions inherited from geometry::Shape
 Shape ()
 Increment the instance counter.
 

Detailed Description

A triangle defined by its three side lengths.

The area is computed via Heron's formula:

\[ s = \frac{a + b + c}{2}, \qquad A = \sqrt{s(s-a)(s-b)(s-c)} \]

Warning
The triangle inequality must hold: each side must be less than the sum of the other two.

Definition at line 202 of file shapes.hpp.

Constructor & Destructor Documentation

◆ Triangle()

geometry::Triangle::Triangle ( double a,
double b,
double c )
inline

Construct a triangle from three side lengths.

Parameters
aFirst side.
bSecond side.
cThird side.
Precondition
\( a + b > c \) and \( b + c > a \) and \( a + c > b \)

Definition at line 211 of file shapes.hpp.

211: a_(a), b_(b), c_(c) {}

Member Function Documentation

◆ area()

double geometry::Triangle::area ( ) const
inlinenodiscardoverridevirtual

Compute the area of the shape.

Returns
The area in square units.

Implements geometry::Shape.

Definition at line 213 of file shapes.hpp.

213 {
214 double s = (a_ + b_ + c_) / 2.0;
215 return std::sqrt(s * (s - a_) * (s - b_) * (s - c_));
216 }

◆ name()

std::string geometry::Triangle::name ( ) const
inlinenodiscardoverridevirtual

Return the human-readable name of the shape.

Returns
A string like "Circle", "Rectangle", etc.

Implements geometry::Shape.

Definition at line 222 of file shapes.hpp.

222{ return "Triangle"; }

◆ perimeter()

double geometry::Triangle::perimeter ( ) const
inlinenodiscardoverridevirtual

Compute the perimeter of the shape.

Returns
The perimeter in linear units.

Implements geometry::Shape.

Definition at line 218 of file shapes.hpp.

218 {
219 return a_ + b_ + c_;
220 }

The documentation for this class was generated from the following file: