1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
public class DisegnaGrafici
{
public Bitmap bitmap;
public Graphics g2;
public PictureBox pictureBox;
private int SCALE = 10;
private Random R = new Random();
public List<Strade> viewPortPaths;
public List<Strade> viewPortAbsolute;
private Rectangle viewPort;
Matrix m1;
BernulliPathfinder bernulli;
public DisegnaGrafici(int m, int n, int j, Graphics graphics, double p, double epsilon, Rectangle vPort, int dinamicleft, int dinamictop, int contgw, int contgh, TextBox boxassfreq, TextBox boxrelfreq, bool is_abs)
{
this.g2 = graphics;
this.viewPort = vPort;
this.viewPort.X = dinamicleft;
this.viewPort.Y = dinamictop;
this.viewPort.Width = contgw;
this.viewPort.Height = contgh;
this.m1 = new Matrix();
g2.Clear(Color.Transparent);
g2.FillRectangle(Brushes.Transparent, this.viewPort);
g2.DrawRectangle(new Pen(Color.Black), this.viewPort);
//genero la marice per le trasformazioni
m1.Reset();
m1.Translate(-0, -(int)0, MatrixOrder.Append);
m1.Scale((int)(viewPort.Width / (n - 0)), (int)(-viewPort.Height / (1 - 0)), MatrixOrder.Append);
m1.Translate(viewPort.Left, viewPort.Top + viewPort.Height, MatrixOrder.Append);
//genero le "strade"
bernulli = new BernulliPathfinder(n, m, p);
//disegno i vari path convertendoli per il viewport
if (is_abs)
disegnaABSPaths(fromPathstoAbsViewport(bernulli.paths, viewPort,n));
else
{
disegnaPaths(fromPathstoViewport(bernulli.paths, viewPort));
disegnaHistogramma(viewPort, getDistribution(bernulli.paths, m / SCALE, j), n, j);
disegnaHistogramma(viewPort, getDistribution(bernulli.paths, m / SCALE, n), n, n);
}
getAbsoluteFrequencies(bernulli.paths, n, m, p, epsilon, boxassfreq, boxrelfreq);
}
private void disegnaABSPaths(List<List<PointF>> viewPortABSPaths)
{
for (int i = 0; i < viewPortABSPaths.Count; i++)
{
Pen pen = new Pen(Color.FromArgb(R.Next(0, 255), R.Next(0, 255), R.Next(0, 255)));
for (int j = 0; j < viewPortABSPaths[i].Count - 1; j++)
{
g2.DrawLine(pen, (float)viewPortABSPaths[i][j].X, (float)viewPortABSPaths[i][j].Y,
(float)viewPortABSPaths[i][j + 1].X, (float)viewPortABSPaths[i][j + 1].Y);
}
}
}
public List<List<PointF>> fromPathstoAbsViewport(List<Strade> paths, Rectangle viewPort,int n)
{
List<List<PointF>> viewPortAbsPaths = new List<List<PointF>>();
float tmpy = 0;
float tmpx = 0;
foreach (Strade path in paths)
{
tmpy = 0;
tmpx = 0;
List<PointF> abtmp = new List<PointF>();
foreach (int p in path.values)
{
if (p == 1) tmpy++;
tmpx++;
//only for graphic it rapidly
abtmp.Add(new PointF(tmpx, (tmpy / n)));
}
PointF[] viewPortArrayPath = abtmp.ToArray();
this.m1.TransformPoints(viewPortArrayPath);
viewPortAbsPaths.Add(viewPortArrayPath.ToList());
}
return viewPortAbsPaths;
}
public void getAbsoluteFrequencies(List<Strade> paths, int n, int m, double p, double epsilon, TextBox boxassfreq, TextBox boxrelfreq)
{
Intervalli p_neighbourhood = new Intervalli(p - epsilon, p + epsilon);
int absolute_frequency = 0;
double relative_frequency = 0;
foreach (Strade path in paths)
{
for (int i = 0; i < path.getPath().Count; i++)
{
if (
(path.getPath()[i].X == n)
&&
(path.getPath()[i].Y >= p_neighbourhood.LowerBound)
&&
(path.getPath()[i].Y < p_neighbourhood.UpperBound)
)
{
absolute_frequency++;
}
}
}
relative_frequency = (double)absolute_frequency / (double)m;
boxassfreq.Text = absolute_frequency.ToString();
boxrelfreq.Text = relative_frequency * 100 + "%";
}
public void disegnaPaths(List<Strade> viewPortPaths)
{
for (int i = 0; i < viewPortPaths.Count; i++)
{
Pen pen = new Pen(Color.FromArgb(R.Next(0, 255), R.Next(0, 255), R.Next(0, 255)));
for (int j = 0; j < viewPortPaths[i].getPath().Count - 1; j++)
{
g2.DrawLine(pen, (float)viewPortPaths[i].getPath()[j].X, (float)viewPortPaths[i].getPath()[j].Y,
(float)viewPortPaths[i].getPath()[j + 1].X, (float)viewPortPaths[i].getPath()[j + 1].Y);
}
}
}
public void disegnaHistogramma(Rectangle viewPort, List<Intervalli> intervals, int n, int j)
{
for (int i = 0; i < intervals.Count; i++)
{
int x, y;
int width, height;
// in this case on the fly trasformation is way faster
x = (int)(this.viewPort.Left + this.viewPort.Width * (j) / n);
y = (int)(viewPort.Top + viewPort.Height * ((100 - intervals[i].UpperBound * 100) / 100));
width = intervals[i].Counter;
height = viewPort.Height / intervals.Count;
Rectangle rectangle = new Rectangle(x, y, width, height);
g2.DrawRectangle(Pens.Black, rectangle);
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(128, 150, 0, 0));
g2.FillRectangle(semiTransBrush, rectangle);
g2.FillRectangle(Brushes.Violet, rectangle);
}
}
public List<Intervalli> getDistribution(List<Strade> paths, int noIntervals, int j)
{
List<double> frequencies = new List<double>();
for (int i = 0; i < paths.Count; i++)
{
for (int k = 0; k < paths[i].getPath().Count; k++)
{
if (paths[i].getPath()[k].X == j)
{
frequencies.Add(paths[i].getPath()[k].Y);
}
}
}
List<Intervalli> intervals = new List<Intervalli>();
double intervalLength = 1.0 / (double)noIntervals;
for (int i = 0; i < noIntervals; i++)
{
intervals.Add(new Intervalli(i * intervalLength, (i + 1) * intervalLength));
}
for (int i = 0; i < intervals.Count; i++)
{
for (int k = 0; k < frequencies.Count; k++)
{
if ((frequencies[k] >= intervals[i].LowerBound) && (frequencies[k] < intervals[i].UpperBound))
{
intervals[i].Counter++;
}
}
}
return intervals;
}
public List<Strade> fromPathstoViewport(List<Strade> paths, Rectangle viewPort)
{
List<Strade> viewPortPaths = new List<Strade>();
foreach (Strade path in paths)
{
PointF[] viewPortArrayPath = path.getPath().ToArray();
this.m1.TransformPoints(viewPortArrayPath);
viewPortPaths.Add(new Strade(viewPortArrayPath.ToList()));
}
return viewPortPaths;
}
public Graphics getGrapichs()
{
return this.g2;
}
}
|