OSDN Git Service

385938fedc57cef32a13b1a250e7807392be889a
[psychlops/silverlight.git] / test4 / PsychlopsMain.cs
1 ///+ Prefix linkto BasicCode1\r
2 //// Lines for set up Psychlops environment\r
3 using Psychlops;\r
4 \r
5 namespace PsychlopsSilverlightApp\r
6 {\r
7 \r
8         public class PsychlopsMain\r
9         {///- Prefix linkto BasicCode1\r
10 \r
11 \r
12                 ///+ Main Routine\r
13                 //// Psychlops runs at the first line of this function psychlops_main().\r
14                 public void psychlops_main()\r
15                 {\r
16                         ///+ 0 linkto BasicCode3_1a\r
17                         ////Prepare global parameters\r
18                         int maxrectnum = 200;\r
19                         int rectnum = 100;\r
20                         double rectsize = 5.0;\r
21                         Rectangle[] rect = StaticFunctions.NewArray<Rectangle>(maxrectnum);\r
22                         double[] rectcolorR = new double[maxrectnum];\r
23                         double[] rectcolorG = new double[maxrectnum];\r
24                         double[] rectcolorB = new double[maxrectnum];\r
25                         ///- 0 linkto BasicCode3_1a\r
26 \r
27                         Canvas window = new Canvas(Canvas.window);\r
28                         var img = new Image("logo.png");\r
29 \r
30 \r
31                         ///+ 1 linkto BasicCode3_1a\r
32                         ////Initialize\r
33                         for (int i = 0; i < rectnum; i++)\r
34                         {\r
35                                 ///+ 1.1 set1\r
36                                 ////set positions and sizes\r
37                                 rect[i].set(rectsize, rectsize); //Set a size of rectangles.\r
38                                 rect[i].centering();\r
39                                 rect[i].shift((i - 0.5 * rectnum) * rectsize * 1.5,\r
40                                                                                            (i - 0.5 * rectnum) * rectsize * 1.5); //Move Rectangles to initial positions\r
41                                 ///- 1.1 set1\r
42                                 ///+ 1.2 set2\r
43                                 ////set colors\r
44                                 rectcolorR[i] = Math.random(1.0) * 0.5; //Set R values. Note that "i" is converted to double-type.\r
45                                 rectcolorG[i] = Math.random(1.0) * 0.5; //Set G values. Note that "i" is converted to double-type.\r
46                                 rectcolorB[i] = Math.random(1.0) * 0.5; //Set B values. Note that "i" is converted to double-type.\r
47                                 ///- 1.2 set2\r
48                         }\r
49                         ///- 1 linkto BasicCode3_1a\r
50 \r
51                         ///+ 2 drawing\r
52                         ////drawing objects\r
53                         ///+ 2.1 linkto BasicCode3_2a\r
54                         ////Prepare variables for movie control;\r
55                         int frame = 0;\r
56                         int motion_dir = 1;\r
57                         double Horizontal_shift, Vertical_shift;\r
58                         ///- 2.1 linkto BasicCode3_2a\r
59 \r
60                         while (!Keyboard.esc.pushed())\r
61                         {\r
62                                 window.clear(Color.black);\r
63                                 for (int i = 0; i < rectnum; i++)\r
64                                 {\r
65                                         ///+ 2.2 loopset1\r
66                                         ////set positions and sizes\r
67 \r
68                                         Horizontal_shift = Math.sin(2 * Math.PI * ((double)frame / 30.0)) * motion_dir * 100.0; //Calculate horizontal displacement from the center.\r
69                                         Vertical_shift = Math.sin(2 * Math.PI * ((double)frame / 30.0)) * motion_dir * 0.0;//Calculate vertical displacement from the center.\r
70                                         rect[i].resize(rectsize, rectsize); //Resize rectangles.\r
71                                         rect[i].centering(); //Rectangles are moved to the center...\r
72                                         rect[i].shift(Horizontal_shift + (i - 0.5 * rectnum) * rectsize * 1.5,\r
73                                                                   Vertical_shift + (i - 0.5 * rectnum) * rectsize * 1.5); //and then move to designated positions\r
74                                         ///- 2.2 loopset1\r
75 \r
76                                         ///+ 2.3 linkto BasicCode3_2a\r
77                                         ////set colors \r
78                                         rectcolorR[i] = Math.random(1.0) * 0.5; //Set R values. Note that "i" is converted to double-type.\r
79                                         rectcolorG[i] = Math.random(1.0) * 0.5; //Set G values. Note that "i" is converted to double-type.\r
80                                         rectcolorB[i] = Math.random(1.0) * 0.5; //Set B values. Note that "i" is converted to double-type.\r
81                                         ///- 2.3 linkto BasicCode3_2a\r
82 \r
83                                 }\r
84                                 ///+ 2.4 linkto BasicCode3_1a\r
85                                 for (int i = 0; i < rectnum; i++)\r
86                                 {\r
87                                         rect[i].draw(new Color(rectcolorR[i], rectcolorG[i], rectcolorB[i])); //draw objects by designated colors.\r
88                                 }\r
89                                 window.flip();\r
90                                 ///- 2.4 linkto BasicCode3_1a\r
91                                 frame++;\r
92                         }\r
93                         ///- 2 drawing\r
94 \r
95                 }\r
96                 ///- Main Routine\r
97 \r
98 \r
99         }\r
100 \r
101 }\r
102 \r
103 \r
104 \r
105 \r
106 \r
107 /*\r
108 using Psychlops;\r
109 \r
110 namespace PsychlopsSilverlight4test\r
111 {\r
112 \r
113         public class PsychlopsMain\r
114         {\r
115                 void RectLuminance()\r
116                 {\r
117                         Canvas display = new Canvas(Canvas.window);\r
118                         Psychlops.Solver.BinomialLikelihood.showWindow(Math.cumulativeNormalDistibution);\r
119                         Letters le = new Letters("Reload to restart");\r
120                         le.fill = Color.black;\r
121                         le.align = Letters.HorizontalAlign.center;\r
122 \r
123                         while (!Keyboard.esc.pushed())\r
124                         {\r
125                                 Display.clear(Color.white);\r
126                                 le.centering().shift(-200,-10).draw();\r
127                                 Display.flip();\r
128                         }\r
129 \r
130                 }\r
131 \r
132 \r
133                 public void psychlops_main()\r
134                 {\r
135                         RectLuminance();\r
136                 }\r
137 \r
138 \r
139         }\r
140 \r
141 }\r
142 \r
143 */\r
144 \r
145 \r
146 \r
147 \r
148 \r
149 \r
150 \r
151 \r
152 \r
153 /*\r
154  using Psychlops;\r
155 \r
156 namespace PsychlopsSilverlight4test\r
157 {\r
158 \r
159         public class PsychlopsMain\r
160         {\r
161 \r
162                 Psychlops.Canvas cnvs;\r
163                 int i;\r
164                 double x, y, z, t, p, temp, xx, yy;\r
165 \r
166                 //Set Target Initial Value\r
167                 double TargetEcce = 100.0, TargetSize = 5.0, TargetNumber = 5, Rotate = 0.0;\r
168 \r
169                 //Set Background Initial Value\r
170                 double Axis = 0.0, BGRadii = 150, BGSize = 5.0;\r
171                 Psychlops.Widgets.Slider thetaSpeed, DotNumber;\r
172                 Image img;\r
173 \r
174 \r
175 \r
176                 public void psychlops_main()\r
177                 {\r
178                         cnvs = new Canvas(500, 500);\r
179 \r
180                         Psychlops.Solver.BinomialLikelihood.showWindow(Math.cumulativeNormalDistibution);\r
181 \r
182 \r
183                         img = new Image("Resources/logo.png");\r
184 \r
185                         Interval rng = new Interval();\r
186                         thetaSpeed = new Psychlops.Widgets.Slider("Label", -10 <= rng <= 10, 3.0);\r
187                         DotNumber  = new Psychlops.Widgets.Slider("DotNum",   0 <= rng <= 100, 50.0);\r
188 \r
189                         //Declare background dots and target\r
190                         Rectangle[] BGDot = new Rectangle[2048];\r
191                         for(int i=0; i<2048; i++){\r
192                                 BGDot[i]=new Rectangle();\r
193                         }\r
194                         Rectangle[] Target= new Rectangle[10];\r
195                         for(int i=0; i<10; i++){\r
196                                 Target[i]=new Rectangle();\r
197                         }\r
198 \r
199 \r
200                         //Declare Matrix to keep back ground dots' coordinate value\r
201                         double[] DotX=new double[2048];\r
202                         double[] DotY = new double[2048];\r
203 \r
204 \r
205                         //Set Independent variables to manipulate\r
206 \r
207 \r
208                         //Initialize positions of background dots\r
209                         for(int i=0; i<2048; i++){\r
210                                 t=2.0*Math.PI*Math.random(1.0);\r
211                                 p=2.0*Math.PI*Math.random(1.0);\r
212                                 DotX[i]=t;\r
213                                 DotY[i]=p;\r
214 \r
215                                 x=BGRadii*Math.cos(t)*Math.cos(p);\r
216                                 y=BGRadii*Math.sin(t)*Math.cos(p);\r
217                                 BGDot[i].set(BGSize, BGSize);\r
218                                 BGDot[i].centering().shift(x,y);\r
219                         }\r
220 \r
221                         //Initialize positions of targets\r
222                         for(int i=0; i<10; i++)Target[i].set(TargetSize, TargetSize);\r
223 \r
224                         double COS, SIN;\r
225                         //DotNumber = 200;\r
226                         //Main stimulus loop\r
227                         while(true){\r
228                                 //Clear the main window\r
229                                 cnvs.clear();\r
230 \r
231 \r
232                                 temp=Axis/360*2*Math.PI;\r
233                                 COS=Math.cos(temp);\r
234                                 SIN=Math.sin(temp);\r
235 \r
236                                 //Calculate positions of background dots and set them\r
237                                 for(int i=0; i<DotNumber; i++){\r
238                                         //Calculate in polar coordinate\r
239                                         t=DotX[i]+2.0*Math.PI*thetaSpeed/360.0;\r
240                                         p=DotY[i];\r
241 \r
242                                         //Save current position\r
243                                         DotX[i]=t;\r
244 \r
245                                         //Convert to Decartes coordinate\r
246                                         x=BGRadii*Math.cos(t)*Math.cos(p);\r
247                                         y=BGRadii*Math.sin(t)*Math.cos(p);\r
248                                         z=BGRadii*Math.sin(p);\r
249                                         xx=x;\r
250                                         yy=SIN*y+COS*z;\r
251 \r
252                                         //Set Dotsize and Draw them\r
253                                         BGDot[i].set(BGSize, BGSize);\r
254                                         BGDot[i].centering().shift(xx,yy);\r
255                                         BGDot[i].draw( Color.blue );\r
256                                 }\r
257 \r
258                                 //Calculate positions of targets and set them\r
259                                 temp=Rotate*2*Math.PI/360.0;\r
260                                 for (int i = 0; i < 5; i++)\r
261                                 {\r
262                                         t = 2.0 * Math.PI/TargetNumber;\r
263                                         x = TargetEcce*Math.cos(i*t+temp);\r
264                                         y = TargetEcce*Math.sin(i*t+temp);\r
265                                         Target[i].centering().shift(x,y);\r
266                                         Target[i].fill = Color.yellow;\r
267                                         Target[i].draw();\r
268                                 }\r
269 \r
270                                 img.centering(Mouse.position).draw();\r
271 \r
272                                 cnvs.var(Mouse.position.x, 100, 100);\r
273                                 cnvs.var(Mouse.position.y, 100, 140);\r
274 \r
275                                 //Reflect drawing at the next frame;\r
276                                 cnvs.flip();\r
277                         }\r
278                 }\r
279         }\r
280 \r
281 }\r
282 */\r
283 \r
284 \r
285 /*using Psychlops;\r
286 \r
287 namespace PsychlopsSilverlight4test\r
288 {\r
289         public class PsychlopsMain\r
290         {\r
291                 public void psychlops_main()\r
292                 {\r
293                         var figure_type = Psychlops.Widgets.Browser.Element.byID("FIGURE");\r
294                         var size_x = Psychlops.Widgets.Browser.Element.byID("SIZE_X");\r
295                         var size_y = Psychlops.Widgets.Browser.Element.byID("SIZE_Y");\r
296                         var shift_x = Psychlops.Widgets.Browser.Element.byID("SHIFT_X");\r
297                         var shift_y = Psychlops.Widgets.Browser.Element.byID("SHIFT_Y");\r
298                         var color_r = Psychlops.Widgets.Browser.Element.byID("COLOR_R");\r
299                         var color_g = Psychlops.Widgets.Browser.Element.byID("COLOR_G");\r
300                         var color_b = Psychlops.Widgets.Browser.Element.byID("COLOR_B");\r
301 \r
302                         Canvas window = new Canvas(300, 300);\r
303 \r
304                         var figure_r = new Rectangle();\r
305                         var figure_e = new Ellipse();\r
306                         Shape figure = figure_r;\r
307                         \r
308                         while (!Keyboard.esc.pushed())\r
309                         {\r
310                                 if (System.String.Compare(figure_type, "Rectangle") == 0)\r
311                                 {\r
312                                         figure_r.set(size_x, size_y);\r
313                                         figure = figure_r;\r
314                                 }\r
315                                 else\r
316                                 {\r
317                                         figure_e.set(size_x, size_y);\r
318                                         figure = figure_e;\r
319                                 }\r
320                                 figure.fill = new Color(color_r, color_g, color_b);\r
321 \r
322                                 window.clear(Color.black);\r
323                                 figure.centering().shift(shift_x, shift_y).draw();\r
324                                 window.flip();\r
325                         }\r
326                 }\r
327         }\r
328 \r
329 }\r
330 \r
331 \r
332 \r
333 \r
334 /*\r
335 using Psychlops;\r
336 \r
337 namespace Psychlops\r
338 {\r
339 \r
340         public class RandomDots\r
341         {\r
342                 Rectangle dot;\r
343                 public Point[] cood;\r
344                 public RandomDots()\r
345                 {\r
346                         cood = new Point[250];\r
347                         dot = new Rectangle(5, 5);\r
348                 }\r
349                 public void draw()\r
350                 {\r
351                         dot.fill = Color.white;\r
352                         foreach (Point p in cood)\r
353                         {\r
354                                 dot.centering(p).draw();\r
355                         }\r
356                 }\r
357         }\r
358 \r
359 }\r
360 \r
361 \r
362 \r
363 namespace PsychlopsSilverlight4test\r
364 {\r
365 \r
366         public class PsychlopsMain\r
367         {\r
368                 Canvas cnvs;\r
369                 Image img;\r
370                 Ellipse fixation;\r
371                 Shape shape;\r
372                 Color col;\r
373                 int isize = 100;\r
374                 double tfreq = 1;\r
375                 int frames;\r
376                 RandomDots dots;\r
377                 Group g;\r
378 \r
379                 public void psychlops_main()\r
380                 {\r
381                         cnvs = new Canvas(500, 500);\r
382                         g = new Group();\r
383                         img = new Image(isize * 2, isize * 2);\r
384                         Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60);\r
385                         g.append(img);\r
386                         g.rotation = 50;\r
387                         fixation = new Ellipse(10, 10);\r
388                         fixation.fill = Color.red;\r
389                         //var poly = new Rectangle(100, 100);\r
390                         var poly = new Letters("日本語");\r
391                         //var poly = new Polygon(); poly.append(0, 100); poly.append(-100, 0); poly.append(0, -100); poly.append(100, 0);\r
392                         //var poly = new Ellipse(100, 100);\r
393                         //var poly = new Line(0,0,100, 0);\r
394                         poly.fill = Color.red;\r
395                         poly.stroke = new Stroke { color = Color.yellow, thick = 1 };\r
396                         shape = poly;\r
397 \r
398                         var rng = new Interval();\r
399                         var slider = new Psychlops.Widgets.Slider("tesrt", -100 <= rng <= 100);\r
400 \r
401                         dots = new RandomDots();\r
402 \r
403 \r
404                         while (true)\r
405                         {\r
406                                 frames++;\r
407 \r
408 \r
409                                 cnvs.clear(new Color(Mouse.left.pressed() ? 0.75 : 0.5));\r
410                                 col.set(Math.random(1.0));\r
411 \r
412                                 /*if (frames % 2 == 0)\r
413                                 {\r
414                                         fixation.centering().shift(100, 100);\r
415                                         fixation.draw(new Stroke(Color.blue, 3));\r
416                                 }* /\r
417 \r
418                                 fixation.centering().shift(slider, 0);\r
419                                 fixation.draw(Color.red);\r
420 \r
421                                 //Figures.drawGabor(ref img, 20, 100, 1, 0, frames * 2.0 * Math.PI / tfreq / 60);\r
422                                 //Figures.drawGaussian(ref img, 20, 1);\r
423                                 //Figures.drawGrating(ref img, 200, 200, 20, 1, 2, frames * 2.0 * Math.PI / tfreq / 60);\r
424                                 //img.centering(Mouse.position);\r
425                                 //img.draw();\r
426                                 g.centering(Mouse.position).draw();\r
427                                 g.rotation += 1;\r
428 \r
429                                 shape.centering(Mouse.position).shift(100, 0);\r
430                                 shape.draw();\r
431                                 if (Keyboard.spc.pressed()) cnvs.var(Mouse.x, 100, 100);\r
432 \r
433                                 cnvs.var(frames, 0, 20);\r
434                                 //slider.value = frames / 100.0;\r
435 \r
436                                 /*if (frames % 2 != 0)\r
437                                 {\r
438                                         fixation.shift(100, 100);\r
439                                         fixation.draw(new Stroke(Color.green, 3));\r
440                                 }* /\r
441 \r
442                                 for (int i = 0; i < dots.cood.Length; i++)\r
443                                 {\r
444                                         dots.cood[i].set(Math.random(500), Math.random(500));\r
445                                 }\r
446                                 cnvs.var((double)slider, 200, 200);\r
447                                 dots.draw();\r
448 \r
449                                 cnvs.flip();\r
450                         }\r
451                 }\r
452         }\r
453 }\r
454 \r
455 **/\r
456 \r
457 \r
458 /*\r
459 \r
460 using Psychlops;\r
461 namespace PsychlopsSilverlight4test\r
462 {\r
463 \r
464         public class PsychlopsMain\r
465         {\r
466                 Canvas cnvs;\r
467                 Rectangle[] rect;\r
468                 int n;\r
469 \r
470                 public void psychlops_main()\r
471                 {\r
472                         n = 1;\r
473                         cnvs = new Canvas(500, 500);\r
474                         rect = new Rectangle[n];\r
475                         for (int i = 0; i < n; i++)\r
476                         {\r
477                                 rect[i] = new Rectangle(10, 10);\r
478                                 rect[i].fill = Color.red;\r
479                         }\r
480                         Interval rng = new Interval();\r
481                         var slider = new Psychlops.Widgets.Slider("Label", -100 <= rng <= 100);\r
482 \r
483                         while (true)\r
484                         {\r
485                                 cnvs.clear();\r
486                                 for (int i = 0; i < n; i++)\r
487                                 {\r
488                                         rect[i].centering().shift(slider * i*3, i*3).draw();\r
489                                 }\r
490                                 cnvs.flip();\r
491                         }\r
492                 }\r
493         }\r
494 }\r
495 \r
496 */