Python turtle draw a fatline with three parameters - python

Write a function named fatLine(). The function fatLine() takes
three parameters:
a turtle, t
an integer, segments, that is the number of segments in the line that is drawn
an integer, increment, that is how much wider each successive segment of the line is
The function fatLine() should use the turtle t to draw a line
composed of connected segments. Each segment should be of length 50.
The width of the first line segment should be (the parameter)
increment, and each successive segment should be wider than the
preceding segment by increment. For example, if segments = 5 and
increment = 10, the following is correct output
I tried coding this with three parameters and i am not sure how else i would make it run. Any help would be great Thanks.
I am trying to code this in idle but i have no luck. Please help with any idea on how to execute.

Since it's been five years, it's time this question, despite not showing coding effort, had an answer:
from turtle import Screen, Turtle
def fatLine(t, segments, increment):
width = increment
for _ in range(segments):
t.width(width)
t.forward(50)
width += increment
screen = Screen()
turtle = Turtle(visible=False)
turtle.penup()
turtle.backward(200)
turtle.pendown()
fatLine(turtle, 8, 10)
screen.exitonclick()

Related

The iterating polygons increasing by length of 10 px eachtime don't center perfectly with its inner polygon. What could the maths after line 11 be?

Why don't the iterating polygons not align perfectly. (if I try to make a polygon with 4 sides, it works fine, but any other shape and it aligns a bit differently). Is it something to do from line 11 to line 16?
This is the question I am trying to solve with my function
import turtle
t = turtle.Turtle()
t.speed(5)
def draw_shape(length, sides, colores, times):
for timestotal in range(1, times+1):
for side in range(sides):
t.color(colores)
t.forward(length*timestotal)
t.right(360/sides)
t.penup()
t.back(length*2)
t.left(360/sides)
t.forward(length*2)
t.right(360/sides)
t.pendown()
draw_shape(4, 8, "red", 8)
It doesn't really matter if it is ascending or descending lengths as long as all the shapes are centered as is in the exercise.
Unfortunately if the parameter is anything other than 4 the shapes do not align properly
If I pass different commands between penup() and pendown() for each number from 3 to 13 using if, elif, and else statements, it does align the shapes but each number(length) needs its own sets of code:
Could it be something around this command:
t.goto(-(lengthtimestotal)/2,(lengthtimestotal)/2)
It seems like the problem can be reduced to "draw a regular polygon of n sides from a center point". If you can do that, then you need only iterate with different sizes in the outer loop, all drawing from the same point (the turtle's current location).
I don't think it's easy to draw a regular polygon from a center point using only forward, backward and turning commands. But it's possible to use the classic trig polygon approach to compute the vertices of the polygon around the circle:
import turtle
from math import cos, radians, sin
def draw_shape(length, color, sides, times):
t.color(color)
x, y = t.pos()
side = 360 // sides
for i in range(times):
current_length = length // times * (1 + i)
t.penup()
for angle in range(0, 361, side):
t.goto(
current_length * cos(radians(angle - side / 2)) + x,
current_length * sin(radians(angle - side / 2)) + y
)
t.pendown()
t = turtle.Turtle()
t.speed(5)
draw_shape(length=100, color="red", sides=8, times=5)
turtle.exitonclick()
The angle - side / 2 bit is used to rotate the polygon by half a side to match the spec.
I also see draw_shape(100, "blue", 4, 3) has unusual output. You can get this with current_length = length - (20 * i) which hardcodes the step size. Not very pleasant to have to do, but such it is.

How to draw a heptagon using Python Turtle with the point at the top?

I am drawing a heptagon using this code:
tegan.setheading(0);
for i in range (7):
tegan.right(51.43)
tegan.forward(100)
However, this code always draws the shape with a flat edge at the top and I want the point at the top. What am I doing wrong?
To make an heptagon, you need to rotate 360°/7 each step. To have the point up, you rotate only half on the first step.
One trick to do this is to rotate in one direction (here left) half the angle, and then proceed in the other direction with the full angle seven times. Another possibility is to write a condition in the loop for the first step, or split the code in two.
import turtle
angle = 360/7
turtle.left(angle/2)
for i in range(7):
turtle.right(angle)
turtle.forward(100)
Alternatively, you can simply do:
import turtle
turtle.circle(-116, steps=7)
turtle.done()

How to use python to move the mouse in circles

I'm trying to write a script in python, to automatically force the movement of the mouse pointer without the user's input (it quits through the keyboard), and experimenting with PyAutoGUI, PyUserInput and ctypes, I've been figuring out ways to move the pointer with constant speed, instead of having it teleport across the screen(I need the user to be able to see the path it makes). However, I need it to be able to perform curves, and particularly, circles, and I haven't found a way to do so with the aforementioned libraries. Does anybody know of a way to code them into making the mouse describe circles across the screen at constant speed, instead of just straight lines? Thank you beforehand for any input or help you may provide.
This is my attempt at making circle at the center of the screen of radius R - also note if I don't pass parameter duration then the mouse pointer moves to the next coordinates instantly. So for a circle divided into 360 parts you can set the pace using a modulus.
import pyautogui
import math
# Radius
R = 400
# measuring screen size
(x,y) = pyautogui.size()
# locating center of the screen
(X,Y) = pyautogui.position(x/2,y/2)
# offsetting by radius
pyautogui.moveTo(X+R,Y)
for i in range(360):
# setting pace with a modulus
if i%6==0:
pyautogui.moveTo(X+R*math.cos(math.radians(i)),Y+R*math.sin(math.radians(i)))
There is a way to do this using sin, cos, and tan. (I haven't been able to test this code yet, It might not work.)
Import math
Import pyautogui
def circle(radius = 5, accuracy = 360, xpos=0, ypos=0, speed = 5):
local y
local x
local angle
angle = 360/accuracy
local CurAngle
CurAngle = 0
x = []
y = []
sped = speed/accuracy
for i in range(accuracy):
x.append(xpos + radius*math.sin(math.radians(CurAngle)))
y.append(ypos + radius*math.cos(math.radians(CurAngle)))
CurAngle += angle
for i in len(x):
pyautogui.moveTo(x[i], y[i], duration = sped)
You put this near the top of your script, and pass arguments like this:
circle(radius, accuracy, xpos, ypos, speed)
Radius controls the width of the circle
Accuracy controls how many equi-distant points the circle is to be broken up into, setting accuracy to 4 will put 4 invisible points along the circle for the mouse to travel tom which will make a square, not a circle, 5 makes a pentagon, 6 a hexagon, etc.. the bigger the radius, the bigger you will want the accuracy
Xpos controls the x position of where the circle is centered
Ypos controls the y position of where the circle is centered
Speed controls how many seconds you want it to take to draw the circle.
Hope this helps :) Would you mind elaborating what you are wanting when you say 'curves'

Python turtle circle function

So I'm reading a book to learn python and I got to a part about the module turtle.
So after explaining it, it gives you some exercises.
One of them is to define a function that creates regular polygons.
I got this to work.
import turtle
bob = turtle.Turtle()
def polygon(t, l, n):
angle = 360/n
for i in range(n):
t.fd(l)
t.lt(angle)
polygon(bob, 40, 5)
For example this draws a regular pentagon.
The next exercise asks you to draw a "circle" changing the number of sides of the polygon.
The problem is that sometimes it doesn't work and the polygon/circle doesn't close.
I tried to find the solution by changing lots of time both the lenght and the number of sides or only one of the two but I didn't succeed.
For example, lenght = 10 and n°sides = 140 doesn't work, instead lenght = 20 and n°sides = 120 works.
Can someone explain, please?
Found solution.
Being a beginner I forgot about integers and floats.
That's why the "circle" wasn't closing.
Your code works fine in Python 3 but didn't close the polygon in Python 2 due to the difference in how division works. The fix is to simply use 360.0 instead of 360 and then it works fine in both:
from turtle import Turtle, Screen
def polygon(t, l, n):
angle = 360.0 / n
for _ in range(n):
t.fd(l)
t.lt(angle)
bob = Turtle()
polygon(bob, 10, 140)
screen = Screen()
screen.exitonclick()
Python turtle's own circle() method actually draws polygons with the default assumption that 60 sides is sufficient to look like a circle on the screen. Unless the circle is very small (then it uses fewer sides) or the user insists on more (or less) sides via the steps argument.
Try putting 360.0 instead of 360, because the initial value of Python is in integers.
We want to convert it into decimals, that's why we put the .0 after the 360.

How would I make this code into a fractal?

I have a beginning star. Now, how would I make this into a fractal?
import turtle
turing = turtle.Turtle()
for i in range(5):
turing.forward(110)
turing.left(216)
A fractal is something that repeats with some variation. So put your star-loop code into a loop and repeat it several times. Change something after doing each star-loop. You could change where the turtle is, or what angle it is pointing at, or how long the side of the next star will be, or any or all of these.
Following #mgkrebbs general philosophy, with a simple fractal that deflects a line, we can make all the lines of the deflection smaller duplicates of the fractal. Your star is tricky to work with but since it has verticies, we can recursively put smaller stars at each vertex:
from turtle import Turtle, Screen
def star(turtle, length, depth):
turtle.left(90)
for _ in range(5):
turtle.forward(length)
heading = turtle.heading()
if depth > 1:
star(turtle, length / 2, depth - 1)
turtle.setheading(heading)
turtle.left(216)
turing = Turtle()
turing.speed("fastest")
star(turing, 180, 3)
turing.hideturtle()
screen = Screen()
screen.exitonclick()
As the depth increases, you can see the stars start to overlap -- making the image larger by increasing length, or making the recursions a smaller fraction of the length, may help.
OUTPUT

Categories