 -- area of the input polygon
 p_area     float;
 -- cut line
-cut_result geometry[];
+cut_result  geometry[];
 BEGIN
     poly   := (SELECT way FROM parcel WHERE gid = p_uid);
     p_area := ST_Area(poly);

    cut_corner := find_cut_corner(lrc);
    RAISE NOTICE 'cut corner: %', cut_corner;

    IF    cut_corner = 'NW' THEN
        inset_split := h_split(poly,sqrt(target_area),bxmin,bxmax,bymin,bymax);
        IF ST_Area(inset_split[2]) < target_area THEN
            cut_result := hcut_search(poly,1,target_area,bxmin,bxmax,bymin,bymax);
        ELSE
            cut_result := vcut_search(poly,1,target_area,bxmin,bxmax,bymin,bymax);
        END IF;
    ELSIF cut_corner = 'NE' THEN 
        inset_split := h_split(poly,sqrt(target_area),bxmin,bxmax,bymin,bymax);
        IF ST_Area(inset_split[2]) < target_area THEN
            cut_result := hcut_search(poly,1,target_area,bxmin,bxmax,bymin,bymax);
        ELSE
            cut_result := vcut_search(poly,2,target_area,bxmin,bxmax,bymin,bymax);
        END IF;
    ELSIF cut_corner = 'SE' THEN 
        inset_split := h_split(poly,sqrt(target_area),bxmin,bxmax,bymin,bymax);
        IF ST_Area(inset_split[3]) < target_area THEN
            cut_result := hcut_search(poly,2,target_area,bxmin,bxmax,bymin,bymax);
        ELSE
            cut_result := vcut_search(poly,2,target_area,bxmin,bxmax,bymin,bymax);
        END IF;
    ELSIF cut_corner = 'SW' THEN 
        inset_split := h_split(poly,sqrt(target_area),bxmin,bxmax,bymin,bymax);
        IF ST_Area(inset_split[3]) < target_area THEN
            cut_result := hcut_search(poly,2,target_area,bxmin,bxmax,bymin,bymax);
        ELSE
            cut_result := vcut_search(poly,1,target_area,bxmin,bxmax,bymin,bymax);
        END IF;
    END IF;
        
    INSERT INTO support(way) SELECT cut_result[1];

    RETURN true;
END;
$$ LANGUAGE plpgsql;

SELECT pseudo_parcel(1,30000.0);
\set QUIET 0
SELECT parcels_draw();


DECLARE
rand_corner integer;
BEGIN
    IF     (ARRAY['N'] <@ lrc AND ARRAY['W'] <@ lrc) OR ARRAY['NW'] <@ lrc THEN
        RETURN 'NW';
    ELSIF  (ARRAY['N'] <@ lrc AND ARRAY['E'] <@ lrc) OR ARRAY['NE'] <@ lrc THEN
        RETURN 'NE';
    ELSIF  (ARRAY['S'] <@ lrc AND ARRAY['W'] <@ lrc) OR ARRAY['SW'] <@ lrc THEN
        RETURN 'SW';
    ELSIF  (ARRAY['S'] <@ lrc AND ARRAY['E'] <@ lrc) OR ARRAY['SE'] <@ lrc THEN
        RETURN 'SE';
    ELSE
        -- no definitive corner was identified.
