How can I make Turtle recognize a circle?

2020-04-18 03:57发布

问题:

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.

回答1:

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.



回答2:

I had quite similar assignment, tried to solve it the simple way:

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