动画生成与更新
// 在create_data()函数内部
int points_size = index;
for (int frame = 0; frame < frames; ++frame) {
for (index = 0; index < points_size; ++index) {
// 计算出的点的位置增加并更新坐标
double x = points[index].x, y = points[index].y;
double distance = sqrt(pow(x, 2) + pow(y, 2));
double distance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
double x_increase = distance_increase * x / distance / frames;
double y_increase = distance_increase * y / distance / frames;
points[index].x += x_increase;
points[index].y += y_increase;
// 使用XSetForeground和XFillArc绘制点
XSetForeground(display, gc, points[index].color);
XFillArc(display, win, gc, screen_x(points[index].x), screen_y(points[index].y), 2, 2, 0, 360 * 64);
}
for (double size = 17; size < 23; size += 0.3) {
for (index = 0; index < quantity; ++index) {
// 根据条件随机生成点的坐标和颜色,并使用XSetForeground和XFillArc绘制点
if ((create_random(0, 100) / 100.0 > 0.6 && size >= 20) || (size < 20 && (double)create_random(0, 100) / 100.0 > 0.95)) {
double x, y;
if (size >= 20) {
x = origin_points[index].x * size + create_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
y = origin_points[index].y * size + create_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
} else {
x = origin_points[index].x * size + create_random(-5, 5);
y = origin_points[index].y * size + create_random(-5, 5);
}
XSetForeground(display, gc, colors[create_random(0, 6)]);
XFillArc(display, win, gc, screen_x(x), screen_y(y), 2, 2, 0, 360 * 64);
}
}
}
}
points_size
用于获取当前动画帧中的点数,它是根据上一段代码计算得出的。index
是之前生成的点数。
- 外部循环
for (int frame = 0; frame < frames; ++frame)
用于遍历动画的每一帧,frames
指定了总帧数。
- 内部循环
for (index = 0; index < points_size; ++index)
用于处理当前帧中的每个点。在每一帧中,它执行以下操作:
double x = points[index].x, y = points[index].y;
double distance = sqrt(pow(x, 2) + pow(y, 2));
double distance_increase = -0.0009 * distance * distance + 0.35714 * distance + 5;
double x_increase = distance_increase * x / distance / frames;
double y_increase = distance_increase * y / distance / frames;
points[index].x += x_increase;
points[index].y += y_increase;
这些计算用于更新点的x和y坐标,以实现动画中点的运动。distance_increase
控制着点移动的速度,它随点到其原始位置的距离而变化。- 使用XSetForeground
和XFillArc
函数绘制点。这将点绘制到屏幕上,XSetForeground
用于设置绘图颜色,XFillArc
用于绘制填充的点,并且圆的中心坐标通过screen_x
和screen_y
函数进行转换。4. 内部循环的第二部分for (double size = 17; size < 23; size += 0.3)
用于在当前帧中生成额外的点。在这个循环中,每个点被生成、着色并绘制到屏幕上。- 新点的坐标和颜色根据以下条件随机生成:
如果size >= 20
且随机数大于0.6
,或者size < 20
且随机数大于0.95
,则生成一个新点。
- 生成点的x和y坐标是根据原始点的位置和一些随机偏移量计算得出的。
double x, y;
if (size >= 20) {
x = origin_points[index].x * size + create_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
y = origin_points[index].y * size + create_random(-frame * frame / 5 - 15, frame * frame / 5 + 15);
} else {
x = origin_points[index].x * size + create_random(-5, 5);
y = origin_points[index].y * size + create_random(-5, 5);
}
- 最后,使用`XSetForeground`和`XFillArc`函数将生成的点绘制到屏幕上,就像之前的点一样。