Did I find the right examples for you? yes no

All Samples(13)  |  Call(11)  |  Derive(0)  |  Import(2)
Returns a Polygon representing the convex hull of a set of 2D points.

Notes:
======
    This can only be performed on a set of non-symbolic points.

Example:
========
    >>> from sympy.geometry import Point, convex_hull
    >>> points = [ Point(x) for x in [(1,1), (1,2), (3,1), (-5,2), (15,4)] ](more...)

        def convex_hull(*args):
    """
    Returns a Polygon representing the convex hull of a set of 2D points.

    Notes:
    ======
        This can only be performed on a set of non-symbolic points.

    Example:
    ========
        >>> from sympy.geometry import Point, convex_hull
        >>> points = [ Point(x) for x in [(1,1), (1,2), (3,1), (-5,2), (15,4)] ]
        >>> convex_hull(points)
        Polygon(Point(-5, 2), Point(1, 1), Point(3, 1), Point(15, 4))

    Description of method used:
    ===========================
        See http://en.wikipedia.org/wiki/Graham_scan.
    """
    from point import Point
    from line import Segment
    from polygon import Polygon

    def uniquify(a):
        ret = {}
        # not order preserving
        map(ret.__setitem__, a, [])
        return ret.keys()

    p = args[0]
    if isinstance(p, Point):
        p = uniquify(args)

    if len(p) is 1:
        return p[0]
    else:
        if len(p) is 2:
            return Segment(p[0],p[1])



    def orientation(p,q,r):
        '''Return positive if p-q-r are clockwise, neg if ccw, zero if colinear.'''
        return (q[1]-p[1])*(r[0]-p[0]) - (q[0]-p[0])*(r[1]-p[1])


    '''scan to find upper and lower convex hulls of a set of 2d points.'''
    U = []
    L = []
    #print "[graham] points:",Points
    p.sort()
    for p_i in p:
        while len(U) > 1 and orientation(U[-2],U[-1],p_i) <= 0: U.pop()
        while len(L) > 1 and orientation(L[-2],L[-1],p_i) >= 0: L.pop()
        U.append(p_i)
        L.append(p_i)        #print "[graham] result:", U,L
    U.reverse()
    convexHull = tuple(L+U[1:-1])

    #convexHull = uniquify(convexHull)
    #print "U:",U.reverse()
    #print "L:",L
    #print "ch(",len(convexHull),"):",convexHull
    if len(convexHull) is 2:
        return Segment(convexHull[0],convexHull[1])
    return Polygon(convexHull)
        


src/s/y/sympy-HEAD/sympy/geometry/tests/test_geometry.py   sympy(Download)
from sympy import (Abs, C, Dummy, Rational, Float, S, Symbol, cos, oo, pi,
                   simplify, sin, sqrt, symbols, tan)
from sympy.geometry import (Circle, Curve, Ellipse, GeometryError, Line, Point,
                            Polygon, Ray, RegularPolygon, Segment, Triangle,
                            are_similar, convex_hull, intersection, centroid)
    ch2 = Segment(another_p[0], another_p[1])
 
    assert convex_hull(*another_p) == ch2
    assert convex_hull(*p) == ch
    assert convex_hull(p[0]) == p[0]
    assert convex_hull(p[0], p[1]) == Segment(p[0], p[1])

src/s/y/sympy-polys-HEAD/sympy/geometry/tests/test_geometry.py   sympy-polys(Download)
from sympy import Symbol, Rational, sqrt, pi, cos, oo, simplify, Real, raises
from sympy.geometry import Point, Polygon, convex_hull, Segment, \
        RegularPolygon, Circle, Ellipse, GeometryError, Line, intersection, \
        Ray, Triangle, are_similar, Curve
 
    ch2 = Segment(another_p[0],another_p[1]);
 
    assert convex_hull(another_p) == ch2
    assert convex_hull(p) == ch
    assert convex_hull(p[0]) == p[0]
    assert convex_hull(p[0], p[1]) == Segment(p[0], p[1])