From 5421b2e6ce6d22b5ff61f81f7d5a4f55d2b1bcf8 Mon Sep 17 00:00:00 2001 From: Rokas Puzonas Date: Sat, 5 Aug 2023 14:57:43 +0300 Subject: [PATCH] initial commit --- .gitignore | 2 + Makefile | 7 ++++ README.md | 5 +++ main.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 119 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ac5b60 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main +image.ppm diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..3a6e2c2 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +.PHONY := run + +run: main + ./main + +main: main.c Makefile + gcc -o main main.c -O3 -g diff --git a/README.md b/README.md new file mode 100644 index 0000000..4f525cc --- /dev/null +++ b/README.md @@ -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. diff --git a/main.c b/main.c new file mode 100644 index 0000000..bb7ec2c --- /dev/null +++ b/main.c @@ -0,0 +1,105 @@ +#include +#include +#include +#include + +// 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; +}