-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathExample_-_Bresenham_oval_array.c
132 lines (107 loc) · 3.72 KB
/
Example_-_Bresenham_oval_array.c
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
#include "raylib.h"
// width/height/centerx/centery
static void midptellipse(int rx, int ry, int xc, int yc);
static int map[200][200]={0};
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib example.");
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
// width/height/centerx/centery
midptellipse(10,7,30,30);
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
midptellipse(GetRandomValue(5,10),GetRandomValue(5,10),GetRandomValue(5,100),GetRandomValue(5,100));
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
for(int y=0;y<100;y++){
for(int x=0;x<100;x++){
if(map[x][y]==1)DrawRectangle(x*3,y*3,3,3,BLACK);
}
}
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
void midptellipse(int rx, int ry,
int xc, int yc)
{
float dx, dy, d1, d2, x, y;
x = 0;
y = ry;
// Initial decision parameter of region 1
d1 = (ry * ry) - (rx * rx * ry) +
(0.25 * rx * rx);
dx = 2 * ry * ry * x;
dy = 2 * rx * rx * y;
// For region 1
while (dx < dy)
{
// Print points based on 4-way symmetry
int tx[4] = {x+xc,-x+xc,x+xc,-x+xc};
int ty[4] = {y+yc,y+yc,-y+yc,-y+yc};
for(int i=0;i<4;i++){
if(tx[i]>=0 && tx[i]<100 && ty[i]>=0 && ty[i]<100)map[tx[i]][ty[i]] = 1;
}
// Checking and updating value of
// decision parameter based on algorithm
if (d1 < 0)
{
x++;
dx = dx + (2 * ry * ry);
d1 = d1 + dx + (ry * ry);
}
else
{
x++;
y--;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d1 = d1 + dx - dy + (ry * ry);
}
}
// Decision parameter of region 2
d2 = ((ry * ry) * ((x + 0.5) * (x + 0.5))) +
((rx * rx) * ((y - 1) * (y - 1))) -
(rx * rx * ry * ry);
// Plotting points of region 2
while (y >= 0)
{
int tx[4] = {x+xc,-x+xc,x+xc,-x+xc};
int ty[4] = {y+yc,y+yc,-y+yc,-y+yc};
for(int i=0;i<4;i++){
if(tx[i]>=0 && tx[i]<100 && ty[i]>=0 && ty[i]<100)map[tx[i]][ty[i]] = 1;
}
// Checking and updating parameter
// value based on algorithm
if (d2 > 0)
{
y--;
dy = dy - (2 * rx * rx);
d2 = d2 + (rx * rx) - dy;
}
else
{
y--;
x++;
dx = dx + (2 * ry * ry);
dy = dy - (2 * rx * rx);
d2 = d2 + dx - dy + (rx * rx);
}
}
}