gpt4 book ai didi

python - 圆周长有点的三角形, python

转载 作者:太空宇宙 更新时间:2023-11-04 01:13:27 24 4
gpt4 key购买 nike

我必须在一个圆的周长上制作一个具有 3 个随机点的三角形,该三角形具有随机的中心坐标 x 和 y(代码中的 random_x 和 random_y)。它将半径作为用户的输入。并且不能从任何一侧切割圆圈(这是有效的)。

我现在想弄明白的是,如何把"."放在圆的周边。(我自己只是中间一步,看看它是否在周边)

从那里我将移动到三角形线,但我需要先弄清楚这一点。也许我的代码中只是一个数学错误或者只是一些愚蠢的事情:

import tkinter, random, math

g = tkinter.Canvas(width=500, height=500)
g.pack()

radius = int(input("enter radius: "))

#generating random coordinates x and y, center of the circle
#"radius," and "-radius" are there so circle isn't out of canvas
random_x = random.randint(radius,500 - radius)
random_y = random.randint(radius,500 - radius)

#actual coordinates of Circle (Oval)
x1 = random_x - radius
y1 = random_y - radius

x2 = random_x + radius
y2 = random_y + radius

g.create_oval(x1,y1,x2,y2,fill='red')
# fill red is not neccessary

full_circle=2*math.pi
#random.uniform used because of floats ?
random_pi = random.uniform(0, full_circle)

point1 = random_x + (radius * math.cos(random_pi))
point2 = random_y + (radius * math.cos(random_pi))

#this "." is just to check if it is on perimeter of a circle
g.create_text(point1,point2,text=".")

#those prints are here just for check
print("random_x: ", random_x)
print("random_y: ", random_y)
print("radius: ", radius)
print("point1: ", point1)
print("point2: ", point2)

最佳答案

找到了。

point1 = random_x + (radius * math.cos(random_pi))
point2 = random_y + (radius * math.cos(random_pi))

一个应该是cos(),另一个是sin()

关于python - 圆周长有点的三角形, python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26188437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com