C ++ std :: sort with custom function

时间:2016-01-03 09:22:25

标签: c++ sorting boost stl std

我有一个结构点:

typedef struct Point
{
    double x;
    double y;

    Point operator-(const Point &b)
    {
        Point pt;
        pt.x = this->x - b.x;
        pt.y = this->y - b.y;
        return pt;
    }

    friend std::ostream& operator<<(std::ostream& os, const Point& pt)
    {
        return os << "X: " << pt.x << "\t\t\tY: " <<pt.y << std::endl; 
    }

    friend std::istream& operator>>(std::istream& is, Point& pt)
    {
        return is >> pt.x >> pt.y;
    }
}Point;

我正在尝试从vector<Point>中找到最小元素,然后根据此最小点的角度进行排序。

以下是相应的代码:

bool lex_yx(const Point &a, const Point &b)
{
    if(a.y < b.y)
        return true;

    if (a.y == b.y)
        return (a.x < b.x);

    return false;
}

bool CalcAngRad_Compare (const Point &p_min, Point &a, Point &b)
{
    Point subPt_1, subPt_2;
    double a_r, b_r, a_angle, b_angle;

    subPt_1 = a - p_min, subPt_2 = b - p_min;

    a_angle = atan2(subPt_1.y, subPt_1.x);
    b_angle = atan2(subPt_2.y, subPt_2.x);

    if (a_angle < b_angle) {return true;}
    if (a_angle > b_angle) {return false;}

    a_r = subPt_1.x * subPt_1.x * subPt_1.y * subPt_1.y; 
    b_r = subPt_2.x * subPt_2.x * subPt_2.y * subPt_2.y; 

    return (a_r < b_r); // return (a_r <= b_r); // Code crashes, saying invalid operator <. I do not know why. Pl tell me.
}

auto it  = std::min_element(V.begin(), V.end(), lex_yx);
std::sort(  V.begin(), V.end(), boost::bind(CalcAngRad_Compare, *it, _1, _2)); 

当我传入一个简单的测试输入时,例如

2 2
3 3
4 4
1 1 // this should be first element in the sorted array
5 5

它有效,我得到排序的数组。但现在看看另一个简单的输入:

-0.5    -0.1
-0.1    -0.1
0       1
-1      -0.1 // this should be first element is sorted array. BUT IT IS NOT!!
5       5

我认为我没有做错任何事。一个问题可能是当y坐标相等时,角度为0或pi。在这种情况下,如果半径也相等,那么我return false。我试图将其更改为return (a_r <= b_r),但这似乎不可能。代码崩溃说无效运算符&lt;。 (在文件xutility中,此行为true:else if (_Pred(_Right, _Left))。我不明白正在测试的内容,可能会检查一些优先级。)

我想让你回答:

  1. 如何解决问题并始终获取(正确)排序的矢量
  2. 有没有其他方法可以实现我的目标?
  3. 我非常有兴趣了解我的思维/实施风格中的问题是什么?

2 个答案:

答案 0 :(得分:2)

您的第一个排序标准是atan2计算的角度。在您的情况下,p_min是-1.0 / -0.1。如果a为-1.0 / -0.1且b为-0.5 / -0.1,则您将计算atan2( -0.1 + 0.1, -1.0 + 1.0 );a-p_min)和atan2( -0.1 + 0.1, -0.5 + 1.0 );({{1在这两种情况下都是b-p_min。您继续将0.00.0 * 0.0 * 0.0 * 0.0)与a0.5 * 0.5 * 0.0 * 0.0)进行比较,这意味着b。因此0.0 < 0.0( - 1.0 / -0.1)不小于a( - 0.5 / -0.1)。

要计算矢量之间的角度,请使用DOT产品。 | A | * | B | * cos(A和B之间的角度)= Ax * Bx + Ay * By

b

要比较角度,不必计算double len_p_min = sqrt( p_min.x*p_min.x + p_min.y*p_min.y ); // lenght of p_min double len_a = sqrt( a.x*a.x + a.y*a.y ); // lenght of a double len_b = sqrt( b.x*b.x + b.y*b.y ); // lenght of b Point normalizedMin{ p_min.x / len_p_min, p_min.y / len_p_min }; // direction of p_min ( lenght == 1.0) Point normalizedA{ a.x / len_a, a.y / len_a }; // direction of a ( lenght == 1.0) Point normalizedB{ b.x / len_b, b.y / len_b }; // direction of b ( lenght == 1.0) double cosAmin = normalizedMin.x*normalizedA.x + normalizedMin.y*normalizedA.y; // cos of angle between a and p_min double angleAmin = acos( cosAmin ); double cosBmin = normalizedMin.x*normalizedB.x + normalizedMin.y*normalizedB.y; // cos of angle between b and p_min double angleBmin = acos( cosBmin ); 。比较角度cos

就足够了
acos

如果您想按长度对矢量进行排序,请使用| v | = sqrt(v.x * v.x + v.y * v.y) 如果只需要比较向量的长度,则不需要计算if ( fabs( cosAmin ) < fabs( cosBmin ) ) {return true;} if ( fabs( cosAmin ) > fabs( cosbmin ) ) {return false;} ,只需比较lentgth ^ 2.

sqrt

答案 1 :(得分:2)

  

如何解决问题并始终获得(正确)排序的向量    有没有其他方法可以实现我的目标?

不确定您想要的订单,less_yx是否可以完成这项工作?

  

我非常有兴趣了解我的思维/实施风格中的问题是什么?

假设IEEE浮点运算(否则您有0, 0的域错误

由于您的参考点是最低y,因此传递给atan2的第二个arg为正或空。

- `atan2(0, 0) = 0`
- `atan2(+x, 0) = 0`
- `atan2(x, +y) > 0`

因此根据atan2,任何最低y都是等价的。

a_r = subPt_1.x * subPt_1.x * subPt_1.y * subPt_1.y;
对于0

总是subPt_1.y == 0。 你的意思是

a_r = subPt_1.x * subPt_1.x + subPt_1.y * subPt_1.y;

相反? (因此,即使x

,它也会比较y == 0