C++_OpenCV_获取斜线上的所有坐标点

//点在哪个象限
short isAtQuadrantal (int x0,int y0, int x1, int y1){

    if( (x0 != x1 && x0 != y1) || (y0 != x1 && y0 != y1) ){
        return -1;
    }

    if(x0 < x1){
        return 0;
    }

    if(y0 > y1){
        return 90;
    }

    if(x0 > x1){
        return 180;
    }

    if(y0 < y1){
        return 270;
    }

}

//获取斜线上的所有坐标点,必须是斜线
void getObliqueLineAllPoint(int x0,int y0, int x1, int y1, vector<cv::Point> &points, float precision = 0.1f){

    short isQuadrantal = isAtQuadrantal(x0,x1,x1,y1);
    if(isQuadrantal != -1){
        if(isQuadrantal == 0 || isQuadrantal == 180){
            int increase = (x0-x1) > 0 ? -1 : 1;
            int x = x0 + increase;
            for (int i = 0; i < abs(x0-x1)-1; i++){
                points.push_back(cv::Point(x,y0));
                x += increase;
            }
        }else{
            int increase = (y0-y1) > 0 ? -1 : 1;
            int y = y0 + increase;
            for (int i = 0; i < abs(y0-y1)-1; i++){
                points.push_back(cv::Point(x0,y));
                y += increase;
            }
        }
        return;
    }
    
    float slope = getSlope(x0,y0,x1,y1);
    float x = x0;
    float y;
    for (float i = 0; i < abs(x1-x0); i+=precision){
        //计算y轴坐标
        y =  slope*(x-x0)+y0;//当已知斜率k,和一点坐标(x1,y1)时,常使用点斜式: y-y1 = k(x-x1)
        points.push_back(cv::Point(x,y));
        if (x0 < x1)
            x+=precision;
        else
            x-=precision;
    }
}


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值