且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

WPF特效-绘制实时2D激光雷达图

更新时间:2022-09-17 20:00:49

原文:WPF特效-绘制实时2D激光雷达图

接前两篇:

https://blog.csdn.net/u013224722/article/details/80738619

https://blog.csdn.net/u013224722/article/details/80738995

除了c# GDI 、Opencv(c++)、 c# Emgu绘图外,其实c#  WPF绘图功能也很强大。上文中之所以最终使用了Emgu绘图 ,只是因为在踩坑过程中尝试使用了Emgu的图像处理函数。 即首先将List<double>的数据集合处理成DrawingImage然后得到RenderTargetBitmap,再转换为System.Drawing.Bitmap 再转换为Emgu.CV.Image。 所以后续的实验中直接就使用了Emgu绘图,处理完成后转换为BitmapSource在WPF界面呈现。其实完全使用WPF的绘图方式也能实现实时雷达图效果。

如:

WPF特效-绘制实时2D激光雷达图

绘制效率也挺不错的。上面的Gif,每秒10帧,每帧760个数据点。  显示成弧形是因为我将数据截断了,即设定了最大值范围,超过了则等于设定的最大值。

 #region Data Processing

        private DrawingGroup DrawingGroup;

        private void InitRadarVisualDraw()
        {
            this.DrawingGroup = new DrawingGroup();
            DrawingImage oImgSrc = new DrawingImage(this.DrawingGroup);
            this.ImgMainZm.Source = oImgSrc;

            this.ImgMainZm.Height = this.RadarRadius;
            this.ImgMainZm.Width = this.RadarRadius * 1920d / 1080d;
            this.IntervalDegree = 240d / 760d;
        }

        private double RadarRadius = 1000d;
        private double IntervalDegree = 0;

        private void DrawRadarDatas(List<double> ltDistances)
        {
            try
            {
                this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, (Action)(() =>
                {
                    using (DrawingContext oDrawContent = this.DrawingGroup.Open())
                    {
                        oDrawContent.DrawRectangle(new SolidColorBrush(Colors.Black), new Pen(),
                            new Rect(0, 0, this.ImgMainZm.Width, this.ImgMainZm.Height));
                        oDrawContent.DrawEllipse(new SolidColorBrush(Colors.Green), new Pen(),
                               new Point(this.ImgMainZm.Width/2d, this.ImgMainZm.Height-10), 10, 10);
                        for (int i = 0; i < ltDistances.Count; i++)
                        {
                            double lDistance = ltDistances[i];
                            double dDegree = -120d + i * this.IntervalDegree;
                            double dRadian = Utilitys.ConvertToRads(dDegree);
                            double dX = this.ImgMainZm.Width / 2d + lDistance * Math.Sin(dRadian);
                            if (dX < 0)
                                dX = 0;
                            if (dX > this.ImgMainZm.Width)
                                dX = this.ImgMainZm.Width;
                            double dY = lDistance * Math.Cos(dRadian);

                            oDrawContent.DrawEllipse(new SolidColorBrush(Colors.Green), new Pen(),
                                new Point(dX, dY), 3, 3);
                        }
                    }
                }));
               
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        #endregion