, . . , sin cos X Y.
X Y :
typedef enum { X, Y } xy_enum;
:
template<xy_enum T>
struct _xcys // x cos y sin
{
static double f( double t ) { return cos(t); }
};
template<>
struct _xcys<Y> // x cos y sin
{
static double f( double t ) { return sin(t); }
};
template<xy_enum T>
struct _xsyc // x sin y cos
{
static double f( double t ) { return sin(t); }
};
template<>
struct _xsyc<Y> // x sin y cos
{
static double f( double t ) { return cos(t); }
};
, sin cos X Y. , xcys() X cos Y sin. xsyc() X sin Y cos.
template<xy_enum T>
double xcys ( double t ) { return _xcys<T>::f(t); }
template<xy_enum T>
double xsyc ( double t ) { return _xsyc<T>::f(t); }
std::cout << xcys<X>(0) << " " << xcys<Y>(0) << std::endl;
std::cout << xcys<X>(M_PI/2) << " " << xcys<Y>(M_PI/2) << std::endl;
std::cout << xsyc<X>(0) << " " << xsyc<Y>(0) << std::endl;
std::cout << xsyc<X>(M_PI/2) << " " << xsyc<Y>(M_PI/2) << std::endl;
:
1 0
~0 1
0 1
1 ~0
, :
double calculateObstaclePosition(double robotX, double sesnorReading, double robotDegree, int angleBetweenSensors){
return robotX + sesnorReading * cos((robotDegree + i * angleBetweenSensors) * PI / 180);
}
double calculateObstaclePosition(double robotY, double sesnorReading, double robotDegree, int angleBetweenSensors){
return robotY + sesnorReading * sin((robotDegree + i * angleBetweenSensors) * PI / 180);
}
:
template< xy_enum T >
double calculateObstaclePosition(double robotXY, double sesnorReading, double robotDegree, int angleBetweenSensors){
return robotXY + sesnorReading * xcys<T>((robotDegree + i * angleBetweenSensors) * PI / 180);
}
x y:
obstacleX = calculateObstaclePosition<X>(robot.x, robot.sensorReadings.at(i), robot.deg, angleBetweenSensors);
obstacleY = calculateObstaclePosition<Y>(robot.y, robot.sensorReadings.at(i), robot.deg, angleBetweenSensors);