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
|
#include <stdio.h>
#include <stdbool.h>
#ifdef PLATFORM_WEB
#include RAYLIB_SRC
#else
#include <raylib.h>
#endif
// game coords to screen coords
#define GTOS(n) ((n) * (SH) / 2)
#define GTOSV(n) ((Vector2){(GTOS(n.x)), (GTOS(n.y))})
// screen coords to game coords
#define STOG(n) ((n) / (SH) * 2)
#define STOGV(n) ((Vector2){(STOG(n.x)), (STOG(n.y))})
#define IN_RANGE(x, y, z) (((x) > ((y) - (z))) && ((x) < ((y) + (z))))
#define IN_RANGEV(a, b, c) ((IN_RANGE(a.x, b.x, c)) && (IN_RANGE(a.y, b.y, c)))
int SW = 1200;
int SH = 800;
int MAX_ITER = 50;
Vector2 a = {-1, 0};
Vector2 b = { 1, 0};
char text[1024];
Vector2 median(Vector2 p1, Vector2 p2)
{
return (Vector2){(p1.x + p2.x)/2, (p1.y + p2.y)/2};
}
void calc_points(Vector2 p1, Vector2 p2, Vector2 q)
{
static int iter = 0;
if(iter++ >= MAX_ITER) {
sprintf(text, "iter: %d; x: %f; y: %f", iter-1, p1.x, -p1.y);
iter = 0;
return;
}
DrawLineV(GTOSV(p1), GTOSV(p2), (iter % 2 == 0) ? RED : BLUE);
calc_points(q, median(p1, p2), p2);
}
int main(void)
{
InitWindow(SW, SH, "circle thingy");
SetTargetFPS(60);
SetWindowState(FLAG_WINDOW_RESIZABLE);
printf("\n----------------------- Instructions -----------------------\n");
printf("Use WASD to move around\n");
printf("Use Q and E increase or decrease the iterations\n");
printf("Use the mouse to move the points and to zoom in or out\n");
printf("------------------------------------------------------------\n");
Camera2D camera = {0};
camera.zoom = 1.0f;
while (!WindowShouldClose())
{
// for resizing
SW = GetScreenWidth();
SH = GetScreenHeight();
camera.offset = (Vector2){SW/2.0f, SH/2.0f};
if(IsKeyDown(KEY_W)) camera.target.y -= 500.0f * GetFrameTime();
if(IsKeyDown(KEY_A)) camera.target.x -= 500.0f * GetFrameTime();
if(IsKeyDown(KEY_S)) camera.target.y += 500.0f * GetFrameTime();
if(IsKeyDown(KEY_D)) camera.target.x += 500.0f * GetFrameTime();
if(IsKeyPressed(KEY_Q)) MAX_ITER++;
if(IsKeyPressed(KEY_E)) MAX_ITER--;
camera.zoom += ((float)GetMouseWheelMove()*0.05f);
static bool a_moving = false;
static bool b_moving = false;
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode2D(camera);
DrawCircleLines(0, 0, GTOS(1), BLACK);
DrawLine(GTOS(0), GTOS(1), GTOS(0), GTOS(-1), BLACK);
DrawLine(GTOS(1), GTOS(0), GTOS(-1), GTOS(0), BLACK);
DrawCircleV(GTOSV(a), 10, BLUE);
DrawCircleV(GTOSV(b), 10, RED);
// moving the point a
if(IN_RANGEV(GetScreenToWorld2D(GetMousePosition(), camera), GTOSV(a), 10)
|| a_moving) {
DrawCircleLines(GTOS(a.x), GTOS(a.y), 13, BLUE);
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !b_moving) {
a = STOGV(GetScreenToWorld2D(GetMousePosition(), camera));
a_moving = true;
} else a_moving = false;
}
// moving the point b
if(IN_RANGEV(GetScreenToWorld2D(GetMousePosition(), camera), GTOSV(b), 10)
|| b_moving) {
DrawCircleLines(GTOS(b.x), GTOS(b.y), 13, RED);
if(IsMouseButtonDown(MOUSE_BUTTON_LEFT) && !a_moving) {
b = STOGV(GetScreenToWorld2D(GetMousePosition(), camera));
b_moving = true;
} else b_moving = false;
}
calc_points((Vector2){0, -1}, a, b);
EndMode2D();
DrawFPS(10, 10);
DrawText(text, 100, 10, 30, DARKGREEN);
EndDrawing();
}
CloseWindow();
return 0;
}
|