예제 #1
0
        void clip_polygon_by_half_plane(
            const Polygon2d& P, 
            const vec2& q1,
            const vec2& q2,
            Polygon2d& result,
            bool invert
        ) {
            result.clear() ;

            if(P.size() == 0) {
                return ;
            }

            if(P.size() == 1) {
                if(point_is_in_half_plane(P[0], q1, q2, invert)) {
                    result.push_back(P[0]) ;
                }
                return ;
            }

            vec2 prev_p = P[P.size() - 1] ;
            Sign prev_status = point_is_in_half_plane(
                prev_p, q1, q2, invert
            ) ;

            for(unsigned int i=0; i<P.size(); i++) {
                vec2 p = P[i] ;
                Sign status = point_is_in_half_plane(
                    p, q1, q2, invert
                ) ;
                if(
                    status != prev_status &&
                    status != ZERO &&
                    prev_status != ZERO
                ) {
                    vec2 intersect ;
                    if(intersect_segments(prev_p, p, q1, q2, intersect)) {
                        result.push_back(intersect) ;
                    } else {
                    }
                }

                switch(status) {
                case NEGATIVE:
                    break ;
                case ZERO:
                    result.push_back(p) ;
                    break ;
                case POSITIVE:
                    result.push_back(p) ;
                    break ;
                }

                prev_p = p ;
                prev_status = status ;
            }
        }
예제 #2
0
 void convex_hull(const Polygon2d& PP, Polygon2d& result) {
     result.clear() ;
     int n = PP.size() ;
     vec2* P = new vec2[n+1] ;
     { for(int i=0; i<n; i++) {
         P[i] = PP[i] ;
     }}
     int u = make_chain(P, n, cmpl);  
     P[n] = P[0];
     int ch = u+make_chain(P+u, n-u+1, cmph);  
     {for(int i=0; i<ch; i++) {
         result.push_back(P[i]) ;
     }}
     delete[] P ;
 }