I am trying to draw a target like this:
(source: newscientist.com)
using turtle.
The problem is turtle includes the origin as part of the graph, as opposed to the origin being in the center. My question is, how do I get turtle draw a circle AROUND the origin rather than include it?
import turtle
radius = 100
turtle.speed(0)
for rings in range(10):
turtle.circle(radius)
radius += 10
import turtle
radius = 100
turtle.speed(0)
for rings in range(10):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
radius += 10
It's nicer to use radius as the loop variable
import turtle
turtle.speed(0)
for radius in range(100, 200, 10):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
Then you might wish to define a function
import turtle
turtle.speed(0)
def origin_circle(turtle, radius):
turtle.penup()
turtle.goto(0, -radius)
turtle.pendown()
turtle.circle(radius)
for radius in range(100, 200, 10):
origin_circle(turtle, radius)
Related
I have a triangle and want to split it into 2 color, left side color red and the right side color yellow.
I can not.
If you can see my code, run it you see 2 triangles and I want to first want to split. See the picture example:
Here is my code:
import turtle
def dreieck(x, y, direction, size, color):
turtle.penup()
turtle.setpos(x, y)
turtle.setheading(direction)
turtle.pendown()
turtle.fillcolor(color)
turtle.begin_fill()
for i in range(3):
turtle.forward(size)
turtle.left(120)
turtle.end_fill()
dreieck(100, 200, 180, 200, "red")
turtle.left(120)
turtle.penup()
turtle.setpos(-50, -50)
turtle.setheading(180)
turtle.pendown()
dreieck(-100, -150, 0, 200, "blue")
The disconnect I see from the provided illustration is that the problem involves three isosceles right triangles but your code draws equilateral triangles! First, we need to change your code to draw the correct type of triangle and then apply it three times from the correct position and orientation. There are many ways to do this, here's an example:
import turtle
MY_HYPOTENUSE = 200
MY_LEG = MY_HYPOTENUSE * 2**0.5/2
def dreieck(hypotenuse, color):
leg = hypotenuse * 2**0.5/2
turtle.fillcolor(color)
turtle.begin_fill()
turtle.forward(hypotenuse)
turtle.left(135)
turtle.forward(leg)
turtle.left(90)
turtle.forward(leg)
turtle.left(135)
turtle.end_fill()
dreieck(MY_HYPOTENUSE, 'blue')
turtle.left(45)
turtle.penup()
turtle.forward(2 * MY_LEG)
turtle.pendown()
turtle.left(135)
dreieck(MY_HYPOTENUSE, 'green')
turtle.right(135)
turtle.backward(MY_LEG)
dreieck(MY_LEG, 'red')
turtle.hideturtle()
turtle.done()
There are likely more clever and efficient ways to do this.
I am making a tic tac toe game and when the user presses 'o' a circle is printed but the circle is always on the left of the turtle. i would like the turtle to be in the center of a box and draw the circle around itself.
You have to move turtle on your own - using left,right,forward, penup, pendowm.
Example
import turtle
radius = 100
# move
turtle.penup()
turtle.right(90)
turtle.forward(radius)
turtle.left(90)
turtle.pendown()
# circle
turtle.circle(radius)
# move back
turtle.penup()
turtle.right(-90)
turtle.forward(radius)
turtle.left(-90)
turtle.pendown()
Result:
There are a couple of ways to draw a circle centered around a Python turtle without moving the turtle. The first is the dot() method. It takes a diameter, rather than a radius, and optionally allows you to specify the color at the same time:
import turtle
RADIUS = 100
turtle.dot(RADIUS * 2)
turtle.dot(RADIUS * 1.6, turtle.bgcolor()) # "unfill" the circle
turtle.done()
Another way to do it is via stamping, that is, make the turtle cursor itself a circle, size it, and then call stamp():
import turtle
RADIUS = 100
CURSOR_RADIUS = 10
turtle.hideturtle()
turtle.shape('circle')
turtle.fillcolor(turtle.bgcolor())
turtle.shapesize(RADIUS / CURSOR_RADIUS, outline=RADIUS/5)
turtle.stamp()
turtle.done()
Both of the above have the side effect of overwritting anything that the circle surrounds, which doesn't seem like a problem for tic-tac-toe. To avoid this, you can, of course, temporarily shift the turtle's position, as #furas suggests:
import turtle
RADIUS = 100
turtle.width(RADIUS/5)
turtle.penup()
turtle.sety(turtle.ycor() - RADIUS)
turtle.pendown()
turtle.circle(RADIUS)
turtle.penup()
turtle.sety(turtle.ycor() + RADIUS)
turtle.pendown()
turtle.done()
I want to create concentric circles. I want the user to input how many circles they would like and at any specified radius. My problem is that the circles are not forming within one another.Any hints would be awesome
import turtle
import random
n1 = int(input("How many circles"))
n2 = int(input("Radius?"))
for i in range(n1):
turtle.penup()
turtle.right(50)
turtle.forward(i)
turtle.begin_fill()
turtle.color(random.random(), random.random(), random.random())
turtle.circle(n2)
turtle.end_fill()
turtle.pendown()
turtle.home()
while i in range(n1):
turtle.pendown()
turtle.right(40)
turtle.forward(i)
turtle.begin_fill()
turtle.color(random.random(), random.random(), random.random())
turtle.circle(n2-20)
turtle.end_fill()
turtle.penup()
import turtle
import random
num = int(input("How many circles? "))
radius = int(input("Radius? "))
# move to starting point - tangent to right edge of largest circle
turtle.forward(radius)
turtle.left(90)
# draw circles, largest to smallest
for circle in range(num, 0, -1): # (num .. 1)
# draw a filled circle
turtle.begin_fill()
turtle.color(random.random(), random.random(), random.random())
turtle.circle(radius * circle / num)
turtle.end_fill()
# shift inward to next circle starting-point
turtle.left(90)
turtle.forward(radius / num)
turtle.right(90)
given 6, 120 produces
Has anyone ever done this? Using multiple colors at the save time.
I have a list of colors:
colors = ["#880000",\
"#884400",\
"#888800",\
"#008800",\
"#008888",\
"#000088",\
"#440088",\
"#880088"]
My aim is to take that list of colors and pass it to turtle so it can draw a colorful circle in one line of turn.
my functions is as follows:
def drawImage(colorList, radius):
for color in colorList:
turtle.color(color)
turtle.penup()
turtle.setpos(0, -radius)
xpos=turtle.xcor()
ypos=turtle.ycor()
turtle.begin_fill()
turtle.pendown()
turtle.home()
turtle.setpos(xpos,ypos)
turtle.circle(radius)
turtle.end_fill()
turtle.color('black')
turtle.width(2)
turtle.circle(radius)
return
The problem with the above function is that it draws using only one color instead of little arcs of different colors from the list. can anyone help me solve this or point to what I'm doing wrong?
the function is called like drawImage(colors,200) this will draw a colorful circle with a radius of 200
Do you mean this circle ?
import turtle
colors = [
"#880000",
"#884400",
"#888800",
"#008800",
"#008888",
"#000088",
"#440088",
"#880088"
]
#--------------------
#turtle.reset()
angle = 360/len(colors)
turtle.width(10)
for color in colors:
turtle.color(color)
turtle.circle(100, angle)
There will be more work with filled circle because you have to draw filled "triangle" (arc) one by one.
EDIT:
import turtle
colors = [
"#880000",
"#884400",
"#888800",
"#008800",
"#008888",
"#000088",
"#440088",
"#880088"
]
#--------------------
def filled_arc(radius, angle, color):
turtle.color(color)
turtle.begin_fill()
turtle.forward(radius)
turtle.left(90)
turtle.circle(radius, angle)
turtle.left(90)
turtle.forward(radius)
turtle.end_fill()
turtle.left(180-angle)
#--------------------
angle = 360/len(colors)
for color in colors:
filled_arc(100, angle, color)
turtle.left(angle)
I have the following code which is supposed to draw a ring of colors around a circle but only one color if printed and changed 8 times before moving to the next
import turtle
def drawCircle(colorList, radius):
for color in colorList:
turtle.color(color)
for i in range(len(colorList)):
turtle.penup()
turtle.setpos(0, -radius)
xpos=turtle.xcor()
ypos=turtle.ycor()
head=turtle.heading()
turtle.begin_fill()
turtle.pendown()
turtle.home()
turtle.setpos(xpos,ypos)
turtle.setheading(head)
turtle.circle(radius)
turtle.end_fill()
turtle.penup()
return
colorList=["#880000","#884400","#888800","#008800",\
"#008888","#000088","#440088","#880088"]
drawCircle(colorList,200)
How would I make it that each arc around the circle is a different color. here is an example
you will need something like this
def drawSegment(color,x, y, r, angleStart, angleEnd, step=1):
#More efficient to work in radians
radianStart = angleStart*pi / 180
radianEnd = angleEnd*pi / 180
radianStep=step *pi/180
#Draw the segment
turtle.penup()
turtle.setpos(x,y)
turtle.color(color)
turtle.begin_fill()
turtle.pendown()
for theta in arange(radianStart,radianEnd,radianStep):
turtle.setpos(x + r * cos(theta), y + r * sin(theta))
turtle.setpos(x + r * cos(radianEnd), y + r * sin(radianEnd))
turtle.setpos(x, y);
turtle.end_fill()
def drawCircle(colorList,radius):
#do something to draw an equal segment for each color advancing it around 360 degree's