Solving polygon centroid

In the front end of the development, especially in the front end of the game development process, many scenes request centroid of a polygon. For example, in constructing the map consist of polygons, for aesthetic purposes we need to place names marked on the map of the center of mass, the polygon game gravitational field needs to be calculated in accordance with the movement of the centroid. This article details the thinking process of solving the polygon centroid.

First, from a simple system to start

The figure is a system consisting of a, b two dots, wherein a mass of ma, b is a mass mb. We can balance the principle of leverage, we determined these two points of focus (to k). which is:

(k.x-a.x)mag1=(b.x-k.x)mbg2
k.x=(a.x*mag1+b.x*mbg2)/(mag1+mbg2)

A uniform gravitational field, i.e., like a case where g1 = g2, and center of gravity coincides with the center of mass, thus center of mass of this system is:

kx = (ax + BX * in * mb) / (at + mb)

Two systems, a plurality of points

1. three points

Add point c, point c is set at a mass mc, we put a, b as a subsystem, so that the quality of the subsystem mk, then mk = (ma + mb). The system is provided particle coordinate point l, the same manner can be deduced:

LX = (kx * mk + cx * mc) / (mk + mc) = (Ax * ma + bx * mb + cx * mc) / (ma + mb + mc); 
ly = (ky * mk + Cy * mc) / (mk + mc) = (ay * ma + by * mb + Cy * mc) / (ma + mb + mc);

2. The plurality of points

By extension, we can find that there are n points of the center of mass of the system, where p1, p2 ... to coordinate the plurality of points, m1, m2 ... for the corresponding point of quality:

ln.x = (p1.x * m1 + p2.x * m2 + p3.x * m3 + ... + pn.x * mn) / (m1 + m2 + m3 + ... + mn); 
ln.y = (p1.y * m1 + p2.y * m2 + p3.y * m3 + ... + pn.y * mn) / (m1 + m2 + m3 + ... + mn);

Third, find the centroid of the triangle

Triangle centroid of triangle equivalent to the system by three vertices of the centroid, and the same quality of three vertices, i.e., ma = mb = mc, apply the above equation, we obtain:

x=(x1+x2+x3)/3;
y=(y1+y2+y3)/3;

Fourth, find the centroid of a convex polygon

First consider the case where a quadrangle, we let each vertex mass is m, then the three points classified as system 1, system 2 is further classified point. Mass of the system 1 and system 2 and the centroid is known, according to the principle of the lever balance, can be obtained quadrilateral centroid:

x=(x1+x2+x3+x4)/4;
y=(y1+y2+y3+x4)/4;

The same can be obtained by a convex polygon with n vertices centroid is:

x = (x1 + x2 + x3 + ... + xn) / n; 
y = (y1 + y2 + y3 + ... + a) / n;

Fifth, seeking concave polygon centroid

For concave polygon can not be calculated according to the above embodiment, as described above only considers the case where the point, but did not consider the pattern thereof. For concave polygon, the vertices of the same distribution, different compositions may be polygonal, their centroid vary greatly, as shown below:


For concave edge shape, we can put it into the system a plurality of triangles, where each triangle can be abstracted as a point p with the quality, the position of the point of the centroid of the triangle, and the triangle is proportional to the area of ​​mass points, i.e., m = s * k.

The formula to solve the centroid of the system of n points, the concave edge shape can be obtained centroid Solving equation:

x=(p1.x*s1+p2.x*s2+p3.x*s3+...+pn.x*sn)/(s1+s2+s3+...+sn);
y=(p1.y*s1+p2.y*s2+p3.y*s3+...+pn.y*sn)/(s1+s2+s3+...+sn);

Wherein p1, p2, p3 ... triangular centroid, s1, s2, s3 ... triangular area.

Of course, this formula can also find the centroid of a convex polygon.

Six technical knowledge

1. Known apex of the triangle area of ​​a triangle seek

Can be calculated using the vector cross product of two sides of the triangular configuration wherein the reference: dot product and cross product

2. polygon triangulation

Solving concave polygon centroid, need more polygons into triangles, you can self-correlation algorithm search elements, then I will write a special article discussion.

 

Guess you like

Origin www.cnblogs.com/snsart/p/10939524.html