找到两点之间的中点

时间:2014-03-03 03:46:51

标签: c++

如何计算a点和b点之间的中点?我的代码会起作用吗?

struct Point
{
  int x;
  int y;    
};

Point midpoint(const Point& a, const Point& b)
{
    double middle = (a.x + b.x) / 2;
    return middle;
}

4 个答案:

答案 0 :(得分:9)

这应该有效:

Point midpoint(const Point& a, const Point& b) {
    Point ret;
    ret.x = (a.x + b.x) / 2;
    ret.y = (a.y + b.y) / 2;
    return ret;
}

答案 1 :(得分:6)

如果中点返回一个Point,你为什么要返回一个双?

答案 2 :(得分:5)

您需要返回

Point p;
p.x = (a.x+b.x)/2;
p.y = (a.y+b.y)/2;
return p;

答案 3 :(得分:1)

Point midpoint(const Point& a, const Point& b)
{
    //double middle = (a.x + b.x) / 2;
    //return middle;
    Point middle;
    middle.x = (a.x + b.x) / 2;
    middle.y = (a.y + b.y) / 2;
    return middle;
}