程式語言 - LeetCode - C++ - 478. Generate Random Point in a Circle



參考資訊:
https://www.cnblogs.com/grandyang/p/9741220.html

題目:


解答:

class Solution {
private:
    double r;
    double xc;
    double yc;

public:
    Solution(double radius, double x_center, double y_center) {
        r = radius;
        xc = x_center;
        yc = y_center;        
    }
    
    vector<double> randPoint() {
        double u = static_cast<double>(rand()) / RAND_MAX;
        double t = (static_cast<double>(rand()) / RAND_MAX) * 2 * M_PI;

        double len = sqrt(u) * r;
        double x = xc + len * cos(t);
        double y = yc + len * sin(t);

        return { x, y };
    }
};

/**
 * Your Solution object will be instantiated and called as such:
 * Solution* obj = new Solution(radius, x_center, y_center);
 * vector<double> param_1 = obj->randPoint();
 */