用opencv绘制一个箭头,沿着圆运动并留下运动轨迹(c++)

用opencv绘制一个箭头,沿着圆运动并留下运动轨迹(c++)。基于该例程可以简单实现一个运动小车的模型。

cpp 复制代码
using namespace cv;

int main()
{
    // 创建一个黑色背景的图像,大小为400*400
    Mat image(400, 400, CV_8UC3, Scalar(0, 0, 0));

    //设置箭头的初始位置和方向
    Point2f arrow_center(200, 200);  //箭头中心点
    double arrow_angle = 0.0;        //箭头角度(弧度)

    // 循环处理每帧图像
    while (true) {
        // 旋转箭头
        arrow_angle += 0.1;
        if (arrow_angle >= 2 * CV_PI) {
            arrow_angle -= 2 * CV_PI;
        }

        // 计算箭头的头和尾位置
        Point2f arrow_head(arrow_center.x + 50 * cos(arrow_angle),
            arrow_center.y + 50 * sin(arrow_angle));
        Point2f arrow_tail(arrow_center.x - 50 * cos(arrow_angle),
            arrow_center.y - 50 * sin(arrow_angle));

        // 绘制箭头
        arrowedLine(image, arrow_tail, arrow_head, Scalar(0, 0, 255), 3);

        // 将箭头中心向前移动10个像素
        arrow_center.x += 10 * cos(arrow_angle);
        arrow_center.y += 10 * sin(arrow_angle);

        // 如果箭头越过边界,则将其移回中央
        if (arrow_center.x < 0 || arrow_center.y < 0 ||
            arrow_center.x > image.rows || arrow_center.y > image.cols) {
            arrow_center.x = image.cols / 2;
            arrow_center.y = image.rows / 2;
        }

        // 如果应该闪烁,将箭头颜色改为绿色,否则为红色


        // 显示图像
        imshow("Arrow", image);

        // 等待一会儿
        waitKey(100);
        //if ((int)(arrow_angle / CV_PI * 5) % 2 == 0) {
            arrowedLine(image, arrow_tail, arrow_head, Scalar(0, 255, 0), 3);
        //}
        imshow("Arrow", image);
    }

    return 0;
}
相关推荐
ofoxcoding2 分钟前
Qwen3.5 API 接入实测:和 GPT-4o 比到底差多少
人工智能·qwen3.5
摄影图5 分钟前
智能汽车领域应用图素材 汽车AI研发转型
人工智能·科技·aigc
m0_569881478 分钟前
基于C++的数据库连接池
开发语言·c++·算法
一只落魄的蜂鸟12 分钟前
【2026年-11期】Where lies the future of humanity in the age of AI?
人工智能
IT阳晨。14 分钟前
PyTorch深度学习实践
人工智能·pytorch·深度学习
.select.15 分钟前
c++ auto
开发语言·c++·算法
老师用之于民16 分钟前
【DAY29】嵌入式系统基础概念总结
人工智能
一水鉴天17 分钟前
整体设计 定稿 的 整理 和完成20260320 之2:文档解析辅助工具编码实现手册 (豆包助手)
人工智能·架构·自动化
欧阳小猜19 分钟前
Transformer革命:从序列建模到通用人工智能的架构突破
人工智能·架构·transformer
2401_8845632419 分钟前
C++中的访问者模式高级应用
开发语言·c++·算法