×

Data Structure Using C & C++

Divide a circular region into two parts with minimal difference of angle

Here is an implementation to divide a circular region into two parts with minimal difference of angle in C:

C program to divide a circular region into two parts with minimal difference of angle

#include <math.h>
#include <stdio.h>

#define PI 3.14159265358979323846

// Function to calculate minimal angle difference
double calMinimalAngleDifference(double angles[], int n) {
  // Sort the angles array
  for (int i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
      if (angles[i] > angles[j]) {
        double temp = angles[i];
        angles[i] = angles[j];
        angles[j] = temp;
      }
    }
  }

  double minDifference = 360.0;
  for (int i = 0; i < n; i++) {
    double difference = angles[(i + 1) % n] - angles[i];
    if (difference < 0) {
      difference += 360.0;
    }
    if (difference < minDifference) {
      minDifference = difference;
    }
  }

  return minDifference;
}

int main() {
  double angles[] = {60.0, 90.0, 120.0, 180.0, 240.0, 300.0};
  int n = sizeof(angles) / sizeof(angles[0]);

  double minDifference = calMinimalAngleDifference(angles, n);

  printf(
      "Minimal difference of angles to divide the circular region into two "
      "parts is: %.2f degrees\n",
      minDifference);

  return 0;
}

Output

Minimal difference of angles to divide the circular region into two parts is: 30.00 degrees

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.