#ifndef jfxmap_bezier_h
#define jfxmap_bezier_h

#include <vector>
#include "CoordSys.h"

namespace jf {
//贝赛尔曲线
inline XYZ GetBezierPoints(const std::vector<XYZ> &xys, float t) {
    Point r;
    if (xys.size() == 1) {
        return xys[0];
    } else if (xys.size() == 2) {
        return xys[0] + (xys[1] - xys[0]) * t;
    } else {
        std::vector<XYZ> next_xys;
        for (int i = 0, j = 1; j < xys.size(); i++, j++) {
            XYZ n = xys[i] + (xys[j] - xys[i]) * t;
            next_xys.push_back(n);
        }
        return GetBezierPoints(next_xys, t);
    }
}

inline std::vector<Point> Bezier(const std::vector<Point> &ps, int num) {
    std::vector<XYZ> xyzs;
    for (const Point &p : ps) {
        xyzs.push_back(P2XYZ(p));
    }

    std::vector<Point> ret;

    for (int i = 0; i < num; ++i) {
        float t = i / float(num);
        XYZ r = GetBezierPoints(xyzs, t);
        ret.push_back(XYZ2P(r));
    }

    return ret;
}

}  // namespace jf

#endif