Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在圆周上找到点_Python_Math_Pygame - Fatal编程技术网

Python 如何在圆周上找到点

Python 如何在圆周上找到点,python,math,pygame,Python,Math,Pygame,如何在圆的圆周上找到用户定义的点数。找到后,将其放置在二维数组中,以备日后使用。要更好地了解它,请执行以下操作: 感谢所有的帮助:)您想找到点(x,y),这些点是方程x^2+y^2=R^2的解 首先请注意,x和y都必须在[-R,R]中,当您选择x(或y)时,可以通过求解y=sqrt(R^2-x^2)找到另一个 另一种方法是使用角度θ和x=R*cos(θ),y=R*sin(θ)。您只需求解θ,单位为[0,2*PI)弧度(sin,cos通常使用弧度,PI弧度为180度)。这取决于您是否要使用tri

如何在圆的圆周上找到用户定义的点数。找到后,将其放置在二维数组中,以备日后使用。要更好地了解它,请执行以下操作:

感谢所有的帮助:)

您想找到点(x,y),这些点是方程x^2+y^2=R^2的解

首先请注意,x和y都必须在[-R,R]中,当您选择x(或y)时,可以通过求解y=sqrt(R^2-x^2)找到另一个

另一种方法是使用角度θ和x=R*cos(θ),y=R*sin(θ)。您只需求解θ,单位为[0,2*PI)弧度(sin,cos通常使用弧度,PI弧度为180度)。这取决于您是否要使用trignometry(sin/cos是跨接函数)


只需将(x1,y1)添加到(x,y)即可将这些点(x,y)转换为新的中心(xc,yc),从而得到转换点(xt,yt)=(x+xc,y+yc)。

我假设您希望这些点的间距均匀

圆的中心为360度,或2pi弧度

你需要将2pi除以你想要的点数。比如说4->2pi/4

这是从一个点到下一个点的弧度数

要计算x和y坐标,请使用这两个方程r=sqrt(x2+y2),以及 θ=tan-1(y/x)

式中,θ1=0*2*pi/4,θ2=1*2*pi/4,θ3=2*2*pi/4,θ4=3*2*pi/4

有些代码如下所示:

import numpy as np

def get_points(radius, number_of_points):
    radians_between_each_point = 2*np.pi/number_of_points
    list_of_points = []
    for p in range(0, number_of_points):
        list_of_points.append( (radius*np.cos(p*radians_between_each_point),radius*np.sin(p*radians_between_each_point)) )
    return list_of_points

您也可以使用pygame的
Vector2
类。只需使用半径的长度旋转一个向量,并将其添加到圆的中心,即可获得圆周上的一个点

import pygame as pg
from pygame.math import Vector2


def points(number, center, radius):
    angle = 360/number
    point_list = []
    for i in range(number):
        # Create a vector with the length of the radius, rotate it
        # and add it to the center point to get a point on the circumference.
        vec = center + Vector2(radius, 0).rotate(i*angle)
        # pygame.draw.circle needs ints, so we have to convert the vector.
        point_list.append([int(vec.x), int(vec.y)])
    return point_list


def main():
    screen = pg.display.set_mode((640, 480))
    clock = pg.time.Clock()
    center = (320, 240)
    radius = 150
    number = 2
    point_list = points(number, center, radius)

    done = False

    while not done:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                done = True
            elif event.type == pg.KEYDOWN:
                if event.key == pg.K_c:
                    number += 1 # Increment the number and generate new points.
                    point_list = points(number, center, radius)

        screen.fill((30, 30, 30))
        pg.draw.circle(screen, (220, 120, 0), center, radius, 2)
        for pos in point_list:
            pg.draw.circle(screen, (0, 120, 250), pos, 20, 2)
        pg.display.flip()
        clock.tick(30)


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()

使用三角学:
(r*cos(t),r*sin(t))
参数化以半径原点为中心的圆
r
t
可以在间隔
[0,2*pi]内的等间距点上进行测量
。这些点是随机分布还是均匀分布?均匀分布,试图在pygameWow中重建环形网络拓扑!你读到我的想法了,我想问一些代码。我可以通过pip安装numpy,对吗?不必使用numpy,只要。
lop=get_points(50100)
打印即可(lop)
给了我一些负的x,y坐标值。按“c”添加更多的圆。这太完美了!非常感谢!这个答案太复杂了,假设用户立即熟悉等式逻辑和理论。