循迹的实现思路 | 轻流扇
0%

循迹的实现思路

基于BC的循迹动画的一种实现思路

preface: 总所周知,在BC中实现动画,一般只能实现一些比较简单的直线,或者特定的斜线运动,但是如果要实现完全不确定的斜线运动,或者更加复杂的运动,至少我完全没有思路。所以呢,在大概4月份,受到了小车循迹的启发,便写出了下面的代码,作为一种思路。你可以发现,我队友实现的无人机避障等功能就是依据下面的思路改进完善的。原理很简单,相信理解起来不难,但是想完善、优化并非一朝一夕之事。

代码如下,只是演示思路,所以并没有很完善

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

#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#include<bios.h>
#include<string.h>
#include<dos.h>
#include<math.h>
#include<time.h>
#include <conio.h>
//画无人机的函数,不用看,不用看,不用看,不用看,不用看,不用看。
void drfdrone(int x, int y)//xy是左上角坐标
{
setcolor(DARKGRAY);
setfillstyle(1, DARKGRAY); //circle(x + 12, y + 12, 4);
fillellipse(x + 12, y + 12, 4, 4);
rectangle(x + 8, y + 9, x + 15, y + 15);// floodfill(x + 12, y + 12, DARKGRAY);
line(x + 5, y + 4, x + 9, y + 8);
line(x + 3, y + 4, x + 9, y + 9);
line(x + 3, y + 5, x + 8, y + 10);
line(x + 9, y + 1, x + 1, y + 9);
line(x + 8, y + 1, x + 0, y + 9);
line(x + 7, y + 1, x + 0, y + 8);
line(x + 15, y + 8, x + 19, y + 4);
line(x + 15, y + 9, x + 20, y + 4);
line(x + 15, y + 10, x + 20, y + 5);
line(x + 15, y + 0, x + 24, y + 9);
line(x + 16, y + 0, x + 24, y + 8);
line(x + 14, y + 1, x + 23, y + 9);
line(x + 16, y + 15, x + 21, y + 20);
line(x + 15, y + 15, x + 20, y + 20);
line(x + 14, y + 16, x + 20, y + 21);
line(x + 24, y + 15, x + 15, y + 24);
line(x + 24, y + 16, x + 16, y + 24);
line(x + 24, y + 14, x + 14, y + 24);
line(x + 9, y + 15, x + 3, y + 21);
line(x + 8, y + 15, x + 3, y + 20);
line(x + 9, y + 16, x + 4, y + 21);
line(x + 0, y + 15, x + 9, y + 24);
line(x + 0, y + 16, x + 8, y + 24);
line(x + 0, y + 14, x + 10, y + 24);
}

//简单的
void move11(int* x, int* y, int* buffer1)
{
//这里的xy 是无人机现在的坐标
int a[3][3];//储存无人机坐标3*3的正方形区域点的颜色,9个点
int i;
int j;
int temp1, temp2;
int judge = 1;
int dx, dy;
//下面应该是核心功能的实现思路。。。。。。
while (1)
{
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
temp1 = i - 1;
temp2 = j - 1;
a[i][j] = getpixel(*x + temp1, *y + temp2);//注意这里实现了对 无人机坐标 (正方形中心)边长为5的矩形区域点列
//的颜色get
if (a[i][j] == BLUE)
{
judge = 0;
}
}
}
getimage(*x, *y, *x + 25, *y + 25, buffer1);
drfdrone(*x, *y);
delay(8);
putimage(*x, *y, buffer1, COPY_PUT);
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
if (a[i][j] == BLUE)
{
temp1 = i - 1;
temp2 = j - 1;
dx = temp1;
dy = temp2;
putpixel(*x, * y, RED);//无人机运动过后,清除该点颜色。
//注意这里没考虑路线交叉的情况。。。。。。。
*x += dx;
*y += dy;
}
}
}
}
}



void dronemove1(void)//这里不是核心
{
//-========================================================
void* buffer1 = malloc(imagesize(0, 0, 25, 25));
int* x1 = malloc(sizeof(int));
int* y1 = malloc(sizeof(int));
int i;
*x1 = 100;//注意这里的初始化
*y1 = 100;
move11(x1, y1, buffer1);
free(buffer1);
free(x1);
free(y1);
}


//主函数
void main(void)
{
int gd = VGA;//graphdriver
int gm = VGAHI;//graphmode
initgraph(&gd, &gm, "C: \\BORLANDC\\BGI");//这里后面的C:什么的看放的BGI文件路径
setbkcolor(WHITE);//background color
setcolor(RED);
setfillstyle(1, RED);
rectangle(600, 0, 640, 40);
line(600, 0, 640, 40);
line(640, 0, 600, 40);//右上角画 退出
setcolor(GREEN);
line(80, 100, 520, 100);//画标题
rectangle(220, 120, 420, 200);
rectangle(220, 240, 420, 320);
rectangle(220, 360, 420, 440);
//++++++++++++++
//这里应该是运动的路径,可以随意更改。这里的循迹是根据颜色来的,蓝色为路径,无人机行驶过后就会变色
setcolor(BLUE);
line(100, 100, 200, 200);
line(200, 200, 400, 300);
while (1)
{
dronemove1();//动画,加while应该是便于观察。
}

closegraph();
}

这里的演示没有考虑到路线交叉等复杂情况,具体怎么完善可以看看C课设拯救计划中我队友的代码(具体文件是哪个我也不知道,或者看演示也行)。这里的主要原理是先画出路径的线(假定为蓝色),然后编写一个函数判断无人机这个点 旁边的点的颜色,使得无人机向周围蓝色的点移动,然后把这个点换别的颜色。(我现在也看不懂我的代码写的啥。思路就是这样

bye~~~

——8.05.2024

留下万分之一点,采得孤人所笑言