Skip to content

Commit 08b29dd

Browse files
committed
update
1 parent 119a223 commit 08b29dd

File tree

6 files changed

+197
-93
lines changed

6 files changed

+197
-93
lines changed

.vscode/settings.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@
9999
"iterator": "cpp",
100100
"memory_resource": "cpp",
101101
"utility": "cpp",
102-
"stop_token": "cpp"
102+
"stop_token": "cpp",
103+
"__availability": "cpp"
103104
},
104-
"C_Cpp.errorSquiggles": "Disabled"
105+
"C_Cpp.errorSquiggles": "Enabled"
105106
}

problems/tangram/HISTORY.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# HISTORY
2+
3+
## 1.1 (2022-12-04)
4+
5+
* Add video animation
6+
7+
## 1.0 (2022-12-03)
8+
9+
* Basic draw starting position

problems/tangram/game.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#pragma once
2+
#include <iostream>
3+
#include <vector>
4+
5+
#include "polygon.h"
6+
7+
// using namespace std;
8+
9+
class Game {
10+
public:
11+
std::vector<Polygon> polygons;
12+
Polygon target;
13+
14+
Game() {
15+
std::vector<point> vertices;
16+
float vx, vy;
17+
18+
vertices = {mp(0.25, 0), mp(0.25, 0.5), mp(0.5, 0.75), mp(0.75, 0.5),
19+
mp(0.75, 0)};
20+
Polygon p1 = Polygon("1", vertices, COLORS::GREEN);
21+
22+
vertices = {mp(0, 0), mp(0, 0.25), mp(0.25, 0.5), mp(0.25, 0)};
23+
Polygon p2 = Polygon("2", vertices, COLORS::PINK);
24+
25+
vertices = {mp(0.75, 0), mp(0.75, 0.5), mp(1, 0.25), mp(1, 0)};
26+
Polygon p3 = Polygon("3", vertices, COLORS::RED);
27+
28+
vertices = {mp(0.5, 0.75), mp(0.75, 1), mp(1, 0.75), mp(1, 0.25)};
29+
Polygon p4 = Polygon("4", vertices, COLORS::BLUE);
30+
31+
vertices = {mp(0, 0.25), mp(0, 0.75), mp(0.5, 1.25), mp(0.75, 1)};
32+
Polygon p5 = Polygon("5", vertices, COLORS::CYAN);
33+
34+
vertices = {mp(0.5, 1.25), mp(1, 1.25), mp(1, 0.75)};
35+
Polygon p6 = Polygon("6", vertices, COLORS::YELLOW);
36+
37+
vertices = {mp(0, 0.75), mp(0, 1.25), mp(0.5, 1.25)};
38+
Polygon p7 = Polygon("7", vertices, COLORS::AMBER);
39+
40+
vertices = {mp(0, 0), mp(0, 1.25), mp(1, 1.25), mp(1, 0)};
41+
this->target = Polygon("target", vertices, COLORS::LIGHT_GRAY);
42+
43+
std::vector<Polygon> polygons = {p1, p2, p3, p4, p5, p6, p7};
44+
this->polygons = polygons;
45+
for(auto &p: this->polygons){
46+
p.SetRandomVelocity();
47+
}
48+
};
49+
void display() {
50+
cout << "Call from Game::display -> Game Display" << endl;
51+
this->target.display();
52+
for (auto &p : this->polygons) {
53+
p.display();
54+
}
55+
}
56+
57+
void Run() { cout << "Running" << endl; }
58+
};

problems/tangram/makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
all: clean build run
2+
3+
build:
4+
g++ -std=c++17 solution.cpp -o solution -framework GLUT -framework OpenGL -Wno-deprecated-declarations
5+
6+
run:
7+
./solution
8+
9+
clean:
10+
rm -rf solution

problems/tangram/polygon.h

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <vector>
5+
6+
#define mp std::make_pair
7+
#define point std::pair<float, float>
8+
9+
using namespace std;
10+
11+
struct Color {
12+
float r;
13+
float g;
14+
float b;
15+
};
16+
17+
class COLORS {
18+
public:
19+
static const Color RED, BLACK, WHITE, PINK, PURPLE, GREEN, BLUE, YELLOW,
20+
AMBER, CYAN, GRAY, LIGHT_GRAY;
21+
};
22+
23+
const Color COLORS::WHITE = {1.0f, 1.0f, 1.0f};
24+
const Color COLORS::BLACK = {0.0f, 0.0f, 0.0f};
25+
const Color COLORS::RED = {0.96, 0.26f, 0.21f};
26+
const Color COLORS::PINK = {0.91f, 0.12f, 0.39f};
27+
const Color COLORS::PURPLE = {0.61f, 0.15f, 0.69f};
28+
const Color COLORS::GREEN = {0.30f, 0.69f, 0.31f};
29+
const Color COLORS::BLUE = {0.13f, 0.59f, 0.95f};
30+
const Color COLORS::YELLOW = {1.00f, 0.92f, 0.23f};
31+
const Color COLORS::AMBER = {1.00f, 0.76f, 0.03f};
32+
const Color COLORS::CYAN = {0.00f, 0.74f, 0.83f};
33+
const Color COLORS::GRAY = {0.62f, 0.62f, 0.62f};
34+
const Color COLORS::LIGHT_GRAY = {0.96f, 0.96f, 0.96f};
35+
36+
class Polygon {
37+
public:
38+
vector<point> vertices;
39+
string id;
40+
Color color;
41+
point velocity;
42+
point position;
43+
int iter;
44+
45+
Polygon(){};
46+
47+
Polygon(std::string id, std::vector<point> v, Color color) {
48+
this->id = id;
49+
this->vertices = v;
50+
this->color = color;
51+
this->velocity = mp(0.0, 0.0);
52+
this->position = mp(0.0, 0.0);
53+
this->iter = 0;
54+
}
55+
56+
void SetVelocity(point velocity) { this->velocity = velocity; }
57+
58+
void SetRandomVelocity(){
59+
float vx = ((rand() % 1000) - 500) * 0.00001;
60+
float vy = ((rand() % 1000) - 500) * 0.00001;
61+
vx *= 2;
62+
vy *= 2;
63+
this->velocity = mp(vx, vy);
64+
}
65+
66+
void display() {
67+
glBegin(GL_POLYGON);
68+
point screen_translate = mp(-0.5, -0.7);
69+
float vx = this->velocity.first;
70+
float vy = this->velocity.second;
71+
this->iter += 1;
72+
this->position = mp(this->velocity.first + this->position.first,
73+
this->velocity.second + this->position.second);
74+
if(this->position.first > 1.1 || this->position.first < -1.1){
75+
vx = -this->velocity.first;
76+
}
77+
if(this->position.second > 1.1 || this->position.second < -1.1){
78+
vy = -this->velocity.second;
79+
}
80+
if(this->iter > 1000){
81+
this->position = mp(0.0, 0.0);
82+
}
83+
this->velocity = mp(vx, vy);
84+
cout << this->id << endl;
85+
cout << "(x, y) = " << this->position.first << ", " << this->position.second
86+
<< endl;
87+
glColor3f(this->color.r, this->color.g, this->color.b);
88+
89+
for (auto p : this->vertices) {
90+
float x = p.first + screen_translate.first + this->position.first;
91+
float y = p.second + screen_translate.second + this->position.second;
92+
93+
glVertex2d(x, y);
94+
}
95+
glEnd();
96+
}
97+
};

problems/tangram/solution.cpp

Lines changed: 20 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
#include <string>
77
#include <utility>
88
#include <vector>
9-
// #include <GL/freeglut.h>
10-
// #include <GL/glew.h>
11-
// #include <GL/freeglut.h>
12-
// #include <OpenGL/gltex.h>
9+
10+
#include "game.h"
11+
#include "polygon.h"
1312

1413
#define mp std::make_pair
1514
#define point std::pair<float, float>
@@ -19,28 +18,7 @@ using namespace std;
1918
const int WINDOW_WIDTH = 600;
2019
const int WINDOW_HEIGHT = 600;
2120

22-
struct Color {
23-
float r;
24-
float g;
25-
float b;
26-
};
27-
28-
class COLORS {
29-
public:
30-
static const Color RED, BLACK, WHITE, PINK, PURPLE, GREEN, BLUE, YELLOW,
31-
AMBER, CYAN;
32-
};
33-
// Color COLORS::RED = 4;
34-
const Color COLORS::WHITE = {1.0f, 1.0f, 1.0f};
35-
const Color COLORS::BLACK = {0.0f, 0.0f, 0.0f};
36-
const Color COLORS::RED = {0.96, 0.26f, 0.21f};
37-
const Color COLORS::PINK = {0.91f, 0.12f, 0.39f};
38-
const Color COLORS::PURPLE = {0.61f, 0.15f, 0.69f};
39-
const Color COLORS::GREEN = {0.30f, 0.69f, 0.31f};
40-
const Color COLORS::BLUE = {0.13f, 0.59f, 0.95f};
41-
const Color COLORS::YELLOW = {1.00f, 0.92f, 0.23f};
42-
const Color COLORS::AMBER = {1.00f, 0.76f, 0.03f};
43-
const Color COLORS::CYAN = {0.00f, 0.74f, 0.83f};
21+
Game game;
4422

4523
void init() {
4624
glutInitWindowPosition(100, 100);
@@ -49,30 +27,10 @@ void init() {
4927
Color c = COLORS::BLACK;
5028
glClearColor(c.r, c.g, c.b, 1.0);
5129
glMatrixMode(GL_PROJECTION);
30+
game = Game();
5231
}
5332

54-
class Polygon {
55-
public:
56-
std::vector<point> vertices;
57-
Color color;
58-
59-
Polygon(std::vector<point> v, Color color) {
60-
this->vertices = v;
61-
this->color = color;
62-
}
63-
64-
void display() {
65-
glBegin(GL_POLYGON);
66-
glColor3f(this->color.r, this->color.g, this->color.b);
67-
for (auto p : this->vertices) {
68-
glVertex2d(p.first - 0.5, p.second - 0.7);
69-
}
70-
glEnd();
71-
}
72-
};
73-
7433
void drawText(std::string text, int x, int y) {
75-
// glLoadIdentity();
7634
gluOrtho2D(0.0, WINDOW_WIDTH, 0.0, WINDOW_HEIGHT);
7735
Color c = COLORS::RED;
7836
glColor3f(c.r, c.g, c.b);
@@ -82,63 +40,34 @@ void drawText(std::string text, int x, int y) {
8240
}
8341
}
8442

85-
class Game {
86-
public:
87-
vector<Polygon> polygons;
88-
Game(vector<Polygon> polygons) { this->polygons = polygons; };
89-
void display() {
90-
for (auto p : this->polygons) {
91-
p.display();
92-
}
93-
}
94-
};
95-
9643
void display() {
97-
glClear(GL_COLOR_BUFFER_BIT);
98-
99-
std::vector<point> vertices;
100-
101-
vertices = {mp(0.25, 0), mp(0.25, 0.5), mp(0.5, 0.75), mp(0.75, 0.5),
102-
mp(0.75, 0)};
103-
Polygon p1 = Polygon(vertices, COLORS::GREEN);
104-
105-
vertices = {mp(0, 0), mp(0, 0.25), mp(0.25, 0.5), mp(0.25, 0)};
106-
Polygon p2 = Polygon(vertices, COLORS::PINK);
107-
108-
vertices = {mp(0.75, 0), mp(0.75, 0.5), mp(1, 0.25), mp(1, 0)};
109-
Polygon p3 = Polygon(vertices, COLORS::RED);
110-
111-
vertices = {mp(0.5, 0.75), mp(0.75, 1), mp(1, 0.75), mp(1, 0.25)};
112-
Polygon p4 = Polygon(vertices, COLORS::BLUE);
113-
114-
vertices = {mp(0, 0.25), mp(0, 0.75), mp(0.5, 1.25), mp(0.75, 1)};
115-
Polygon p5 = Polygon(vertices, COLORS::CYAN);
116-
117-
vertices = {mp(0.5, 1.25), mp(1, 1.25), mp(1, 0.75)};
118-
Polygon p6 = Polygon(vertices, COLORS::YELLOW);
119-
120-
vertices = {mp(0, 0.75), mp(0, 1.25), mp(0.5, 1.25)};
121-
Polygon p7 = Polygon(vertices, COLORS::AMBER);
122-
123-
vector<Polygon> polygons = {p1, p2, p3, p4, p5, p6, p7};
124-
125-
Game game = Game(polygons);
44+
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
45+
cout << "Call display " << endl;
12646
game.display();
12747
drawText("TANGRAMS", 250.0f, 50.0f);
128-
129-
glFlush();
48+
glLoadIdentity();
49+
glutSwapBuffers();
13050
}
13151

13252
void reshape() { std::cout << "Reshape " << std::endl; }
13353

54+
void GameLoop(int){
55+
std::cout << "Game Loop " << std::endl;
56+
glutPostRedisplay();
57+
glutTimerFunc(1000/60, GameLoop, 0);
58+
}
59+
13460
int main(int argc, char** argv) {
13561
glutInit(&argc, argv);
13662
glutInitDisplayMode(GLUT_RGB);
137-
13863
init();
13964
glutDisplayFunc(display);
14065
// glutReshapeFunc(reshape);
141-
66+
67+
glutTimerFunc(3000, GameLoop, 0);
14268
glutMainLoop();
69+
// cout << "Timer after MainLoop" << endl;
70+
// glutTimerFunc(3000, GameLoop, 0);
71+
14372
return 0;
14473
}

0 commit comments

Comments
 (0)