程序员

生成正多边形

2017-11-14  本文已影响58人  Zero2none
正多边形

思路

n多边形应该是是建立在上的,
θ随着 n 而变化,有θ=2π/n关系,
据初始顶点坐标即可依次求出所有顶点坐标。

Pycode

import pylab as pl
%matplotlib inline

def polygon(n=3, startX=100, startY=0, r=100):
    """
    Ploygon for docstring.
    Get ploygon x and y postions.
    ---------------------

    Paramter
        n: int, polygon's sides, default 3.
        startX: int, fitst x postion, default 100.
        startY: int, fitst y postion, default 0.
        startX: int, fitst x postion, default 100.
        r: int, radius, default 100.

    Returns
        x, y: list, x and y contain all postions.
    """
    theta = 2 * pl.pi / n
    x = [startX - r * pl.sin(theta * i) if i >
         0 else startX for i in range(n + 1)]
    y = [startY + r - r * pl.cos(theta * i)
         if i > 0 else startY for i in range(n + 1)]
    return x, y
x, y = polygon(3)
pl.plot(x, y)
ax = pl.gca()
ax.set_aspect(1)  # Make xticks' length qeual to yticks'length.
三角形的说

SourceCode

import pylab as pl


def polygon(n=3, startX=100, startY=0, r=100):
    """
    Ploygon for docstring.
    Get ploygon x and y postions.
    ---------------------

    Paramter
        n: int, polygon's sides, default 3.
        startX: int, fitst x postion, default 100.
        startY: int, fitst y postion, default 0.
        startX: int, fitst x postion, default 100.
        r: int, radius, default 100.

    Returns
        x, y: list, x and y contain all postions.
    """
    theta = 2 * pl.pi / n
    x = [startX - r * pl.sin(theta * i) if i >
         0 else startX for i in range(n + 1)]
    y = [startY + r - r * pl.cos(theta * i)
         if i > 0 else startY for i in range(n + 1)]
    return x, y


x, y = polygon(3)
pl.plot(x, y)
ax = pl.gca()
ax.set_aspect(1)  # Make xticks' length qeual to yticks'length.
pl.show()

写在后面的

啊~在用程序画图是建立在坐标上的,没有坐标,那就算了喽


参考


最后,就酱


填坑,补完 https://onlyyouknow.me/2018/11/04/Regular-polygon-algorithm.html

上一篇下一篇

猜你喜欢

热点阅读