-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathuniverse.cpp
More file actions
85 lines (80 loc) · 1.97 KB
/
Copy pathuniverse.cpp
File metadata and controls
85 lines (80 loc) · 1.97 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
#include <iostream>
#include "compatibility.h"
#include "universe.h"
#ifndef PI
#ifdef M_PI
#define PI M_PI
#else
#define PI 3.1415926535
#endif
#endif
void gotoxy(int x, int y) {
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
COORD Pos = { x - 1, y - 1};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), Pos);
#else
printf("\033[%d;%dH", y + 1, x + 1);
#endif
}
Sun::Sun(int ax, int ay, char ach) {
x = ax;
y = ay;
initialCh = ach;
}
void Sun::Show() {
gotoxy(x, y);
putchar(initialCh);
}
void Sun::Hide() {
gotoxy(x, y);
putchar(' ');
}
int Sun::GetX() { return x; }
int Sun::GetY() { return y; }
Earth::Earth(int ar, char ach, Sun* apSun)
: r(ar), initialCh(ach), pSun(apSun) {
x = int(cos(0) * r * 2);
y = int(sin(0) * r);
}
void Earth::Revolve(short angle) {
Hide();
x = int(cos(angle * PI / 180) * r * 2);
y = int(sin(angle * PI / 180) * r);
Show();
}
void Earth::Show() {
gotoxy(pSun->GetX() + x, pSun->GetY() + y);
putchar(initialCh);
}
void Earth::Hide() {
gotoxy(pSun->GetX() + x, pSun->GetY() + y);
putchar(' ');
}
int Earth::GetX() { return pSun->GetX() + x; }
int Earth::GetY() { return pSun->GetY() + y; }
Moon::Moon(int ar, char ach, Earth* apEarth)
: r(ar), initialCh(ach), pEarth(apEarth) {
x = int(cos(0) * r * 2);
y = int(sin(0) * r);
}
void Moon::Revolve(short angle) {
Hide();
/*
* Earth can get the coordinate of the Sun each time when Show() and Hide() is called since
* the Sun is stationary.
*
* Because the Earth is a moving object, we should add pEarth->GetX/Y() from here.
* If not, previous 'M' character will not be removed upon drawing a new coordinate.
*/
x = pEarth->GetX() + int(cos(angle * M_PI / 180) * r * 2);
y = pEarth->GetY() + int(sin(angle * M_PI / 180) * r);
Show();
}
void Moon::Show() {
gotoxy(x, y);
putchar(initialCh);
}
void Moon::Hide() {
gotoxy(x, y);
putchar(' ');
}