【问题标题】:Triangle with points on perimeter of circle, python在圆的周长上带有点的三角形,python
【发布时间】:2014-10-04 00:26:18
【问题描述】:

我必须在圆的周长上创建一个带有 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)

【问题讨论】:

  • 你的问题/问题是什么?
  • 我相信所提供的答案就是您要找的。进一步澄清如果不是很好:)
  • @Matis 你确定必须将 TypeSetting 字符( <_dot_> )放到带有 Circle 对象的像素图 上吗?您的文本听起来,您还有其他任务设置了这些先决条件:[a] 生成一个具有随机 Centre_POINT 的 CIRCLE,但是这样,它比每个 Painting_AREA 边缘的 Radius_DISTANCE 更远, [b] 生成一个 TRIANGLE,它的所有顶点都在这样一个 Circle 的周长上与 [a] 相交(意思是:生成一组三个不相交的随机角度 Phi1、Phi2、Phi3,用于极坐标(R,Phi),中心为 [0,0],由 [Circle.x, Circle.y] 翻译)
  • @user3666197 这个点对我自己有帮助,看看它是否在外围,如果我有这个权利,我会从那里继续创建三角形的线。点只是一个中间步骤。 感谢 Ignacio Vazquez-Abrams 他是对的,另一个应该是 sin()
  • @Matis 您自己可能已经意识到,使用 Polar Coordinate System ( R, Phi ) 在 2D 中通过 [ X,Y] (圆心的偏移), 任何点 (const( R_circle ), var( Phi ) ) 没有别的机会,只能在圆的周长上 (如果不是, 代码中存在错误,或者一些罕见的精度/舍入伪影,但在 x 方向和/或 y 方向上不大于 1 像素,因此与该点相关的三角形的两条线将不得不以完全相同的方式“受苦”。

标签: python tkinter


【解决方案1】:

找到了。

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

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

【讨论】:

    【解决方案2】:

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

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

    .place 与标签的左上角对齐。仅供参考

    现在注意到另一个人给出的答案可能是最好的解决方案,但我想我不妨加上我的十美分价值:)。

    【讨论】:

      【解决方案3】:

      如果你想做的是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)
      

      【讨论】:

      • a . 不是 Python 中的有效名称。
      • @AaronHall,反馈会很好,因为反对票很苛刻:)
      • 我想是因为你写了为什么. 不是一个有效的名字,这与问题没有任何关系。
      • @W1ll1amvl,那是由于对以前的注释有误解。我现在省略了它,并留下了正确的部分。
      • 如果不依赖外部链接,您的答案会更好。一旦该链接断开,您的答案就变得毫无用处。你能在你的答案中添加实际的工作代码吗?
      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 2013-12-24
      • 2017-12-04
      • 2012-10-20
      • 2016-08-09
      • 1970-01-01
      • 2017-11-01
      相关资源
      最近更新 更多