Python 我怎样才能让乌龟认出一个圆圈?
我正在尝试用Turtle图形制作一个Python程序,它在一个矩形内绘制两个重叠的圆(如Venn图),并在Venn图上绘制随机点 我已经成功地做到了这一点,但是现在我想让程序识别一个点是在一个圆中还是在维恩图的交点上。然后我想根据点所在的区域改变点的颜色Python 我怎样才能让乌龟认出一个圆圈?,python,geometry,turtle-graphics,Python,Geometry,Turtle Graphics,我正在尝试用Turtle图形制作一个Python程序,它在一个矩形内绘制两个重叠的圆(如Venn图),并在Venn图上绘制随机点 我已经成功地做到了这一点,但是现在我想让程序识别一个点是在一个圆中还是在维恩图的交点上。然后我想根据点所在的区域改变点的颜色 到目前为止,我为该程序所做的是列出变量、定义形状并创建for循环以随机生成点。turtle只是一个图形库,它不跟踪您在屏幕上绘制的对象。因此,要计算给定点是否在Venn图圆内,需要执行以下步骤: 调用circle() (上课会有帮助,但很可能你
到目前为止,我为该程序所做的是列出变量、定义形状并创建for循环以随机生成点。
turtle
只是一个图形库,它不跟踪您在屏幕上绘制的对象。因此,要计算给定点是否在Venn图圆内,需要执行以下步骤:
circle()
(上课会有帮助,但很可能你还没有学会)def venn_circle(circle_color, circle_radius):
""" Draws a colored circle, returns the points within. """
turtle.color(circle_color)
# <fill in: code to move, orient the turtle>
center = turtle.position()
# <fill in: code to draw the circle>
return circle_coords(center, circle_radius)
def circle_coords(center, radius):
""" Return the set of pixels within the circle. """
raise NotImplementedError()
def venn_圆(圆颜色、圆半径):
“”“绘制一个彩色圆圈,返回其中的点。”“”
海龟颜色(圆圈颜色)
#
中心=海龟位置()
#
返回圆坐标(中心、圆半径)
def圆坐标(中心、半径):
“”“返回圆内的像素集。”“”
引发未实现的错误()
还有一个提示-您永远不应该从软件包导入中执行
*
。在某些情况下这是可以的,但通常只会导致麻烦。在我的示例代码中,我假设您已经用这个习惯用法替换了导入turtleturtle
,它只是一个图形库-它不跟踪您在屏幕上绘制的对象。因此,要计算给定点是否在Venn图圆内,需要执行以下步骤:
circle()
(上课会有帮助,但很可能你还没有学会)def venn_circle(circle_color, circle_radius):
""" Draws a colored circle, returns the points within. """
turtle.color(circle_color)
# <fill in: code to move, orient the turtle>
center = turtle.position()
# <fill in: code to draw the circle>
return circle_coords(center, circle_radius)
def circle_coords(center, radius):
""" Return the set of pixels within the circle. """
raise NotImplementedError()
def venn_圆(圆颜色、圆半径):
“”“绘制一个彩色圆圈,返回其中的点。”“”
海龟颜色(圆圈颜色)
#
中心=海龟位置()
#
返回圆坐标(中心、圆半径)
def圆坐标(中心、半径):
“”“返回圆内的像素集。”“”
引发未实现的错误()
还有一个提示-您永远不应该从软件包导入中执行
*
。在某些情况下这是可以的,但通常只会导致麻烦。在我的示例代码中,我假设您已将此习语替换为import-turtle
我有一个非常类似的任务,尝试用简单的方法解决它:
import tkinter
import random
canvas = tkinter.Canvas(width = 300, height = 200, bg = "white")
canvas.pack()
n = 500
for i in range(n):
x = random.randrange(0, 300)
y = random.randrange(0, 200)
bod = canvas.create_oval(x+3, y+3, x-3, y-3, fill = "black")
if (x - 100)**2+(y - 100)**2 < 80**2: #for dot in circle1 fill red
canvas.itemconfig(bod, fill = "red")
if (x - 180)**2+(y - 100)**2 < 90**2: #for dot in circle2 fill blue
canvas.itemconfig(bod, fill = "blue")
if (x - 100)**2+(y - 100)**2 < 80**2 and (x - 180)**2+(y - 100)**2 < 90**2:
canvas.itemconfig(bod, fill = "green") #overlapping of circle1 and 2
导入tkinter
随机输入
canvas=tkinter.canvas(宽度=300,高度=200,bg=“白色”)
canvas.pack()
n=500
对于范围(n)中的i:
x=random.randrange(0,300)
y=random.randrange(0,200)
bod=画布。创建椭圆(x+3,y+3,x-3,y-3,fill=“黑色”)
如果(x-100)**2+(y-100)**2<80**2:#对于圆圈1中的点填充红色
canvas.itemconfig(bod,fill=“red”)
如果(x-180)**2+(y-100)**2<90**2:#对于圆圈中的点2填充蓝色
canvas.itemconfig(bod,fill=“blue”)
如果(x-100)**2+(y-100)**2<80**2和(x-180)**2+(y-100)**2<90**2:
canvas.itemconfig(bod,fill=“green”)#圆圈1和2的重叠
我有一个非常类似的任务,试图用简单的方法解决它:
import tkinter
import random
canvas = tkinter.Canvas(width = 300, height = 200, bg = "white")
canvas.pack()
n = 500
for i in range(n):
x = random.randrange(0, 300)
y = random.randrange(0, 200)
bod = canvas.create_oval(x+3, y+3, x-3, y-3, fill = "black")
if (x - 100)**2+(y - 100)**2 < 80**2: #for dot in circle1 fill red
canvas.itemconfig(bod, fill = "red")
if (x - 180)**2+(y - 100)**2 < 90**2: #for dot in circle2 fill blue
canvas.itemconfig(bod, fill = "blue")
if (x - 100)**2+(y - 100)**2 < 80**2 and (x - 180)**2+(y - 100)**2 < 90**2:
canvas.itemconfig(bod, fill = "green") #overlapping of circle1 and 2
导入tkinter
随机输入
canvas=tkinter.canvas(宽度=300,高度=200,bg=“白色”)
canvas.pack()
n=500
对于范围(n)中的i:
x=random.randrange(0,300)
y=random.randrange(0,200)
bod=画布。创建椭圆(x+3,y+3,x-3,y-3,fill=“黑色”)
如果(x-100)**2+(y-100)**2<80**2:#对于圆圈1中的点填充红色
canvas.itemconfig(bod,fill=“red”)
如果(x-180)**2+(y-100)**2<90**2:#对于圆圈中的点2填充蓝色
canvas.itemconfig(bod,fill=“blue”)
如果(x-100)**2+(y-100)**2<80**2和(x-180)**2+(y-100)**2<90**2:
canvas.itemconfig(bod,fill=“green”)#圆圈1和2的重叠
。欢迎使用StackOverflow可能重复!这是一篇经常被引用的文章。简言之,当问你的问题时,你应该证明你已经为解决问题付出了努力。理想情况下,您将有一些代码要显示,以及该代码的特定问题。如果你坚持下去,你会得到更多积极的回应!。可能重复欢迎使用StackOverflow!这是一篇经常被引用的文章。简言之,当问你的问题时,你应该证明你已经为解决问题付出了努力。理想情况下,您将有一些代码要显示,以及该代码的特定问题。你会得到更多积极的回应