数据压缩算法:旋转门算法(SDT)的C#实现

来源:互联网 发布:网络中的亲社会行为 编辑:程序博客网 时间:2024/05/01 10:56

public struct point    {        public point(double pointx, double pointy)        {            this.x = pointx;            this.y = pointy;        }        private double x;        private double y;        public double X        {            get            {                return x;            }            set            {                x = value;            }        }        public double Y        {            get            {                return y;            }            set            {                y = value;            }        }    }

    public struct SDTPoints    {        private point currentData;//当前读取数据        private point lastReadData;//上一个读取数据        private point lastStoredData;//上一个保存数据        public point CurrentData        {            get            {                return currentData;            }            set            {                currentData = value;            }        }        public point LastReadData        {            get            {                return lastReadData;            }            set            {                lastReadData = value;            }        }        public point LastStoredData        {            get            {                return lastStoredData;            }            set            {                lastStoredData = value;            }        }    }

 public List<point> SDTcompress(List<point> originData, double AccuracyE,IProgress<int> progress,CancellationToken cancel)//后两个参数为其异步编程使用        {            List<point> listSDT=new List<point>();            double upGate = -double.MaxValue;            double downGate = double.MaxValue;            double nowUp, nowDown;//当前数据的上下斜率            SDTPoints status=new SDTPoints();            if (originData.Count <= 0)                return null;            status.LastReadData = originData[0];            status.LastStoredData = status.LastReadData;            listSDT.Add(status.LastReadData);            int i = 0;            foreach (var p in originData)            {                status.CurrentData = p;                nowUp = (p.Y - status.LastStoredData.Y - AccuracyE)/(p.X - status.LastStoredData.X);                if (nowUp > upGate)                    upGate = nowUp;                nowDown = (p.Y - status.LastStoredData.Y + AccuracyE)/(p.X - status.LastStoredData.X);                if (nowDown < downGate)                    downGate = nowDown;                if (upGate >= downGate)                {                    listSDT.Add(status.LastReadData);//保存前一个点                    status.LastStoredData = status.LastReadData;//修改最近保存的点                    upGate=(p.Y-status.LastStoredData.Y-AccuracyE)/ (p.X - status.LastStoredData.X);                    downGate = (p.Y - status.LastStoredData.Y + AccuracyE) / (p.X - status.LastStoredData.X);                }                status.LastReadData = p;                i++;                cancel.ThrowIfCancellationRequested();                progress?.Report(i * 100 / originData.Count );            }            if (listSDT.Count == 1)            {                listSDT.Add(originData[originData.Count-1]);            }            return listSDT;        }


原创粉丝点击