Results 1 to 8 of 8

Thread: Fix Polygon Vertexorder

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Sergeant serthy's Avatar
    Join Date
    Nov 2012
    Posts
    450
    Thanks
    96
    Thanked 296 Times in 188 Posts

    Fix Polygon Vertexorder

    Sanibonani =)

    I have an array of n unsorted points (x,y,z).
    These vertices will create a !convex! polygon.
    Now I need your help to create an indexlist of these vertices in a sorted way:

    Example:

    5 points in 3d space: ( A , B , D , C )
    Code:
    	A      B
    
    
    
    
    
    
    	C      D
    connect them in the unsorted order as they are:
    Code:
    	A------B
    	 \    /
    	  \  /
    	   \/
    	   /\
    	  /  \
    	 /    \
    	C------D
    now sort them (counter-)clock wise:
    Code:
    	A      B
    
    
    
    
    
    
    	D      C
    and connect them again: ( A , B , C , D )
    Code:
    	A------B
    	|      |
    	|      |
    	|      |
    	|      |
    	|      |
    	|      |
    	C------D
    I am currently going over a dotProduct from the center of the polygon, but stucked there...
    Maybe arcTan( x / y ) works!?

    PHP Code:
    sortVertsOnPolyverts center )
    {
        
    dots = [];
        
    idx = [];

        for( 
    verts.size i++ )
        {
            
    v1 verts[i];                //catch current vert
            
    v2 verts[( ) % verts.size];    //catch next vert to compare to

            
    v1 -= center;    //calculate current directions
            
    v2 -= center;

            
    dots[i] = ( v1[0] * v2[1] - v1[1] * v2[0] ); //calculate the dot(2D)
            
    idx[i] = i;    //store the index-information in a different array
        
    }

        
    //sort them by their dot-value
        
    for( dots.size i-- )
        {
            for( 
    j++ )
            {
                if( 
    dots[i] > dots[1] )    
                {
                    
    dots[i];    //swap
                    
    dots[i] = dots[1];
                    
    dots[1] = t;

                    
    idx[i];        //also swap the index array
                    
    idx[i] = idx[1];
                    
    idx[1] = t;
                }
            }
        }

        return 
    idx;    //return sorted index array

    EDIT:

    or just get vertex #1 as reference and calculate all other angles and compare them:
    guess this woll work also... geez, why i didnt get this in my first toughts?!

    PHP Code:
        dirs = [];
        
    dirs[0] = 0;
        
    dir vectorNormalizeverts[0] - center );

        for( 
    verts.size i++ )
        {
            
    dirs[i] = aCosvectorDotdir vectorNormalizeverts[i] - center ) ) );
            
    idx[i] = i;
        } 
    EDIT: dont mind, im confused, the dot is the almost angle ~.~
    Last edited by serthy; 27th January 2013 at 18:18.

  2. The Following User Says Thank You to serthy For This Useful Post:

    kung foo man (27th January 2013)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •