Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <math.h>
- #include <stdio.h>
- #define N 5
- typedef struct {
- double x;
- double y;
- double radius;
- } Circumference;
- double compute_distance(Circumference c1, Circumference c2);
- double compute_longest_distance(Circumference circumferences[], size_t length);
- int main(void) {
- Circumference circumferences[N];
- printf("Insert %d circumferences [x y radius]: ", N);
- for (size_t i = 0; i < N; i++) {
- scanf("%lf %lf %lf", &circumferences[i].x, &circumferences[i].y,
- &circumferences[i].radius);
- }
- printf("Longest distance: %.4lf", compute_longest_distance(circumferences, N));
- return 0;
- }
- double compute_distance(Circumference c1, Circumference c2) {
- // (x2 - x1) ^ 2 + (y2 - y1) ^ 2
- double sq_distance =
- (c1.x - c2.x) * (c1.x - c2.x) + (c2.y - c1.y) * (c2.y - c1.y);
- double abs_distance = sq_distance >= 0 ? sq_distance : -sq_distance;
- // sqrt( (x2 - x1) ^ 2 + (y2 - y1) ^ 2 )
- // Substract (c1.radius + c2.radius) for distance between borders
- return sqrt(abs_distance);
- }
- double compute_longest_distance(Circumference circumferences[], size_t length) {
- if (length <= 0) return -1.0;
- if (length == 1) return 0.0;
- double longest_distance = 0.0;
- for (size_t i = 0; i < length; i++) {
- for (size_t j = i + 1; j < length; j++) {
- // Computing distance between circumferences[i] and circumferences[j]
- double current_distance =
- compute_distance(circumferences[i], circumferences[j]);
- // Longer then the previous best
- if (current_distance > longest_distance) {
- longest_distance = current_distance;
- }
- }
- }
- return longest_distance;
- }
Add Comment
Please, Sign In to add comment