我怎样才能让龟认出一个圆圈? [英] How can I make Turtle recognize a circle?

查看:129
本文介绍了我怎样才能让龟认出一个圆圈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用Turtle Graphics制作一个Python程序,在矩形内绘制两个重叠的圆圈(如维恩图),并将随机点绘制到维恩图上。



我已经成功完成了这个任务,但是现在我想让程序识别一个点是否位于其中一个圆圈或维恩图的交点处。然后我想根据他们在哪个区域改变点的颜色。



到目前为止我为程序所做的工作是列出变量,定义了形状并创建了一个for循环来随机生成点。

解决方案

turtle is只是一个图形库 - 它不会跟踪你在屏幕上绘制的对象。因此,要计算某个点是否位于您的维恩图圈之一内,您需要执行以下步骤:


  1. 商店当你调用 circle()
    时,每个圆的坐标都是有用的,但是你可能还没有学到这些。
  2. 调用函数以测试点是否在存储的圆形坐标空间中。这将是对笛卡尔坐标的纯数学运算。链接@Tim给了(等式测试是否一个点

有关步骤1的一点指导:



绘制圆时,您的中心(当前海龟位置)和半径。从那里,获得该圆圈内的所有点只是几何图形(如果无法推导出公式,快速搜索将帮助您完成)。我建议你制作一个绘制维恩图圆的函数,以及一个返回圆内点的函数。就像这样:

  def venn_circle(circle_color,circle_radius):
绘制一个彩色圆圈,返回点。
turtle.color(circle_color)
#<填入:移动的代码,定位乌龟>
center = turtle.position()
#<填写:绘制圆的代码>
return circle_coords(center,circle_radius)

$ b $ def circle_coords(center,radius):
返回圆圈内的像素组。
raise NotImplementedError()

c>从包导入* 。在某些情况下没问题,但通常会导致麻烦。在我的示例代码中,我假设你用 import turtle 替代了这个习语。


I am trying to make a Python program with Turtle Graphics that draws 2 circles overlapping (like a Venn Diagram) within a rectangle, and plots random dots onto the Venn Diagram.

I have successfully done this, but now I want to make the program recognize if a point is in one of the circles or in the intersection of the Venn Diagram. I then want to change the color of the dots depending on which region they're in.

What I have done so far for the program is listed variables, defined the shapes and made a for loop to randomly generate points.

解决方案

turtle is just a graphics library- it doesn't keep track of the objects you've drawn on screen. So, to calculate if a given point is within one of your Venn diagram circles, you'll need to take the following steps:

  1. Store each circle's coordinates when you call circle() (classes would be helpful, but chances are you haven't learned those yet)
  2. Call a function to test if the point is in the stored circle coordinate space. This will be a purely mathematical operation on Cartesian coordinates. The link @Tim gave (Equation for testing if a point is inside a circle) will help you achieve this.

A little guidance on step 1:

When you draw a circle, you have its center (current turtle position), and a radius. From there, obtaining all points within that circle is just geometry (if you can't derive the formula, a quick search will help you out). I'd suggest that you make a function that draws a Venn diagram circle, and one that returns the points within a circle. Something like this:

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()

And one quick note- you should never do from package import *. It's okay in some cases, but will generally just lead to trouble. In my example code, I've assumed you've substituted this idiom for import turtle.

这篇关于我怎样才能让龟认出一个圆圈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆