Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

convex hull algorithm python

# Python3 program to find convex hull of a set of points. Refer
# https://www.geeksforgeeks.org/orientation-3-ordered-points/
# for explanation of orientation()
 
# point class with x, y as point
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
 
def Left_index(points):
     
    '''
    Finding the left most point
    '''
    minn = 0
    for i in range(1,len(points)):
        if points[i].x < points[minn].x:
            minn = i
        elif points[i].x == points[minn].x:
            if points[i].y > points[minn].y:
                minn = i
    return minn
 
def orientation(p, q, r):
    '''
    To find orientation of ordered triplet (p, q, r).
    The function returns following values
    0 --> p, q and r are collinear
    1 --> Clockwise
    2 --> Counterclockwise
    '''
    val = (q.y - p.y) * (r.x - q.x) - 
          (q.x - p.x) * (r.y - q.y)
 
    if val == 0:
        return 0
    elif val > 0:
        return 1
    else:
        return 2
 
def convexHull(points, n):
     
    # There must be at least 3 points
    if n < 3:
        return
 
    # Find the leftmost point
    l = Left_index(points)
 
    hull = []
     
    '''
    Start from leftmost point, keep moving counterclockwise
    until reach the start point again. This loop runs O(h)
    times where h is number of points in result or output.
    '''
    p = l
    q = 0
    while(True):
         
        # Add current point to result
        hull.append(p)
 
        '''
        Search for a point 'q' such that orientation(p, q,
        x) is counterclockwise for all points 'x'. The idea
        is to keep track of last visited most counterclock-
        wise point in q. If any point 'i' is more counterclock-
        wise than q, then update q.
        '''
        q = (p + 1) % n
 
        for i in range(n):
             
            # If i is more counterclockwise
            # than current q, then update q
            if(orientation(points[p],
                           points[i], points[q]) == 2):
                q = i
 
        '''
        Now q is the most counterclockwise with respect to p
        Set p as q for next iteration, so that q is added to
        result 'hull'
        '''
        p = q
 
        # While we don't come to first point
        if(p == l):
            break
 
    # Print Result
    for each in hull:
        print(points[each].x, points[each].y)
 
# Driver Code
points = []
points.append(Point(0, 3))
points.append(Point(2, 2))
points.append(Point(1, 1))
points.append(Point(2, 1))
points.append(Point(3, 0))
points.append(Point(0, 0))
points.append(Point(3, 3))
 
convexHull(points, len(points))
 
# This code is contributed by
# Akarsh Somani, IIIT Kalyani
Comment

convex hull python

from scipy.spatial import ConvexHull

# ... generate 'points', a 2D numpy array 

hull = ConvexHull(points)

# to plot the hull
vertices = hull.vertices
plt.plot(points[vertices,0], points[vertices,1], 'r', lw = 2)
Comment

PREVIOUS NEXT
Code Example
Python :: pyodbc sql save pandas dataframe 
Python :: tuple slicing in python 
Python :: uniform distribution python example 
Python :: Getting the Current Working Directory in Python 
Python :: python read from stdin 
Python :: how to check django rest framework version 
Python :: remove special characters from string python 
Python :: python find closest lower value in list 
Python :: python print version 
Python :: falsy values in python 
Python :: python import worldcloud 
Python :: numpy standard deviation 
Python :: python randomize a dataframe pandas 
Python :: python generate id 
Python :: sparse categorical cross entropy python 
Python :: how to convert a byte array to string in python 
Python :: how to delete a csv file in python 
Python :: Insert missing data in pandas 
Python :: sum of number digits python 
Python :: convert dict to dataframe 
Python :: ursina python 
Python :: fill null values with zero python 
Python :: pandas convert series of datetime to date 
Python :: 7zip python extract 
Python :: python datetime format 
Python :: runge kutta 
Python :: how to redirect in django rest framework 
Python :: python number and name of weekday 
Python :: how to play a video in tkinter window 
Python :: clahe opencv 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =