Tuesday 30 October 2018

Hacktoberfest Third Pull Request

This week I contributed to consumable algorithms library by creating the most complex and biggest pull request so far - Convex Hull function.

Convex Hull function


  • receives array of points with (x,y) coordinates
  • returns smallest sorted array of points that belong to smallest convex hull of received points
  • sorted array means that array[i] and array[i+1] are adjacent vertices of a hull (array[0] and array[array.length-1] are also adjacent
Here is an example of how this function should work:
Points connected by lines are points that are returned by function. As I already mentioned, returned array is sorted, so hull can be easily drawn (by drawing line segments connecting adjacent elements of returned array).

Here are more examples of how convexHull() works:
This is wrong:


This is correct:

Specific cases:

  • If function receives less than 3 points, it returns all points
  • If many points are on one line, only extreme points are returned

Implementation


To solve this problem I had to do a lot of Math and to write 8 functions. You can see full code with JSDocs comments  here. I also added tests for my code. If you want to use this function or any other function from algorithms library, check instructions here in README.md.

No comments:

Post a Comment