三角形与圆周边的点,蟒蛇

时间:2014-10-04 00:26:18

标签: python tkinter

我必须在一个圆的周边做一个有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)

3 个答案:

答案 0 :(得分:3)

找到它。

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

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

答案 1 :(得分:1)

在tkinter中可能有两种方法可以做到这一点,你可以使用标签,或者我会用图像作为标签,但这取决于你。

要获得所需的标签,您可以使用.place(x=..., y=...),因为您拥有该点的坐标。这意味着您应该将现有的.pack()更改为.place()

.place与标签的左上角对齐。 FYI

现在已经注意到另一个人已经给出了答案,这可能是最好的解决方案,但我想我也可以加上我的10美分价值:)。

答案 2 :(得分:-1)

如果你要做的是draw a dot in tkinter, then this link will assist

来自Link Provided

的解决方案

Draw a 1 line pixel to represent your dot

您的坐标位于point1point2,因此非常简单:

line = canvas.create_line(point1, point2, point1+1, point2+1)
相关问题