简单的跟踪算法
2015-04-26 20:01
169 查看
设两个物体间的X距离为Sx,Y距离为Sy
跟踪物体的X轴速度为Vx,Y轴速度为Vy
那么可以得到简单关系是
Vx / Vy = Sx / Sy;
不妨设
即可得到跟踪的关系式。
下面是一个简单例子,两个小球追击的例子
跟踪物体的X轴速度为Vx,Y轴速度为Vy
那么可以得到简单关系是
Vx / Vy = Sx / Sy;
不妨设
Vx = u * Sx / Sqrt(Sx * Sx + Sy * Sy); Vy = u * Sy / Sqrt(Sx * Sx + Sy * Sy);
即可得到跟踪的关系式。
下面是一个简单例子,两个小球追击的例子
class Ball { public Point m_Location = new Point(110, 110);//小球左边 Point m_Speed = new Point(10, 10);//小球速度 int R = 10;//小球半径 Pen pen = new Pen(Color.Red, 2); public Ball(Point location) { m_Location.X = location.X; m_Location.Y = location.Y; } public void Draw(Graphics g) { Size size = new Size(R,R); Rectangle rt = new Rectangle(m_Location,size); g.DrawEllipse(pen, rt); } public void Move(Rectangle rt) { if (m_Location.X < rt.Left || m_Location.X + R * 2 > rt.Right) { m_Speed.X = -m_Speed.X; } if (m_Location.Y < rt.Top || m_Location.Y + R * 2 > rt.Bottom) { m_Speed.Y = -m_Speed.Y; } m_Location.X += m_Speed.X; m_Location.Y += m_Speed.Y; } public void MoveTo(Point Location) { if (Location.Y != m_Location.Y) { int Sxx = (Location.X - m_Location.X) * (Location.X - m_Location.X); int Syy = (Location.Y - m_Location.Y) * (Location.Y - m_Location.Y); m_Speed.X = (int)((Location.X - m_Location.X) * 14 / Math.Sqrt(Sxx + Syy)); m_Speed.Y = (int)((Location.Y - m_Location.Y) * 14 / Math.Sqrt(Sxx + Syy)); } m_Location.X += m_Speed.X; m_Location.Y += m_Speed.Y; } }
//定义两个小球 Ball ball1 = new Ball(new Point(200,100)); Ball ball2 = new Ball(new Point(0,0)); public Form1() { InitializeComponent(); } //绘制 private void timer_Tick(object sender, EventArgs e) { pictureBox.Refresh(); ball1.Draw(pictureBox.CreateGraphics()); ball1.Move(this.DisplayRectangle); ball2.Draw(pictureBox.CreateGraphics()); ball2.MoveTo(ball1.m_Location); }
相关文章推荐
- 算法:背包,栈,队列的简单实现,链式结构
- 基于cocos2dx的RPG简单实用算法之1 - 角色的移动
- 简单讲解KMP单模式匹配与AC算法多模式匹配(KMP篇)
- 捕食者“物体跟踪算法:未来的计算机操作界面
- 运动目标检测跟踪各过程算法综述
- hdu 1150 Machine Schedule(二分匹配,简单匈牙利算法)
- 一个简单的题让我了解算法并且喜欢上了她
- 简单的分东西算法
- 简单自制跟踪导弹
- 简单的分词算法
- 简单的算法问题1——塑泥课
- 有关数论的算法&简单代码
- 一个简单的JavaScript 日期计算算法
- 数据集+树的一种最简单高效的算法
- 跟踪算法基准--Tracking the Trackers: An Analysis of the State of the Art in Multiple Object Tracking
- Slope one:简单高效的推荐算法
- 【高斯模糊算法的理解】简单易懂
- 时空上下文视觉跟踪(STC)算法的解读与代码复现
- 关于素数的简单算法整理
- 数据结构及简单算法的总结----之【排序】