cqcn1991
V2EX  ›  问与答

Python: 怎么求 N 维点集的中点?

  •  
  •   cqcn1991 · May 17, 2015 · 1996 views
    This topic created in 4039 days ago, the information mentioned may be changed or developed.

    我只会这样求,但是显然对于N维来说,这个样子太累了...

    rectangle = [(0,0,0),(0,1,0),(1,1,0),(1,0,0)]
    x, y, z = zip(*rectangle)
    centroid = (sum(x)/len(x), sum(y)/len(y), sum(z)/len(z))
    

    不知道合适的语法应该怎么写?

    另外,再求教一下,3d geometry操作的lib有何推荐?
    比如我想rectangle.z = z0
    或者rectangle.scale((0,0,1), 0.5),朝着某个点缩放坐标,0.5倍
    再或者,求几分点等等

    3 replies    2015-05-17 13:50:35 +08:00
    Valyrian
        2
    Valyrian  
       May 17, 2015
    N维的话for loop一下不就行了。。
    Septembers
        3
    Septembers  
       May 17, 2015
    >>> rectangle = [(0, 0, 0), (0, 1, 0), (1, 1, 0), (1, 0, 0)]
    >>> centroid = [sum(x) / len(x) for x in zip(*rectangle)]
    >>> centroid
    [0, 0, 0]
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3038 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 38ms · UTC 13:21 · PVG 21:21 · LAX 06:21 · JFK 09:21
    ♥ Do have faith in what you're doing.