Skip to content

Instantly share code, notes, and snippets.

@artyom-poptsov
Last active December 18, 2023 05:48
Show Gist options
  • Save artyom-poptsov/e97cd26805118c4427a7f40b10d827f1 to your computer and use it in GitHub Desktop.
Save artyom-poptsov/e97cd26805118c4427a7f40b10d827f1 to your computer and use it in GitHub Desktop.
sad.c
/*
Copyright (C) 2023 Artyom V. Poptsov <poptsov.artyom@gmail.com>
Mastodon: https://fosstodon.org/@avp
Home page: https://memory-heap.org
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notice and this notice are preserved. This file is offered as-is,
without any warranty.
*/
#include <stdio.h>
#include <stdlib.h>
struct point {
size_t x;
size_t y;
};
typedef struct point point_t;
struct dimension {
size_t h;
size_t w;
};
typedef struct dimension dimension_t;
void fill(char* arr, dimension_t size, char filling) {
for (int i = 0; i < size.h; i++) {
for (int j = 0; j < size.w; j++) {
*(arr + i * size.w + j) = filling;
}
}
}
void print(char* arr, dimension_t size) {
for (int i = 0; i < size.h; i++) {
for (int j = 0; j < size.w; j++) {
printf("%c ", *(arr + i * size.w + j));
}
putchar('\n');
}
}
void circle(char* arr, dimension_t size, point_t center, size_t r, char filling) {
for (int i = center.y - r; i <= center.y + r; i++) {
for (int j = center.x - r; j <= center.x + r; j++) {
if ( ((j - center.x) * (j - center.x))
+ ((i - center.y) * (i - center.y))
<= r * r ) {
if ((i >= 0) && (i < size.h) && (j >= 0) && (j < size.w)) {
*(arr + i * size.w + j) = filling;
}
}
}
}
}
void rectangle(char* arr,
dimension_t arr_size,
point_t rect_pos,
dimension_t rect_size,
char filling) {
for (int i = rect_pos.y; i < rect_pos.y + rect_size.h; i++) {
for (int j = rect_pos.x; j < rect_pos.x + rect_size.w; j++) {
if ((i >= 0) && (i < arr_size.h) && (j >= 0) && (j < arr_size.w)) {
*(arr + i * arr_size.w + j) = filling;
}
}
}
}
int main() {
enum { H = 50, W = 60 };
dimension_t arr_size = { .h = H, .w = W };
char arr[H][W] = { 0 };
fill(*arr, arr_size, '.');
/* Mouth. */
circle(
*arr,
arr_size,
(point_t) { .x = 50, .y = 25 },
10,
'o');
rectangle(
*arr,
arr_size,
(point_t) { .x = 50, .y = 0 },
(dimension_t) { .w = 20, .h = H },
'.');
circle(
*arr,
arr_size,
(point_t) { .x = 55, .y = 25 },
10,
'.');
/* Nose. */
rectangle(
*arr,
arr_size,
(point_t) { .x = 20, .y = H / 2 - 2 },
(dimension_t) { .w = 15, .h = 4 },
'o');
/* Eyes. */
circle(
*arr,
arr_size,
(point_t) { .x = 10, .y = 15 },
5,
'o');
circle(
*arr,
arr_size,
(point_t) { .x = 10, .y = 36.0 },
5,
'o');
print(*arr, (dimension_t) { .h = H, .w = W });
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment