2014-10-04 52 views
2

我必须在一个圆的周长上有三个随机点,它有随机中心坐标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) 
+0

你的问题是什么? – 2014-10-04 00:27:58

+0

我相信所提供的答案是你正在寻找的。进一步澄清,如果不是很好:) – 2014-10-04 01:11:17

+0

@Matis你肯定不得不把一个** TypeSetting字符(<_dot_>)与一个Circle对象放在一个pixmap **上吗?你的文字听起来像是有这些前置条件设置的其他任务:** [a]产生一个CIRCLE **,它有一个随机的Centre_POINT,但是这个距离比每个Painting_AREA边缘的Radius_DISTANCE更远,** [ b]生成一个TRIANGLE **,它的所有顶点都在这个Circle会议的周边[a](意思是:生成一组三个不连续的随机角Phi1,Phi2,Phi3,用于极坐标(R,Phi )与中心[0,0]翻译[Circle.x,Circle.y]) – user3666197 2014-10-04 01:18:51

回答

3

发现了它。

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

每个人都应该cos(),其他sin()

+0

是的 - 简单的数学错误。 – Floris 2014-10-04 05:34:54

+0

谢谢!你是对的,没有看到。 – Matis 2014-10-04 07:58:45

-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) 
+0

一个'.'在Python中不是一个有效的名字 – 2014-10-04 00:44:52

+0

@AaronHall,反馈会很好,因为downvote是苛刻的: ) – 2014-10-04 01:06:32

+0

我想是因为你写了奇怪的答案,为什么'.'不是一个有效的名字,它真的没有任何与这个问题有关。 – W1ll1amvl 2014-10-04 01:27:29

1

可能有几种方法可以在tkinter中做到这一点,您可以使用标签或我将图片用作标签,但这取决于您。

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

.place与标签的左上方对齐。 fyi

现在已经注意到另一个人已经给出了一个可能是最好的解决方案的答案,但是我认为我不妨补充我的十分钱:)。