Created
September 20, 2018 05:42
-
-
Save ryukinix/2ea0019e71849bb7b9ae1b3a35048324 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* ================================================ | |
* | |
* Copyright 2018 Manoel Vilela | |
* | |
* Author: Manoel Vilela | |
* Contact: manoel_vilela@engineer.com | |
* Organization: UFC | |
* | |
* =============================================== | |
*/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <math.h> | |
#include <stdbool.h> | |
#define ANSI_COLOR_RED "\x1b[31m" | |
#define ANSI_COLOR_GREEN "\x1b[32m" | |
#define ANSI_COLOR_RESET "\x1b[0m" | |
#define N 21 | |
#define M 30 | |
typedef struct point { | |
float x; | |
float y; | |
} Point; | |
typedef struct circle { | |
float radius; | |
Point *center; | |
} Circle; | |
float square(float n) { | |
return pow(n, 2); | |
} | |
Point* new_point(int x, int y) { | |
Point *p = malloc(sizeof(Point)); | |
p->x = x; | |
p->y = y; | |
return p; | |
} | |
void free_point(Point *p) { | |
free(p); | |
} | |
Circle* new_circle(int n, int m) { | |
Circle *c = malloc(sizeof(Circle)); | |
c->radius = n / 2; | |
c->center = new_point(n/2, m/2); | |
return c; | |
} | |
void free_circle(Circle *c) { | |
free(c->center); | |
free(c); | |
} | |
float euclidian_distance(Point* p1, Point* p2) { | |
return sqrt(square(p1->x - p2->x) + square(p1->y - p2->y)); | |
} | |
int circle_is_inside(Circle* c, Point* p) { | |
float radius = c->radius; | |
float distance = euclidian_distance(c->center, p); | |
return (distance <= radius)? true: false; | |
} | |
void fill_circle_matrix(int matrix[N][M]) { | |
Circle* c = new_circle(N, M); | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < M; j++) { | |
Point *p = new_point(i, j); | |
matrix[i][j] = circle_is_inside(c, p); | |
free_point(p); | |
} | |
} | |
free_circle(c); | |
} | |
void print_matrix(int matrix[N][M]) { | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < M; j++) { | |
int value = matrix[i][j]; | |
if (value == 1) { | |
printf(ANSI_COLOR_GREEN "%d " ANSI_COLOR_RESET, value); | |
} else { | |
printf(ANSI_COLOR_RED "%d " ANSI_COLOR_RESET, value); | |
} | |
} | |
printf("\n"); | |
} | |
} | |
int main(void) { | |
int matrix[N][M]; | |
fill_circle_matrix(matrix); | |
print_matrix(matrix); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment