Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sweep Line Algorithm for Convex Hull in C #1602

Merged
merged 1 commit into from
Nov 4, 2024

Conversation

Bhupendrakumar20
Copy link
Contributor

@Bhupendrakumar20 Bhupendrakumar20 commented Nov 3, 2024

issue resolved :#1562

Pull Request: Sweep Line Algorithm for Convex Hull in C

Description
This PR introduces a C implementation of the Sweep Line Algorithm to compute the convex hull for a set of 2D points. The convex hull is a minimal convex boundary that can enclose all given points. This sweep line approach enables efficient computation by processing points in sorted order and maintaining the hull dynamically, making it well-suited for large sets of points.

Features Implemented

  1. Sorting Points: Points are sorted by x-coordinate, enabling efficient left-to-right processing.
  2. Convex Hull Construction:
    • The algorithm uses the sweep line method to iteratively build both the lower and upper hulls.
    • Cross Product: This function helps determine the orientation of three points, essential for ensuring the hull's convexity.
  3. Memory-Efficient Design: This implementation uses a linked list for dynamically managing points, reducing memory constraints compared to array-based approaches.

Files Added

  • sweep_line_convex_hull.c: Contains the main C code implementing the sweep line approach for convex hull.
  • README.md: Documentation covering the algorithm, usage, and example scenarios.

Code Overview

  • Data Structures:
    • Point struct to store x and y coordinates.
    • Linked list used for handling dynamically increasing points on the hull.
  • Key Functions:
    • compare: Helper function to sort points by x and y coordinates.
    • cross_product: Calculates the cross product of three points to determine left or right turns.
    • sweep_line_convex_hull: Computes the convex hull using the sweep line method.
  • Main Function:
    • Initializes a set of points and calls sweep_line_convex_hull to calculate and print the vertices of the convex hull.

Example

Consider the input points:

{ (0, 3), (2, 2), (1, 1), (2, 1), (3, 0), (0, 0), (3, 3) }

Expected output (convex hull vertices in counter-clockwise order):

(0, 0)
(3, 0)
(3, 3)
(0, 3)

Testing

The code has been tested with:

  • Basic configurations to verify output correctness.
  • Edge cases such as collinear points, minimal input (2-3 points), and duplicate points.
  • Larger datasets to ensure efficiency and correctness.

Checklist

  • Implemented Sweep Line Algorithm for convex hull.
  • Verified against known test cases for accuracy.
  • Included comprehensive documentation in README.md.
  • Code is formatted and commented for readability.

Additional Notes
The Sweep Line Algorithm provides a foundation for computational geometry tasks that require efficient hull calculations. This PR supports integer coordinates; future extensions could add floating-point support or graphical visualizations.


@Bhupendrakumar20
Copy link
Contributor Author

@pankaj-bind kindly review the issue and possibly merge it

@pankaj-bind pankaj-bind merged commit fd048f0 into AlgoGenesis:main Nov 4, 2024
2 of 4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants