Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.GrammaticalOptimization/DynamicDataDisplay/Charts/Filters/FrequencyFilter.cs @ 12503

Last change on this file since 12503 was 12503, checked in by aballeit, 9 years ago

#2283 added GUI and charts; fixed MCTS

File size: 4.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Windows;
4using Microsoft.Research.DynamicDataDisplay.Charts.Filters;
5
6namespace Microsoft.Research.DynamicDataDisplay.Filters
7{
8    public sealed class FrequencyFilter : PointsFilterBase
9    {
10
11        /// <summary>Visible region in screen coordinates</summary>
12        private Rect screenRect;
13
14        #region IPointFilter Members
15
16        public override void SetScreenRect(Rect screenRect)
17        {
18            this.screenRect = screenRect;
19        }
20
21        // todo probably use LINQ here.
22        public override List<Point> Filter(List<Point> points)
23        {
24            if (points.Count == 0) return points;
25
26            List<Point> resultPoints = points;
27            List<Point> currentChain = new List<Point>();
28
29            if (points.Count > 2 * screenRect.Width)
30            {
31                resultPoints = new List<Point>();
32
33                double currentX = Math.Floor(points[0].X);
34                foreach (Point p in points)
35                {
36                    if (Math.Floor(p.X) == currentX)
37                    {
38                        currentChain.Add(p);
39                    }
40                    else
41                    {
42                        // Analyse current chain
43                        if (currentChain.Count <= 2)
44                        {
45                            resultPoints.AddRange(currentChain);
46                        }
47                        else
48                        {
49                            Point first = MinByX(currentChain);
50                            Point last = MaxByX(currentChain);
51                            Point min = MinByY(currentChain);
52                            Point max = MaxByY(currentChain);
53                            resultPoints.Add(first);
54
55                            Point smaller = min.X < max.X ? min : max;
56                            Point greater = min.X > max.X ? min : max;
57                            if (smaller != resultPoints.GetLast())
58                            {
59                                resultPoints.Add(smaller);
60                            }
61                            if (greater != resultPoints.GetLast())
62                            {
63                                resultPoints.Add(greater);
64                            }
65                            if (last != resultPoints.GetLast())
66                            {
67                                resultPoints.Add(last);
68                            }
69                        }
70                        currentChain.Clear();
71                        currentChain.Add(p);
72                        currentX = Math.Floor(p.X);
73                    }
74                }
75            }
76
77            resultPoints.AddRange(currentChain);
78
79            return resultPoints;
80        }
81
82        #endregion
83
84        private static Point MinByX(IList<Point> points)
85        {
86            Point minPoint = points[0];
87            foreach (Point p in points)
88            {
89                if (p.X < minPoint.X)
90                {
91                    minPoint = p;
92                }
93            }
94            return minPoint;
95        }
96
97        private static Point MaxByX(IList<Point> points)
98        {
99            Point maxPoint = points[0];
100            foreach (Point p in points)
101            {
102                if (p.X > maxPoint.X)
103                {
104                    maxPoint = p;
105                }
106            }
107            return maxPoint;
108        }
109
110        private static Point MinByY(IList<Point> points)
111        {
112            Point minPoint = points[0];
113            foreach (Point p in points)
114            {
115                if (p.Y < minPoint.Y)
116                {
117                    minPoint = p;
118                }
119            }
120            return minPoint;
121        }
122
123        private static Point MaxByY(IList<Point> points)
124        {
125            Point maxPoint = points[0];
126            foreach (Point p in points)
127            {
128                if (p.Y > maxPoint.Y)
129                {
130                    maxPoint = p;
131                }
132            }
133            return maxPoint;
134        }
135    }
136}
Note: See TracBrowser for help on using the repository browser.