root/trunk/qd/test6.d

Revision 620, 2.1 kB (checked in by FeepingCreature, 3 years ago)
  • Forgot one
Line 
1 module test6;
2
3 import qd, std.math, SDL_ttf;
4 import tools.base, std.stdio, std.stdio: format;
5
6 alias cdouble cfn;
7
8 void transform(T...)(T thing) {
9   static if (is(T[1]: creal)) {
10     int t1, t2;
11     transform(thing[0], (int k, int l) { t1=k; t2=l; });
12     transform(thing[1], (int k, int l) { foreach (th; thing[2..$]) th(t1, t2, k, l); });
13   } else {
14     auto x=cast(int)((thing[0].re*0.5*screen.width+screen.width)*0.5);
15     auto y=cast(int)((thing[0].im*-0.5*screen.height+screen.height)*0.5);
16     foreach (th; thing[1..$]) th(x, y);
17   }
18 }
19 pt toScreen(creal c) {
20   return pt(cast(int)((c.re*0.5*screen.width+screen.width)*0.5), cast(int)((c.im*0.5*screen.height+screen.height)*0.5));
21 }
22
23 struct _call(alias T) { typeof(T(TupleInit!(U))) opCall(U...)(U u) { return T(u); } }
24
25 template call(alias T) { _call!(T) call; }
26
27 void main() {
28   // init("FreeSans.ttf");
29   screen(640, 480);
30   flip=false;
31   float step=0.004;
32   for (float i=0; true; i+=step) {
33     if (i>2*PI) i-=2*PI;
34     line(0, 0, 640, 480, Fill=Black);
35     transform(0+0i, call!(pset), (int x, int y) { print(x, y, Bottom|Right, "a"); });
36     transform(cos(i)+0i, call!(line), call!(line), (int x, int y) { print(x, y, Bottom|Right, "y"); });
37     transform(cos(i)+sin(i)*1i, call!(line), (int x, int y) { print(x, y, Bottom|Right, "b"); });
38     transform(0+0i, call!(line));
39     transform(1+0i, call!(pset));
40     for (float j=0; j<i; j+=step) transform(cos(j)+sin(j)*1i, call!(line));
41    
42     print(10, 400, Top|Right, format("sin a = ", cast(int)(sin(i)*100)/100.0));
43     pset(10, 440); for (float j=0; j<i; j+=step) line(10+j*10, 440-sin(j)*40);
44    
45     print(330, 400, Top|Right, format("cos a = ", cast(int)(cos(i)*100)/100.0));
46     pset(330, 400); for (float j=0; j<i; j+=step) line(330+j*10, 440-cos(j)*40);
47     auto a = i*(180.0/PI), _a = a;
48     if (_a > 180) _a -= 180;
49     if (_a > 90) _a = 180 - _a;
50     auto b = 90.0 - _a;
51     print(10, 10, Bottom|Right, format("a = ", cast(int)(a*100)/100.0, "°"));
52    
53     print(10, 50, Bottom|Right, format("b = ", cast(int)(b*100)/100.0, "°"));
54     print(10, 90, Bottom|Right, format("y = 90°"));
55     flip; events;
56   }
57   while (true) { flip; events; }
58 }
Note: See TracBrowser for help on using the browser.