如何使用C++构建一个极坐标系?
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
//Polar coordinate system
using Point = std::pair<double, double>;
//https://developers.google.com/maps/documentation/javascript/coordinates
class PolarTransform
{
public:
PolarTransform()
:m_origin{ 0,0 }
{
}
Point pixelToPixelScreenCoordinate(Point point) {
}
/*!
极坐标=>平面坐标
*/
Point polarToCartesian(double radius/*极径*/,double degrees/*极角*/) {
auto x = radius * std::cos(degressToRadians(degrees));//x = ρcosθ
auto y = radius * std::sin(degressToRadians(degrees));//y = ρsinθ
std::cout << "polarToCartesian:[" << x << "," << y << "]" << std::endl;
return { x,y };
}
/*!
计算方位角,即与 x 轴的夹角
计算平面上任一点P的极坐标(极径,极角)
*/
void calcIncludedAngle(Point point, double &radius/*极径*/, double °rees/*极角*/,bool ifCalcQuadrant) {
auto abs_x = std::abs(point.first - m_origin.first);
auto abs_y = std::abs(point.second - m_origin.second);
radius = std::hypot(abs_x, abs_y);
degrees = radiansToDegress(std::atan2(abs_y, abs_x));
if (ifCalcQuadrant) {
if (point.first > m_origin.first && point.second > m_origin.second) {//1
} else if (point.first < m_origin.first && point.second > m_origin.second) {//2
degrees = 180 - degrees;
} else if (point.first < m_origin.first && point.second < m_origin.second) {//3
degrees = 180 + degrees;
} else if (point.first > m_origin.first && point.second < m_origin.second) {//4
degrees = 360 - degrees;
}
}
std::cout << "calcIncludedAngle:[" << radius << "," << degrees << "]" << std::endl;
}
/*!将度数转换为弧度*/
inline double degressToRadians(double degress) {
constexpr double unit_degress = 3.14159265358979323846 / 180;//单位度
return degress * unit_degress;
}
/*!将弧度转换为度*/
inline double radiansToDegress(double radians) {
constexpr double unit_radians = 180 / 3.14159265358979323846;//单位弧度
return radians * unit_radians;
}
private:
Point m_origin;
};
转载请注明出处并保持作品的完整性,谢谢