initial commit
This commit is contained in:
commit
5421b2e6ce
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
main
|
||||||
|
image.ppm
|
7
Makefile
Normal file
7
Makefile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
.PHONY := run
|
||||||
|
|
||||||
|
run: main
|
||||||
|
./main
|
||||||
|
|
||||||
|
main: main.c Makefile
|
||||||
|
gcc -o main main.c -O3 -g
|
5
README.md
Normal file
5
README.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# The Four Programming Questions from Computer Enhance
|
||||||
|
|
||||||
|
My solutions for the questions from this post: https://www.computerenhance.com/p/the-four-programming-questions-from
|
||||||
|
|
||||||
|
These solution might not be the best, but they are mine ant I like them.
|
105
main.c
Normal file
105
main.c
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
#include <math.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
// Question 1:
|
||||||
|
void copy_rect(
|
||||||
|
char *buffer_a, int pitch_a,
|
||||||
|
char *buffer_b, int pitch_b,
|
||||||
|
int from_min_x, int from_min_y,
|
||||||
|
int from_max_x, int from_max_y,
|
||||||
|
int to_min_x, int to_min_y
|
||||||
|
) {
|
||||||
|
|
||||||
|
for (int y = 0; y < (from_max_y - from_min_y); y++) {
|
||||||
|
for (int x = 0; x < (from_max_x - from_min_x); x++) {
|
||||||
|
int from_idx = (y + from_min_y) * pitch_a + (x + from_min_x);
|
||||||
|
int to_idx = (y + to_min_y ) * pitch_b + (x + to_min_x );
|
||||||
|
|
||||||
|
buffer_b[to_idx] = buffer_a[from_idx];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Question 2:
|
||||||
|
void copy_string(char *from, char *to) {
|
||||||
|
int i = 0;
|
||||||
|
while (from[i] != 0) {
|
||||||
|
to[i] = from[i];
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
to[i] = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Question 3:
|
||||||
|
bool contains_color(char unsigned pixel, char unsigned color) {
|
||||||
|
char mask = (color << 6) | (color << 4) | (color << 2) | color;
|
||||||
|
char masked = ~(pixel ^ mask);
|
||||||
|
char masked2 = masked & (masked << 1);
|
||||||
|
return (masked2 & 0b10101010) != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Question 4:
|
||||||
|
struct Image {
|
||||||
|
bool *data;
|
||||||
|
int width, height;
|
||||||
|
};
|
||||||
|
|
||||||
|
void write_image_ppm(char *filename, struct Image *image) {
|
||||||
|
FILE* f = fopen(filename, "w");
|
||||||
|
char header[64];
|
||||||
|
int header_size = snprintf(header, sizeof(header), "P1\n%d %d\n", image->width, image->height);
|
||||||
|
fwrite(header, sizeof(char), header_size, f);
|
||||||
|
for (int i = 0; i < image->width * image->height; i++) {
|
||||||
|
char *symbol = image->data[i] ? "1" : "0";
|
||||||
|
fwrite(symbol, sizeof(char), 1, f);
|
||||||
|
}
|
||||||
|
fclose(f);
|
||||||
|
}
|
||||||
|
|
||||||
|
void plot(struct Image *image, int x, int y) {
|
||||||
|
if ((0 <= x && x < image->width) && (0 <= y && y < image->height)) {
|
||||||
|
int idx = y * image->width + x;
|
||||||
|
image->data[idx] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void outline_circle(struct Image *image, int cx, int cy, int radius) {
|
||||||
|
int mid_point = ceil(radius/sqrt(2));
|
||||||
|
|
||||||
|
int oy = -radius;
|
||||||
|
for (int ox = 0; ox < mid_point; ox++) {
|
||||||
|
if (ox*ox + oy*oy >= radius*radius) {
|
||||||
|
oy++;
|
||||||
|
}
|
||||||
|
|
||||||
|
plot(image, cx+ox, cy+oy);
|
||||||
|
plot(image, cx-ox, cy+oy);
|
||||||
|
plot(image, cx+ox, cy-oy);
|
||||||
|
plot(image, cx-ox, cy-oy);
|
||||||
|
plot(image, cx+oy, cy+ox);
|
||||||
|
plot(image, cx-oy, cy+ox);
|
||||||
|
plot(image, cx+oy, cy-ox);
|
||||||
|
plot(image, cx-oy, cy-ox);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
int image_width = 100;
|
||||||
|
int image_height = 100;
|
||||||
|
bool image_data[image_width * image_height];
|
||||||
|
memset(image_data, false, sizeof(bool) * image_width * image_height);
|
||||||
|
struct Image image = {
|
||||||
|
.data = image_data,
|
||||||
|
.width = image_width,
|
||||||
|
.height = image_height
|
||||||
|
};
|
||||||
|
|
||||||
|
for (int i = 8; i < 50; i+=10) {
|
||||||
|
outline_circle(&image, 50, 50, i);
|
||||||
|
}
|
||||||
|
write_image_ppm("image.ppm", &image);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user