xtd 0.2.0
math.cpp

This example uses several mathematical and trigonometric functions from the xtd::math class to calculate the inner angles of a trapezoid.

#include <xtd/console>
#include <xtd/math>
#include <xtd/startup>
using namespace xtd;
namespace math_example {
// The following class represents simple functionality of the trapezoid.
class math_trapezoid_sample {
public:
// The main entry point for the application.
static auto main() {
auto trpz = math_trapezoid_sample {20.0, 10.0, 8.0, 6.0};
console::write_line("The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0");
auto h = trpz.get_height();
console::write_line("trapezoid height is: {0}", h);
auto dx_r = trpz.get_left_base_radian_angle();
console::write_line("trapezoid left base angle is: {0} Radians", dx_r);
auto dy_r = trpz.get_right_base_radian_angle();
console::write_line("trapezoid right base angle is: {0} Radians", dy_r);
auto dx_d = trpz.get_left_base_degree_angle();
console::write_line("trapezoid left base angle is: {0} Degrees", dx_d);
auto dy_d = trpz.get_right_base_degree_angle();
console::write_line("trapezoid left base angle is: {0} Degrees", dy_d);
}
math_trapezoid_sample(double long_base, double short_base, double left_leg, double right_leg) {
long_base_ = math::abs(long_base);
short_base_ = math::abs(short_base);
left_leg_ = math::abs(left_leg);
right_leg_ = math::abs(right_leg);
}
auto get_height()->double {
auto x = get_right_small_base();
return math::sqrt(math::pow(right_leg_, 2.0) - math::pow(x, 2.0));
}
auto get_square()->double {
return get_height() * long_base_ / 2.0;
}
auto get_left_base_radian_angle()->double {
auto sin_x = get_height() / left_leg_;
return math::round(math::asin(sin_x), 2);
}
auto get_right_base_radian_angle()->double {
auto x = get_right_small_base();
auto cos_x = (math::pow(right_leg_, 2.0) + math::pow(x, 2.0) - math::pow(get_height(), 2.0)) / (2 * x * right_leg_);
return math::round(math::acos(cos_x), 2);
}
auto get_left_base_degree_angle()->double {
auto x = math::radians_to_degrees(get_left_base_radian_angle());
return math::round(x, 2);
}
auto get_right_base_degree_angle()->double {
auto x = math::radians_to_degrees(get_right_base_radian_angle());
return math::round(x, 2);
}
private:
auto get_right_small_base()->double {
return (math::pow(right_leg_, 2.0) - math::pow(left_leg_, 2.0) + math::pow(long_base_, 2.0) + math::pow(short_base_, 2.0) - 2 * short_base_ * long_base_) / (2 * (long_base_ - short_base_));
}
double long_base_ = .0;
double short_base_ = .0;
double left_leg_ = .0;
double right_leg_ = .0;
};
}
startup_(math_example::math_trapezoid_sample::main);
// This code produces the following output :
//
// The trapezoid's bases are 20.0 and 10.0, the trapezoid's legs are 8.0 and 6.0
// trapezoid height is: 4.8
// trapezoid left base angle is: 0.64 Radians
// trapezoid right base angle is: 0.93 Radians
// trapezoid left base angle is: 36.67 Degrees
// trapezoid left base angle is: 53.29 Degrees
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static double sqrt(double value)
Returns the square root of a specified number.
static decimal abs(decimal value)
Returns the absolute value of a decimal number.
static double radians_to_degrees(double radians) noexcept
Convert radians to degrees.
static double pow(double x, double y)
Returns a specified number raised to the specified power.
static decimal round(decimal value)
Rounds a double-precision floating-point value to the nearest integral value.
static double acos(double value)
Returns the angle whose cosine is the specified number.
static double asin(double value)
Returns the angle whose sine is the specified number.
#define startup_(main_method)
Defines the entry point to be called when the application loads. Generally this is set either to the ...
Definition startup.hpp:167
@ h
The H key.
Definition console_key.hpp:102
@ x
The X key.
Definition console_key.hpp:134
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8