TU-Programmieren_2/exercise4/task1.main.cpp
2025-04-09 10:22:44 +02:00

43 lines
1.9 KiB
C++

/// @file
/// @brief Task1: "single-file" excutable C++ program
/// @todo Include standard library headers as needed
#include <array> // std::array
#include <cmath>
#include <iostream>
namespace task1 {
using Coord = std::array<double, 2>;
/// @todo Implement a function 'rotate_counter_clockwise' according to the description below:
/// - the function receives a two-dimensional coordinate in form of a 'std::array<double, 2>'
/// - the function receives a rotation angle (in radians) in form of a 'double'
/// - the function rotates the coordinate counter clockwise around the origin
/// - the function returns the rotated coordinate as a std::array<double, 2>
std::array<double, 2> rotate_counter_clockwise(const Coord& coord, double angle) {
std::array<double, 2> rotated_coord = {0, 0};
rotated_coord[0] = coord[0] * cos(angle) - coord[1] * sin(angle);
rotated_coord[1] = coord[0] * sin(angle) + coord[1] * cos(angle);
return rotated_coord;
}
} // namespace task1
/// @todo Implement a main function conducting the following tasks in this order:
/// - Create two coordinates (local variables):
/// - std::array<double, 2> coord1 = {112,211};
/// - std::array<double, 2> coord2 = {-42,23};
/// - Use your 'rotate_counter_clockwise' function to rotate both coordinate by 180 degrees
/// - Print the resulting rotated coordinates to the console
/// - Hint: the expected coordinates after rotating 180 degrees are
/// - std::array<double, 2> coord1_rotated = {-112,-211};
/// - std::array<double, 2> coord2_rotated = {42,-23};
int main() {
std::array<double, 2> coord1 = {112, 211};
std::cout << "Original coord1: " << coord1[0] << ", " << coord1[1] << std::endl;
std::array<double, 2> coord1_rotated = task1::rotate_counter_clockwise(coord1, std::numbers::pi_v<double>);
std::cout << "Rotated coord1: " << coord1_rotated[0] << ", " << coord1_rotated[1] << std::endl;
return 0;
}