x012.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "xerror.h"
4 #include "xmath.h"
5 #include "xtools.h"
6 #include <array>
7 #include <cmath>
8 #include <iostream>
9 
10 namespace nmx::apps::x012 {
11 
16 {
17 private:
18  //speichere Seiten
19  std::array<double, 3> _sides;
20  //speichere Winkel
21  std::array<double, 3> _angles;
22  //Fläche, Umfang
23  double _area, _perimeter;
24 
25 public:
31  inline RightTriangle(double a, double b) {
32  if (a <= 0.0 || b <= 0.0) {
33  throw std::domain_error(__func__);
34  }
35  const double c = std::hypot(a, b);
36  _sides = { a, b, c };
37  _angles = { asin(a / c), asin(b / c), 0.5 * Math::PI };
38  _area = 0.5 * a * b;
39  _perimeter = a + b + c;
40  }
41 
46  enum Idx { a, b, c };
47 
52  inline const auto &sides() const { return _sides; }
53 
58  inline auto side(Idx idx) const { return _sides.at(idx); }
59 
64  inline const auto &angles() const { return _angles; }
65 
70  inline auto angle(Idx idx) const { return _angles.at(idx); }
71 
76  inline double area() const { return _area; }
77 
82  inline double perimeter() const { return _perimeter; }
83 
90  friend std::ostream &operator<<(std::ostream &os, const RightTriangle &t) {
91  using Idx = RightTriangle::Idx;
92  os << "{" << t.side(Idx::a) << ",";
93  os << t.side(Idx::b) << ",";
94  os << t.side(Idx::c) << "}" << std::endl;
95  os << "{" << t.angle(Idx::a) << ",";
96  os << t.angle(Idx::b) << ",";
97  os << t.angle(Idx::c) << "}" << std::endl;
98  os << "{" << t.area() << "," //
99  << t.perimeter() << "}" << std::endl;
100  return os;
101  }
102 }; //RightTriangle
103 
108 inline void ex1() {
109  using Triangle = RightTriangle;
110  Triangle tr0{ 1, 2 };
111  std::cout << tr0;
112  //kopiere Dreieck
113  Triangle tr1 = tr0;
114  std::cout << "-------------------------------\n";
115  std::cout << tr1;
116 }
117 
118 } // namespace nmx::apps::x012
const auto & sides() const
sides
Definition: x012.h:52
Idx
The Idx enum Zugriff auf die Seiten und Winkel des Dreiecks per Index (Aufzählungstyp) ...
Definition: x012.h:46
auto side(Idx idx) const
side Länge einer Seite
Definition: x012.h:58
double perimeter() const
perimeter
Definition: x012.h:82
RightTriangle(double a, double b)
RightTriangle Konstruktor.
Definition: x012.h:31
The RightTriangle Klasse für ein rechtwinkliges Dreieck.
Definition: x012.h:15
static constexpr double PI
Definition: xmath.h:11
double area() const
area
Definition: x012.h:76
auto angle(Idx idx) const
angle lese einen Winkel
Definition: x012.h:70
friend std::ostream & operator<<(std::ostream &os, const RightTriangle &t)
operator << Ausgabe der Daten eines Dreiecks
Definition: x012.h:90
void ex1()
ex1 Beispiel Ausgabe der Daten für ein rechtwinkliges Dreieck
Definition: x012.h:108
const auto & angles() const
angles
Definition: x012.h:64