C++_OpenCV_获取两点的所有坐标点(两点相连直线垂直)

绘制结果

//获取直线上的所有坐标点,这必须是直线
void getStraightLineAllPoint(int x0,int y0, int x1, int y1, vector<cv::Point> &points){

    int distance = 0;
    int xIncrement = 0,yIncrement = 0;
    if(x0 == x1){
        distance = y0 - y1;
        yIncrement = y0 > y1 ? -1 : 1;
    }else if(y0 == y1){
        distance = x0 - x1;
        xIncrement = x0 > x1 ? -1 : 1;
    }else {
        throw "getStraightLineAllPoint error!";
    }

    for(int i = 0; i < std::abs(distance); i++){
        points.push_back(cv::Point(x0+xIncrement*i,y0+yIncrement*i));
    }
}

测试

    Point p1 = Point(200,200);
    Point p2 = Point(300,200);
    
    cv::circle(src,p1,5,Scalar(0,255,0));
    cv::circle(src,Point(200,300),5,Scalar(0,255,0));
    cv::circle(src,Point(200,100),5,Scalar(0,255,0));
    cv::circle(src,Point(100,200),5,Scalar(0,255,0));
    cv::circle(src,Point(300,200),5,Scalar(0,255,0));
    vector<cv::Point> points;
    
    getStraightLineAllPoint(p1.x,p1.y,p2.x,p2.y,points);
    for(vector<cv::Point>::iterator it = points.begin(); it !=points.end(); ++it ){
        cv::circle(src,*it,1,Scalar(0,0,255));
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值