Finalize and merge hw-01
This commit is contained in:
commit
9adde41415
98
homework-01-two-circles/README.md
Normal file
98
homework-01-two-circles/README.md
Normal file
@ -0,0 +1,98 @@
|
||||
Úkolem je napsat program, který rozhodne vzájemnou polohu 2 kružnic.
|
||||
|
||||
Vstupem programu je zadání dvou kružnic. Každá kružnice je zadaná svým středem (x a y souřadnice) a poloměrem. Čísla jsou zadaná v pořadí x y r, jedná se o desetinná čísla.
|
||||
|
||||
Výstupem programu je rozhodnutí o vzájemné poloze kružnic. Celkem může nastat 6 variant vzájemné polohy:
|
||||
|
||||
- kružnice splývají,
|
||||
- jedna kružnice leží zcela uvnitř druhé,
|
||||
- jedna kružnice se zevnitř dotýká druhé,
|
||||
- kružnice se protínají,
|
||||
- kružnice se dotýkají zvenku,
|
||||
- kružnice leží zcela mimo sebe.
|
||||
|
||||
Program rozhodne o poloze a vypíše ji. Dále, pokud je to relevantní, program vypíše plochu překryvu kružnic. Formát výstupu pro všech 6 variant je uveden v ukázkovém běhu níže. Pozor, za výstupem je odřádkování (\n).
|
||||
|
||||
Program musí být schopen detekovat nesprávný vstup. Pokud je na vstupu nesmyslné zadání, program to zjistí, vypíše chybové hlášení a ukončí se. Formát chybového hlášení je uveden v ukázce níže. Pozor, za případným chybovým hlášením je odřádkování (\n). Chybové hlášení zasílejte na standardní výstup (printf), nezasílejte jej na chybový výstup. Za chybu je považováno:
|
||||
|
||||
- nečíselná hodnota nějaké souřadnice,
|
||||
- nečíselná hodnota poloměru,
|
||||
- poloměr nulový nebo záporný.
|
||||
|
||||
**Ukázka práce programu:**
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 5
|
||||
Zadejte parametry kruznice #2:
|
||||
0 10 5
|
||||
Vnejsi dotyk, zadny prekryv.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 3
|
||||
Zadejte parametry kruznice #2:
|
||||
1.5 1.5 0.8
|
||||
Kruznice #2 lezi uvnitr kruznice #1, prekryv: 2.010619
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 4
|
||||
Zadejte parametry kruznice #2:
|
||||
2 0 2
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 12.566371
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 5
|
||||
Zadejte parametry kruznice #2:
|
||||
100 100 10
|
||||
Kruznice lezi vne sebe, zadny prekryv.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 4.5
|
||||
Zadejte parametry kruznice #2:
|
||||
10 10 15
|
||||
Kruznice se protinaji, prekryv: 37.475800
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
-10.25 -20.5 4
|
||||
Zadejte parametry kruznice #2:
|
||||
-10.25 -20.5 4
|
||||
Kruznice splyvaji, prekryv: 50.265482
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 0 -2
|
||||
Nespravny vstup.
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
```
|
||||
Zadejte parametry kruznice #1:
|
||||
0 12 5
|
||||
Zadejte parametry kruznice #2:
|
||||
3 7 abc
|
||||
Nespravny vstup.
|
||||
```
|
138
homework-01-two-circles/main.c
Normal file
138
homework-01-two-circles/main.c
Normal file
@ -0,0 +1,138 @@
|
||||
#include <bits/pthreadtypes.h>
|
||||
#include <float.h>
|
||||
#include <math.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef __PROGTEST__
|
||||
#define debug(...) ((void)0)
|
||||
#else
|
||||
#define debug(fmt, ...) \
|
||||
fprintf(stdout, "[%s:%d %s()] " fmt "\n", __FILE__, __LINE__, __func__, \
|
||||
##__VA_ARGS__)
|
||||
#endif
|
||||
|
||||
double clamp(double d) {
|
||||
if (d > 1.0) {
|
||||
return 1.0;
|
||||
} else if (d < -1.0) {
|
||||
return -1.0;
|
||||
} else {
|
||||
return d;
|
||||
}
|
||||
}
|
||||
|
||||
// https://www.geeksforgeeks.org/dsa/area-of-intersection-of-two-circles/
|
||||
double intersection_area(double a_r, double b_r, double distance) {
|
||||
double a_square = a_r * a_r;
|
||||
double b_square = b_r * b_r;
|
||||
double distance_square = distance * distance;
|
||||
|
||||
double alpha_acos =
|
||||
(distance_square + a_square - b_square) / (2 * a_r * distance);
|
||||
double beta_acos =
|
||||
(distance_square + b_square - a_square) / (2 * b_r * distance);
|
||||
|
||||
double alpha = acos(clamp(alpha_acos)) * 2;
|
||||
double beta = acos(clamp(beta_acos)) * 2;
|
||||
|
||||
double a1 = 0.5 * beta * b_square - 0.5 * b_square * sin(beta);
|
||||
double a2 = 0.5 * alpha * a_square - 0.5 * a_square * sin(alpha);
|
||||
|
||||
double area = a1 + a2;
|
||||
|
||||
debug("Area = %lf", area);
|
||||
|
||||
return area;
|
||||
}
|
||||
|
||||
double circle_area(double radius) { return M_PI * radius * radius; }
|
||||
|
||||
int main() {
|
||||
double a_center_x = 0, a_center_y = 0, a_radius = 0, b_center_x = 0,
|
||||
b_center_y = 0, b_radius = 0;
|
||||
|
||||
printf("Zadejte parametry kruznice #1:\n");
|
||||
|
||||
int inputs_read = scanf("%lf %lf %lf", &a_center_x, &a_center_y, &a_radius);
|
||||
if (inputs_read != 3 || a_radius <= 0) {
|
||||
printf("Nespravny vstup.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
printf("Zadejte parametry kruznice #2:\n");
|
||||
|
||||
inputs_read = scanf("%lf %lf %lf", &b_center_x, &b_center_y, &b_radius);
|
||||
if (inputs_read != 3 || b_radius <= 0) {
|
||||
printf("Nespravny vstup.\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
double center_distance =
|
||||
sqrt((b_center_x - a_center_x) * (b_center_x - a_center_x) +
|
||||
(b_center_y - a_center_y) * (b_center_y - a_center_y));
|
||||
|
||||
debug("Distance of centers = %0.60lf", center_distance);
|
||||
debug("Radii difference = %0.60lf", a_radius - b_radius);
|
||||
debug("Radii difference = %0.60lf", b_radius - a_radius);
|
||||
debug("Center + #1 = %0.60lf", a_center_x + a_radius);
|
||||
debug("Center + #2 = %0.60lf", b_center_x + b_radius);
|
||||
|
||||
double radii_sum = a_radius + b_radius;
|
||||
double radii_difference = fabs(a_radius - b_radius);
|
||||
double epsilon = 100 * DBL_EPSILON * fmax(a_radius, b_radius);
|
||||
|
||||
// Both origin points and radii are identical
|
||||
bool are_identical = a_center_x == b_center_x && a_center_y == b_center_y && a_radius == b_radius;
|
||||
|
||||
// d = r1 + r2
|
||||
// d - r1 - r1 < epsilon
|
||||
bool are_touching_from_outside = fabs(center_distance - radii_sum) < epsilon;
|
||||
|
||||
// d = |r1 - r2|
|
||||
// d - |r1 - r2| < epsilon
|
||||
bool are_touching_from_inside = fabs(center_distance - radii_difference) < epsilon;
|
||||
|
||||
// d < |r1 - r2|
|
||||
bool is_fully_inside = center_distance < radii_difference;
|
||||
|
||||
// d < r1 + r2
|
||||
bool are_overlapping = center_distance < radii_sum;
|
||||
|
||||
if (are_identical) {
|
||||
printf("Kruznice splyvaji, prekryv: %lf\n", circle_area(a_radius));
|
||||
}
|
||||
else if (are_touching_from_inside) {
|
||||
if (a_radius > b_radius) {
|
||||
printf(
|
||||
"Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: %lf\n",
|
||||
circle_area(b_radius));
|
||||
} else {
|
||||
printf(
|
||||
"Vnitrni dotyk, kruznice #1 lezi uvnitr kruznice #2, prekryv: %lf\n",
|
||||
circle_area(a_radius));
|
||||
}
|
||||
}
|
||||
else if (are_touching_from_outside) {
|
||||
printf("Vnejsi dotyk, zadny prekryv.\n");
|
||||
}
|
||||
else if (is_fully_inside) {
|
||||
if (a_radius > b_radius) {
|
||||
printf("Kruznice #2 lezi uvnitr kruznice #1, prekryv: %lf\n",
|
||||
circle_area(b_radius));
|
||||
} else {
|
||||
printf("Kruznice #1 lezi uvnitr kruznice #2, prekryv: %lf\n",
|
||||
circle_area(a_radius));
|
||||
}
|
||||
}
|
||||
else if (are_overlapping) {
|
||||
printf("Kruznice se protinaji, prekryv: %lf\n",
|
||||
intersection_area(a_radius, b_radius, center_distance));
|
||||
}
|
||||
else {
|
||||
printf("Kruznice lezi vne sebe, zadny prekryv.\n");
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
2
homework-01-two-circles/test_data/0000_in.txt
Normal file
2
homework-01-two-circles/test_data/0000_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 5
|
||||
0 10 5
|
3
homework-01-two-circles/test_data/0000_out.txt
Normal file
3
homework-01-two-circles/test_data/0000_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnejsi dotyk, zadny prekryv.
|
3
homework-01-two-circles/test_data/0000_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0000_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnejsi dotyk, zadny prekryv.
|
2
homework-01-two-circles/test_data/0001_in.txt
Normal file
2
homework-01-two-circles/test_data/0001_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 3
|
||||
1.5 1.5 0.8
|
3
homework-01-two-circles/test_data/0001_out.txt
Normal file
3
homework-01-two-circles/test_data/0001_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice #2 lezi uvnitr kruznice #1, prekryv: 2.010619
|
3
homework-01-two-circles/test_data/0001_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0001_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice #2 lezi uvnitr kruznice #1, prekryv: 2.010619
|
2
homework-01-two-circles/test_data/0002_in.txt
Normal file
2
homework-01-two-circles/test_data/0002_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 4
|
||||
2 0 2
|
3
homework-01-two-circles/test_data/0002_out.txt
Normal file
3
homework-01-two-circles/test_data/0002_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 12.566371
|
3
homework-01-two-circles/test_data/0002_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0002_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 12.566371
|
2
homework-01-two-circles/test_data/0003_in.txt
Normal file
2
homework-01-two-circles/test_data/0003_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 5
|
||||
100 100 10
|
3
homework-01-two-circles/test_data/0003_out.txt
Normal file
3
homework-01-two-circles/test_data/0003_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice lezi vne sebe, zadny prekryv.
|
3
homework-01-two-circles/test_data/0003_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0003_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice lezi vne sebe, zadny prekryv.
|
2
homework-01-two-circles/test_data/0004_in.txt
Normal file
2
homework-01-two-circles/test_data/0004_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 0 4.5
|
||||
10 10 15
|
3
homework-01-two-circles/test_data/0004_out.txt
Normal file
3
homework-01-two-circles/test_data/0004_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice se protinaji, prekryv: 37.475800
|
3
homework-01-two-circles/test_data/0004_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0004_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice se protinaji, prekryv: 37.475800
|
2
homework-01-two-circles/test_data/0005_in.txt
Normal file
2
homework-01-two-circles/test_data/0005_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
-10.25 -20.5 4
|
||||
-10.25 -20.5 4
|
3
homework-01-two-circles/test_data/0005_out.txt
Normal file
3
homework-01-two-circles/test_data/0005_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice splyvaji, prekryv: 50.265482
|
3
homework-01-two-circles/test_data/0005_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0005_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Kruznice splyvaji, prekryv: 50.265482
|
1
homework-01-two-circles/test_data/0006_in.txt
Normal file
1
homework-01-two-circles/test_data/0006_in.txt
Normal file
@ -0,0 +1 @@
|
||||
0 0 -2
|
2
homework-01-two-circles/test_data/0006_out.txt
Normal file
2
homework-01-two-circles/test_data/0006_out.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Nespravny vstup.
|
2
homework-01-two-circles/test_data/0006_out_win.txt
Normal file
2
homework-01-two-circles/test_data/0006_out_win.txt
Normal file
@ -0,0 +1,2 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Nespravny vstup.
|
2
homework-01-two-circles/test_data/0007_in.txt
Normal file
2
homework-01-two-circles/test_data/0007_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
0 12 5
|
||||
3 7 abc
|
3
homework-01-two-circles/test_data/0007_out.txt
Normal file
3
homework-01-two-circles/test_data/0007_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Nespravny vstup.
|
3
homework-01-two-circles/test_data/0007_out_win.txt
Normal file
3
homework-01-two-circles/test_data/0007_out_win.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Nespravny vstup.
|
2
homework-01-two-circles/test_data/0008_in.txt
Normal file
2
homework-01-two-circles/test_data/0008_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
9.7e-39 -1.85e-38 4.16e-38
|
||||
4.03e-38 2.3e-39 4.6e-39
|
3
homework-01-two-circles/test_data/0008_out.txt
Normal file
3
homework-01-two-circles/test_data/0008_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 0.000000
|
2
homework-01-two-circles/test_data/0009_in.txt
Normal file
2
homework-01-two-circles/test_data/0009_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
4.03e-38 2.3e-39 4.6e-39
|
||||
9.7e-39 -1.85e-38 4.16e-38
|
3
homework-01-two-circles/test_data/0009_out.txt
Normal file
3
homework-01-two-circles/test_data/0009_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #1 lezi uvnitr kruznice #2, prekryv: 0.000000
|
2
homework-01-two-circles/test_data/0010_in.txt
Normal file
2
homework-01-two-circles/test_data/0010_in.txt
Normal file
@ -0,0 +1,2 @@
|
||||
-2.21e-38 -1.11e-38 1.4862e-36
|
||||
1.2253e-36 5.721e-37 1.092e-37
|
3
homework-01-two-circles/test_data/0010_out.txt
Normal file
3
homework-01-two-circles/test_data/0010_out.txt
Normal file
@ -0,0 +1,3 @@
|
||||
Zadejte parametry kruznice #1:
|
||||
Zadejte parametry kruznice #2:
|
||||
Vnitrni dotyk, kruznice #2 lezi uvnitr kruznice #1, prekryv: 0.000000
|
Loading…
x
Reference in New Issue
Block a user