Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include "common.h"
- void get_coords(const char *, long double *, long double *);
- long double get_value(const char *);
- #define pi 3.1415926535897932384626433832795029L
- inline long double square (long double number) {
- return number * number;
- }
- int main (void) {
- long double x1, x2, y1, y2;
- get_coords("First circle: ", &x1, &y1);
- get_coords("Second circle: ", &x2, &y2);
- long double scale = get_value("Radius: ");
- long double distance = sqrtl(square(x2 - x1) + square(y2 - y1));
- distance /= scale;
- if (distance >= 2.0L) {
- printf("no overlap\n");
- return 0;
- }
- long double overlap = pi - distance * sqrtl(1.0L - 0.25L * square(distance)) - 2.0L * asinl(0.5L * distance);
- long double area = (2.0L * pi - overlap) * square(scale);
- printf("Area: %.10Lg\n", area);
- return 0;
- }
- void get_coords (const char * prompt, long double * x, long double * y) {
- char * line;
- char ** tokens;
- while (1) {
- printf("%s", prompt);
- line = get_line(NULL);
- tokens = tokenize_string(line, " ", 1);
- free(line);
- if (count_lines(tokens) != 2) {
- free(tokens);
- printf("Please enter two coordinates.\n");
- continue;
- }
- *x = strtold(*tokens, &line);
- if (*line) {
- free(tokens);
- printf("Please enter a valid value for the X coordinate.\n");
- continue;
- }
- *y = strtold(tokens[1], &line);
- if (*line) {
- free(tokens);
- printf("Please enter a valid value for the Y coordinate.\n");
- continue;
- }
- free(tokens);
- return;
- }
- }
- long double get_value (const char * prompt) {
- char * line;
- char * errp;
- long double result;
- while (1) {
- printf("%s", prompt);
- line = get_line(NULL);
- result = strtold(line, &errp);
- if (*errp || (result <= 0.0L)) {
- free(line);
- printf("Please enter a valid value.\n");
- continue;
- }
- free(line);
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment