I Have a stream of points and would like to combine each 2 points in order to draw a line .
public class MyPoint
{
public int X { get; set; }
public int Y { get; set; }
}
I am looking for something that would combine the functionality of Aggregate and Select , Meaning that i would like to later Subscribe to ether a complex type combining the 2 points , or to receive an aggregation as a parameter to my Observer's OnNext delegate :
Something like :
pointObservable.Subscribe((prev, curr) => { });
or
pointObservable.Subscribe((myLineStruct) => { });
Sample to build on :
List<MyPoint> points = new List<MyPoint>();
for (int i = 0; i < 10; i++)
{
points.Add(new MyPoint{ X = i , Y = i * 10});
}
IObservable<MyPoint> pointObservable = points.ToObservable();
After trying 2 solutions , i came across some issues :
First of here is my actual Stream :
observable // Stream of 250 points arriving every interval
.Take(_xmax + 10) // for test purposes take only the Graph + 10
.Select(NormalizeSampleByX) // Nomalize X ( override Real X with display X (
.Scan(new PlotterEcgSample(-1, 0), MergeSamplesWithDistinctX) // which returns current only if current.X > prev.X
.DistinctUntilChanged() // remove all redundant previous points elements
// here i end up with a stream of normalized points
.Zip(observable.Skip(1), (prev, curr) => new {Prev = prev, Curr = curr})
// Dmitry Ledentsov 's addition
.Subscribe(res =>
{
Debug.WriteLine(" {0} {1} , {2} {3}", res.Prev.X, res.Prev.Y, res.Curr.X , res.Curr.Y);
});
with Dmitry's addition i get the following result .
0 862 , 252 -21
1 888 , 253 -24
2 908 , 254 -28
3 931 , 255 -31
4 941 , 256 -35
5 890 , 257 -38
6 802 , 258 -41
7 676 , 259 -44
8 491 , 260 -48
9 289 , 261 -51
10 231 , 262 -55
@Enigmativity's suggestion :
observable.Take(_xmax + 10)
.Select(NormalizeSample)
.Scan(new PlotterEcgSample(-1, 0), MergeSamplesWithDistinctX)
.DistinctUntilChanged()
.Publish(obs => obs.Zip(observable.Skip(1), (prev, curr) => new {Prev = prev, Curr = curr}))
.Subscribe(res =>
{
Debug.WriteLine(" {0} {1} , {2} {3}", res.Prev.X, res.Prev.Y, res.Curr.X , res.Curr.Y);
});
results in :
59 862 , 1 -21
60 867 , 2 -24
61 893 , 3 -28
62 912 , 4 -31
63 937 , 5 -35
64 937 , 6 -38
65 870 , 7 -41
66 777 , 8 -44
67 632 , 9 -48
68 444 , 10 -51
69 289 , 11 -55
...
...