多边形的重心 [英] Center of gravity of a polygon

查看:208
本文介绍了多边形的重心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个PHP函数,将计算多边形的重心。

I am trying to write a PHP function that will calculate the center of gravity of a polygon.

我看了看其他类似的问题,但我似乎无法找到解决这一点。

I've looked at the other similar questions but I can't seem to find a solution to this.

我的问题是,我需要能够计算出重心的规则和不规则的多边形,甚至自相交的多边形。

My problem is that I need to be able to calculate the center of gravity for both regular and irregular polygons and even self intersecting polygons.

这可能吗?

我也读到: http://paulbourke.net/geometry/polyarea/ 但这仅限于无自相交多边形。

I've also read that: http://paulbourke.net/geometry/polyarea/ But this is restricted to non self intersecting polygons.

我怎样才能做到这一点?你可以点我朝着正确的方向?

How can I do this? Can you point me to the right direction?

推荐答案

重心(也被称为质量中心或质心,可以计算用下面的公式计算:

The center of gravity (also known as "center of mass" or "centroid" can be calculated with the following formula:

X = SUM[(Xi + Xi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A
Y = SUM[(Yi + Yi+1) * (Xi * Yi+1 - Xi+1 * Yi)] / 6 / A

维基百科提取: 一个非自相交的质心封闭多边形由n个顶点(X0,Y0),(X1,Y1),其定义...,(XN-1,炔1)是点(CX,CY),在那里
    
    
和其中,A是多边形的签名区,
    

Extracted from Wikipedia: The centroid of a non-self-intersecting closed polygon defined by n vertices (x0,y0), (x1,y1), ..., (xn−1,yn−1) is the point (Cx, Cy), where


and where A is the polygon's signed area,

使用VBasic例如:

Example using VBasic:

' Find the polygon's centroid.
Public Sub FindCentroid(ByRef X As Single, ByRef Y As _
    Single)
Dim pt As Integer
Dim second_factor As Single
Dim polygon_area As Single

    ' Add the first point at the end of the array.
    ReDim Preserve m_Points(1 To m_NumPoints + 1)
    m_Points(m_NumPoints + 1) = m_Points(1)

    ' Find the centroid.
    X = 0
    Y = 0
    For pt = 1 To m_NumPoints
        second_factor = _
            m_Points(pt).X * m_Points(pt + 1).Y - _
            m_Points(pt + 1).X * m_Points(pt).Y
        X = X + (m_Points(pt).X + m_Points(pt + 1).X) * _
            second_factor
        Y = Y + (m_Points(pt).Y + m_Points(pt + 1).Y) * _
            second_factor
    Next pt

    ' Divide by 6 times the polygon's area.
    polygon_area = PolygonArea
    X = X / 6 / polygon_area
    Y = Y / 6 / polygon_area

    ' If the values are negative, the polygon is
    ' oriented counterclockwise. Reverse the signs.
    If X < 0 Then
        X = -X
        Y = -Y
    End If
End Sub

有关详细信息检查本网站或的维基百科

For more info check this website or Wikipedia.

希望它帮助。

商祺!

这篇关于多边形的重心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆