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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
| #include <stdlib.h>
#include <time.h>
#include <grx20.h>
#include <grxkeys.h>
#include <math.h>
struct TPoint{
int x,y;
int w,h;
int r;
int color;
int vx,vy;
float va,vs;
float a, s;
TPoint(int ix,int iy,int icolor);
virtual ~TPoint();
virtual void Draw(int icolor);
void Show();
void Hide();
void MoveXY(int ix, int iy);
};
struct TBox: public TPoint{
TBox(int ix,int iy,int iw,int ih,int icolor);
virtual ~TBox();
virtual void Draw(int icolor);
virtual void SizeX (int iw, int ix);
virtual void SizeY (int ih, int iy);
};
struct TCircle: public TPoint{
TCircle(int ix,int iy,int icolor,int ir);
virtual ~TCircle();
virtual void Draw(int icolor);
virtual void SizeR (int ir);
};
struct TLine: public TPoint {
TLine(int ix, int iy, int icolor, int ir);
virtual ~TLine();
virtual void Draw(int icolor);
virtual void SizeR(int ir);
};
TPoint::TPoint(int ix,int iy,int icolor){
color=icolor;
x=ix; y=iy;
a=0; s=0;
vx=rand()%7-3;
vy=rand()%7-3;
va=0.05;
vs=0.5;
}
TPoint::~TPoint()
{Hide();}
void TPoint::Draw(int icolor){
GrPlot(x,y,icolor);
GrPlot(x+1,y,icolor);
GrPlot(x,y+1,icolor);
GrPlot(x+1,y+1,icolor);
}
void TPoint::Show()
{Draw(color);}
void TPoint::Hide()
{Draw(0);}
void TPoint::MoveXY(int ix,int iy)
{
Hide();
//x=ix;
//y=iy;
//if (ix >= GrMaxX()-1){x = 1;};
//if (ix <= 1){x = GrMaxX()-1;};
//if (iy >= GrMaxY()-1){y = 0;};
//if (iy <= 1){y = GrMaxY()-1;};
a=a+va;
s=s+vs;
x=(GrMaxX()/2+s*cos(a));
y=(GrMaxY()/2-s*sin(a));
Show();
}
TBox::TBox(int ix,int iy,int iw,int ih,int icolor):TPoint(ix,iy,icolor)
{w=iw; h=ih;}
TBox::~TBox(){Hide();}
void TBox::Draw(int icolor)
{
GrBox(x-w/2,y-h/2,x+(w-w/2),y+(h-h/2),icolor);
}
void TBox::SizeX(int ix,int iw)
{w=rand()%49+1;};
void TBox::SizeY(int iy,int ih)
{h=rand()%49+1;};
TLine::TLine(int ix,int iy, int icolor, int ir): TPoint(ix,iy,icolor)
{r=ir;}
TLine::~TLine(){Hide();}
void TLine::Draw(int icolor)
{
GrLine(x-r,y-r,x+r,y+r,icolor);
}
void TLine::SizeR(int ir)
{r=rand()%49+1;};
TCircle::TCircle(int ix,int iy,int icolor,int ir): TPoint(ix,iy,icolor)
{r=ir;}
TCircle::~TCircle()
{Hide();}
void TCircle::Draw(int icolor)
{GrEllipse(x,y,r/2,r,icolor);}
void TCircle::SizeR(int ir)
{r=rand()%49+1;};
#define M 100
main()
{int N=3;
TPoint *obj[M];
GrSetMode(GR_default_graphics);
srand(time(0));
obj[0]=new TPoint(GrMaxX()/2,GrMaxY()/2,0);
obj[1]=new TPoint(GrMaxX()/2,GrMaxY()/2,0);
for(int i=2; i<N; i++){
switch(rand()%4){
case 0:obj[i]=new TPoint(GrMaxX()/2,GrMaxY()/2,9);break;
case 1:obj[i]=new TBox(GrMaxX()/2,GrMaxY()/2,rand()%15+10,rand()%15+10,12);break;
case 2:obj[i]=new TCircle(GrMaxX()/2,GrMaxY()/2,10, rand()%15+2);break;
case 3:obj[i]=new TLine(GrMaxX()/2,GrMaxY()/2,10, rand()%15+2);break;
}
obj[i]->Show();
}
GrKeyType key;
do{
while (!GrKeyPressed()){
GrSleep(40);
for(int i=0; i<N; i++){
obj[i]->MoveXY(obj[i]->x+obj[i]->vx,obj[i]->y+obj[i]->vy);
if (rand()%100==50){
obj[i]->vx=rand()%11-5; obj[i]->vy=rand()%11-5;
}
}
}
key=GrKeyRead();
switch(key){
case GrKey_Insert:
if (N<M){
switch(rand()%4){
case 0:obj[N]=new TPoint(GrMaxX()/2,GrMaxY()/2,9);break;
case 1:obj[N]=new TBox(GrMaxX()/2,GrMaxY()/2,rand()%15+10,rand()%15+10,12);break;
case 2:obj[N]=new TCircle(GrMaxX()/2,GrMaxY()/2,10, rand()%15+2);break;
case 3:obj[N]=new TLine(GrMaxX()/2,GrMaxY()/2,10, rand()%15+2);break;
}
N++;
}
break;
case GrKey_Delete:
if (N>0){
delete obj[N-1];
N--;
}
break;
}
}
while(key!=GrKey_Escape);
for(int i=0; i<N; i++){
delete obj[i];}
} |