-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample5.cc
More file actions
109 lines (89 loc) · 2.53 KB
/
example5.cc
File metadata and controls
109 lines (89 loc) · 2.53 KB
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
#include "cpptk.h"
#include <string>
#include <iostream>
#include <cmath>
#include <queue>
using namespace Tk;
using namespace std;
// parameters of the animation
int initLen = 100;
int delay = 50;
double x1d = 0.02;
double y1d = 0.03;
double x2d = 0.04;
double y2d = 0.05;
double rd = 0.01;
double gd = 0.02;
double bd = 0.03;
// queue of lines
queue<string> lines;
// this function creates and draws a new line
void newLine()
{
static double x1a = 0.0;
static double y1a = 0.0;
static double x2a = 0.0;
static double y2a = 0.0;
static double ra = 0.0;
static double ga = 0.0;
static double ba = 0.0;
// get the current size of the canvas
int w = winfo(width, ".c");
int h = winfo(height, ".c");
// compute the coordinates of the new line
Point p1(static_cast<int>(w / 2 * (1 + sin(x1a))),
static_cast<int>(h / 2 * (1 + sin(y1a))));
Point p2(static_cast<int>(w / 2 * (1 + sin(x2a))),
static_cast<int>(h / 2 * (1 + sin(y2a))));
// compute the color of the new line
int r = static_cast<int>(127 * (1 + sin(ra)));
int g = static_cast<int>(127 * (1 + sin(ga)));
int b = static_cast<int>(127 * (1 + sin(ba)));
// draw the line and add its id to the queue
lines.push(".c" << create(line, p1, p2) -Tk::fill(rgb(r, g, b)));
x1a += x1d;
y1a += y1d;
x2a += x2d;
y2a += y2d;
ra += rd;
ga += gd;
ba += ::bd;
}
// note: this is used so that there is
// only one callback registration
string afterCommand;
// this function makes each step of the animation
void nextStep()
{
newLine();
// remove the oldest line from the queue and from the screen
".c" << deleteitem(lines.front());
lines.pop();
// call me again
after(delay, afterCommand);
}
int main(int, char *argv[])
{
try
{
init(argv[0]);
// create the canvas widget
pack(canvas(".c") -background("black"))
-expand(true) -Tk::fill(both);
update();
// create the first initLen lines
for (int i = 0; i != initLen; ++i)
{
newLine();
}
// register the callback function
afterCommand = callback(nextStep);
// start animation
after(delay, afterCommand);
runEventLoop();
}
catch (exception const &e)
{
cerr << "Error: " << e.what() << '\n';
}
}