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 | diff --git a/README.md b/README.md
new file mode 100644
index 0000000..bb84ec7
--- /dev/null
+++ b/README.md
@@ -0,0 +1,2 @@
+# randoms in C
+
diff --git a/coordinate-in-c/Makefile b/coordinate-in-c/Makefile
new file mode 100644
index 0000000..9d4c59e
--- /dev/null
+++ b/coordinate-in-c/Makefile
@@ -0,0 +1,7 @@
+PKG = $(shell pkg-config --cflags --libs )
+
+install:
+ cc -Wall -Wextra $(PKG) -lm -o main main.c
+
+run:
+ ./main
diff --git a/coordinate-in-c/main.c b/coordinate-in-c/main.c
new file mode 100644
index 0000000..b5cb337
--- /dev/null
+++ b/coordinate-in-c/main.c
@@ -0,0 +1,126 @@
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <time.h>
+
+
+#define WIDTH 800
+#define HEIGHT 800
+#define PADDING 40
+#define STEPS 20
+#define PREFIX "output.ppm" // you should use .ppm extension
+
+#define RED 0xFB4934
+#define GREEN 0x00FF00
+#define BLUE 0x0000FF
+
+#define BG_COLOR 0x202020
+#define FG_COLOR 0x928374
+
+#define COLOR 0x2C074F
+
+
+
+uint32_t image[HEIGHT][WIDTH];
+
+typedef uint32_t Color ;
+
+
+void cxy(int *x,int *y)
+{
+ *x = WIDTH /2;
+ *y = HEIGHT /2;
+}
+
+
+void draw_px(int x,int y,Color color)
+{
+ if (x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) return;
+ image[y][x] = color;
+}
+
+void draw_pixels(Color bg)
+{
+ for(int y = 0;y < HEIGHT;++y){
+ for(int x=0;x < WIDTH;++x){
+ draw_px(x, y, bg);
+ }
+ }
+
+}
+
+void draw_circle(int ix, int iy, int radius, Color color)
+{
+ for(int y = -radius;y<=radius;++y){
+ for(int x = -radius;x<=radius;++x){
+ int a = x*x + y*y ;
+ if (a>=radius*radius - radius && a<=radius*radius +radius){
+ draw_px(ix+x,iy+y,color);
+ }
+ }
+
+ }
+
+}
+
+void draw_circlef(int ix, int iy, int radius, Color color)
+{
+ for (int i =0;i<=radius;i=i+3)
+ draw_circle(ix,iy,i,color+i*color);
+
+}
+
+void draw_point(int x,int y)
+{
+ int cx,cy;
+ cxy(&cx,&cy);
+
+ draw_circlef(cx + x*STEPS,(cy -y*STEPS),1,FG_COLOR);
+
+}
+
+
+
+void draw_all()
+{
+ FILE *f= fopen(PREFIX, "wb");
+
+ if (f == NULL){
+ fprintf(stderr,"ERROR: Could not open file!!\n");
+ }
+ fprintf(f,"P6\n%d\n%d\n255\n",WIDTH,HEIGHT);
+
+
+ for(int y = 0;y < HEIGHT;++y){
+ for(int x=0;x < WIDTH;++x){
+
+ Color color = image[y][x];
+ uint8_t bytes[3] = {
+ (color & 0xFF0000) >> 8*2,
+ (color & 0x00FF00) >> 8*1,
+ (color & 0x0000FF) >> 8*0,
+ };
+
+ fwrite(bytes,sizeof(bytes), 1,f);
+ }
+ };
+ fclose(f);
+}
+
+
+
+int main(void) {
+ draw_pixels(BG_COLOR);
+
+ int x,y;
+ cxy(&x,&y);
+
+ // potato
+
+ for (int i =0;i<=5;++i){
+ draw_circlef(rand() % (WIDTH +i),rand() % (HEIGHT +i),x/1.5,RED * rand());
+ }
+
+ draw_all();
+ return 0;
+}
|