Sort the "compute geometry" polygon point set

Source: Internet
Author: User

Problem Description: The known polygon point set C={p1,p2,..., PN}, its order is messy, connecting the n points in turn, unable to form a definite polygon, need to sort the point set C, then draw the polygon.

The key to the point set sorting process is how to define the point size relationship.

To sort by counter-clockwise as an example, the algorithm steps are as follows:

Definition: Point A is counterclockwise in point B, then point A is greater than point B

1. Calculate the center of gravity o of the point set, with the centre of gravity as the central point of counterclockwise rotation.

2. Calculate the size relationship between points.

The calculation of the size relationship can be calculated by two methods.

Method 1:

Use the center of gravity o as a unit vector parallel to the X axis Ox, then calculate the angle between OPI and ox in turn, and determine the size relationship between the points according to the size of the angle.

The larger the angle between OPI and OX, the smaller the point pi.

Method 2:

According to the definition of the vector cross product, the cross product of the vector OPI and OPJ is greater than 0, then the vector opj is in the counterclockwise direction of the vector OPI, that is, the point PJ is less than the dot pi.

According to Method 2, the code for sorting the polygon point set is as follows:

 1 typedef struct POINT 2 {3 int x; 4 int y; 5}point;  6//If point A is greater than point B, that is, point A is clockwise in point B, returns True, otherwise false 7 bool pointcmp (const point &a,const-&b,const point &center) 8 {9 if (a.x >= 0 && b.x < 0) return true;11 if (a.x = = 0 && b.x = 0) R Eturn a.y > b.y;13//Vector oa and vector ob cross product int det = (a.x-center.x) * (B.Y-CENTER.Y)-(b.x-center.x) * (a.y-c ENTER.Y); Det < 0) return true;17 if (Det > 0) return false;19//vector oa and vector ob collinear to Distance judgment size: int d1 = (a.x-center.x) * (a.x-center.x) + (A.Y-CENTER.Y) * (A.Y-CENTER.Y); int d2 = (B.x-cen ter.x) * (B.X-CENTER.Y) + (B.Y-CENTER.Y) * (B.Y-CENTER.Y); return d1 > d2;23}24 void clockwisesortpoints (s Td::vector<point> &vpoints) 25 {26//COMPUTE center of gravity CV::P oint center;28 Double x = 0,y = 0;29 for (int i = 0;i < Vpoints.size (); i++) {x + + vpoints[i].x;32 y + =vpoints[i].y;33}34 center.x = (int) x/vpoints.size (); center.y = (int) y/vpoints.size (); 36 37//Bubble Sort 38         for (int i = 0;i < Vpoints.size ()-1;i++) (int j = 0;j < Vpoints.size ()-i-1;j++) 41 {pointcmp (Vpoints[j],vpoints[j+1],center)) (+)::P oint tmp = vpoints     [j];45 vpoints[j] = vpoints[j + 1];46 vpoints[j + 1] = tmp;47}48}49 }50}

Resources:

http://blog.csdn.net/beyond071/article/details/5855171

Http://stackoverflow.com/questions/6989100/sort-points-in-clockwise-order

Sort the "compute geometry" polygon point set

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.