C++从圆弧中随机一个点

#include <iostream>

#include <cmath>

#include <vector>

#include <random>

#include <stdio.h>

 

using namespace std;

const int eps = 1e-2;//精度

#define PI 3.1415926

 

class point{

    public:

    double x;

    double y;

    point(double x_=0,double y_=0)

    {

        x=x_;

        y=y_;

    } 

    void set(point p)

    {

        x=p.x;

        y=p.y;

    }

    friend const point operator+(const point& p1,const point& p2){

        return point(p1.x+p2.x,p1.y+p2.y);

    };

    friend const point operator-(const point& p1,const point& p2){

        return point(p1.x-p2.x,p1.y-p2.y);

    };

    friend const point operator*(const point& p,const double& m){

        return point(p.x*m,p.y*m);

    };

    friend const point operator*(const double& m,const point& p){

        return point(p.x*m,p.y*m);

    };

    friend const point operator/(const point& p,const double& m){

        return point(p.x/m,p.y/m);

    };

    friend const bool operator==(const point& p1,const point& p2){

        return p1.x==p2.x && p1.y == p2.y;

    };

 

    string to_string()

    {

        char str[256] = {0};

        snprintf(str, 255, "(%lf,%lf)", x, y);

        return str;

    }

};

 

typedef point vect2;//重命名,向量也是用坐标表示 

 

class line{

    public:

    point start;

    point end;

    line(point s,point e)

    {

        start.set(s);

        end.set(e);

    }

};

 

/*

function:判断目标点相对于圆心点在哪个象限    

    p : 计算目标坐标点

    circle_point : 圆心点

*/

int getPointInCircleDuadrant(point p, point circle_point)

{

    if(p == circle_point)

    {

        return 0;

    }

    if(p.x > circle_point.x && p.y >= circle_point.y)

    {

        return 1;

    }

    if(p.x <= circle_point.x && p.y > circle_point.y)

    {

        return 2;

    }

    if(p.x < circle_point.x && p.y <= circle_point.y)

    {

        return 3;

    }

    if(p.x >= circle_point.x && p.y < circle_point.y)

    {

        return 4;

    }

}

/* 

function:从第一象限圆弧中随机一个点

    ratio_min:圆弧最小半径

    ratio_max:圆弧最大半径

    rand_point:从圆弧中随机出来的点

 

*/

int randPointInCircleArc(const uint32_t ratio_min, const uint32_t ratio_max, point& rand_point)

{

    if(ratio_min >= ratio_max)

    {

        return -1;

    }

    int angle = rand() % 90; // 随机一个角度

    cout<< " angle = " << angle << endl;

    int ratio_rand = rand() % (ratio_max-ratio_min+1) + ratio_min; // 从半径范围随机一个半径长度

    cout<< " ratio_rand = " << ratio_rand << endl;

    rand_point.x = ratio_rand * sin((90-angle)*(PI/180));

    rand_point.y = ratio_rand * sin(angle*(PI/180));

    cout<< " x = " << rand_point.x << " y = " << rand_point.y  << endl;

    cout<< " sin = " << sin((90-angle)*(PI/180)) << " sin = " << sin(angle*(PI/180)) <<" sin = " << sin(90*(PI/180)) << endl;

    return 0;

}

/*

function:把点转换到对应象限

 

    p_src 第一象限中的点,相对圆心是(0,0)

    duadrant 目标象限

    circle_center 真实圆心

    p_exchange 转换到目标象限、真实圆心 后点的值

*/

int pointChangeToDuadrant(const point& p_src, const int& duadrant, const point& circle_center,  point& p_exchange)

{

    //一 转换到目标象限

    if(duadrant == 1)

    {

        p_exchange.x = p_src.x;

        p_exchange.y = p_src.y;

    }

    else if(duadrant == 2)

    {

        p_exchange.x = -1 * p_src.x;

        p_exchange.y = p_src.y;

    }

    else if(duadrant == 3)

    {

        p_exchange.x = -1 * p_src.x;

        p_exchange.y = -1 * p_src.y;

    }

    else if(duadrant == 4)

    {

        p_exchange.x = p_src.x;

        p_exchange.y = -1 * p_src.y;

    }

    else

    {

        return -1;

    }

    // 二 转换到真实圆心的绝对位置

    p_exchange.x += circle_center.x;

    p_exchange.y += circle_center.y;

    return 0;

}

 

/*

function:从圆的某个象限中随机一个点 

    duadrant :目标点所在的象限

    circle_center :圆心

    ratio_min :圆弧最小半径

    ratio_max :圆弧最大半径

    rand_point :从圆弧中随机出来的点

*/

int randPointInCircleDuadrant(const int& duadrant, const point& circle_center, const uint32_t ratio_min, const uint32_t ratio_max, point& rand_point)

{

    point rand_point_tmp;

    if(0 != randPointInCircleArc(ratio_min, ratio_max, rand_point_tmp))

    {

        return -1;

    }

    if(0 != pointChangeToDuadrant(rand_point_tmp, duadrant, circle_center, rand_point))

    {

        return -1;

    }

    return 0;

}

 

int main()

{

    point circle_point(-10,-10);

    point rand_point;

    // randPointInCircleArc(3, 10, rand_point);

    randPointInCircleDuadrant(1, circle_point, 3, 10, rand_point);

    cout<<" rand_point = " << rand_point.to_string()<<endl;

    cout<<"--------------"<<endl;

    randPointInCircleDuadrant(2, circle_point, 3, 10, rand_point);

    cout<<" rand_point = " << rand_point.to_string()<<endl;

    cout<<"--------------"<<endl;

    randPointInCircleDuadrant(3, circle_point, 3, 10, rand_point);

    

    cout<<" rand_point = " << rand_point.to_string()<<endl;

    cout<<"--------------"<<endl;

    randPointInCircleDuadrant(4, circle_point, 3, 10, rand_point);

    cout<<" rand_point = " << rand_point.to_string()<<endl;

    return 0;

}

 

// g++ CalculateCirclePoint.cpp -o circle -std=c++0x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值