C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Using turtle in Python to draw six-pointed stars with different side lengths

By : Ashok Arjunan
Date : November 21 2020, 11:01 PM
it helps some times Hopefully I'll be able to explain this well. I'm currently using helper functions to draw a six-pointed star in the turtle graphics window of python. First, we had to create a function to draw a triangle. Here is my code: , Anywhere you use a unit that has a dimension:
code :
``````tess.fd(80)
tess.fd(120) # probably should be tess.fd(sz)
tess.fd(80)
``````
``````from turtle import Turtle, Screen

WIDTH_RATIO = 2 * 3**0.5 / 3  # ratio of widest point in star to edge of triangle

def triangle(size):
for i in range(3):
tess.fd(size)
tess.lt(120)

def sixPtdStar(size):
triangle(size)
tess.lt(30)
tess.pu()
tess.fd(size * WIDTH_RATIO)
tess.lt(150)
tess.pd()
triangle(size)

def rowOfStars(numInRow, size):
for i in range(numInRow):
sixPtdStar(size)
tess.pu()
tess.lt(90)
tess.fd(size * WIDTH_RATIO / 2)
tess.lt(90)
tess.pd()

def sqrOfRows(numInRow, size):
tess.pu()
halfSize = numInRow * size / 2
tess.goto(-halfSize, halfSize)  # center on screen
tess.pd()

for i in range(4):
rowOfStars(numInRow, size)
tess.rt(90)

screen = Screen()
tess = Turtle()
tess.speed("fastest")  # numbers > 10 are all equivalent, safer to use symbols

sqrOfRows(6, 36)

screen.exitonclick()
``````

Share :

## Drawing Stars with turtle in python

By : Soufiane
Date : March 29 2020, 07:55 AM
Any of those help I need to write a function that draws random stars based upon the question of how many stars the person wants. a=color,b=length and c,d are the starting coordinates. I am not really sure where I am going wrong any tips,hints or help would be appreciated. , To call a function, put parentheses after the function name:
code :
``````   a=random_color()
b=length()
c=x()
d=y()
``````
``````star('red',100,0,0)
``````
``````def length():
return randrange(5,71)

def x():
return randrange(-280,281)

def y():
return randrange(-200,201)
``````
``````   for i in range(z):
a=random_color()
b=length()
c=x()
d=y()
star(a,b,c,d)
``````
``````def star(color, side_length, x, y):
print(color, side_length, x, y)
turtle.color(color)
turtle.begin_fill()
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(5):
turtle.forward(side_length)
turtle.right(144)
turtle.forward(side_length)
turtle.end_fill()
``````
``````import turtle
import random

def star(color, side_length, x, y):
print(color, side_length, x, y)
turtle.color(color)
turtle.begin_fill()
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for i in range(5):
turtle.forward(side_length)
turtle.right(144)
turtle.forward(side_length)
turtle.end_fill()

def random_color():
randvar = randrange(0, 5)
if randvar == 0:
return ('red')
elif randvar == 1:
return ('blue')
elif randvar == 2:
return ('green')
elif randvar == 3:
return ('yellow')
else:
return ('black')

def length():
return random.randrange(5, 71)

def xcoord():
return random.randrange(-280, 281)

def ycoord():
return random.randrange(-200, 201)

def night_sky():
z = int(input('How many stars do you want?'))
for i in range(z):
color = random_color()
side_length = length()
x = xcoord()
y = ycoord()
star(color, side_length, x, y)

night_sky()
``````

## How to draw sine in Python turtle?

By : Maya Technology
Date : March 29 2020, 07:55 AM
I wish this help you In the text book, the code can draw sin(x). , Work out the y-coordinates of the points that are being drawn:
code :
``````50 * sin((-175/100) * 2π)
50 * sin((-174/100) * 2π)
50 * sin((-173/100) * 2π)
...
50 * sin((175/100) * 2π)
50 * sin((176/100) * 2π)
``````

## Draw a square in Python Turtle

By : Emir Bečić
Date : March 29 2020, 07:55 AM
I hope this helps . I am following a tutorial for Python learning and I can't get the screen to open to draw. I don't get an error, it just shows that the program finish running. Maybe I missed something, can someone point it out? , Your primary error is these two lines are in the wrong order:
code :
``````window.exitonclick() #click the screen to close it
draw_square()
``````
``````import turtle

# to draw a square, or eventually a turtle, you need to do the things below

def draw_square():
""" draw square for turtles """

# to draw a square you want to : move forward, turn right,
#  move forward, turn right,move forward turn right

brad.forward(100)  # forward takes a number which is the distance to move

window = turtle.Screen()
# this is the background where the turtle will move
window.bgcolor("red") # the color of the window

draw_square()

window.exitonclick()  # click the screen to close it
``````

## How can I create five pointed stars in canvas using function?

By : Mattr
Date : March 29 2020, 07:55 AM
I wish this help you Make a loop of the number of points in the shape. In the case of a 5 pointed star that is 10 points because you have the outer points to make the spikes and the inner points which come back into the shape.
Inside the loop you find each consecutive point (x, y coordinate) to draw a line to e.g. using ctx.lineTo(x, y);.
code :
``````  const x = xOffset+Math.sin(angle)*radius;
``````
``````for(let i = 0; i < 10; i ++){ ... }
``````
``````const canv = document.getElementById('c');
const ctx = canv.getContext('2d');
const w = window.innerWidth;
const h = window.innerHeight
canv.width = w;
canv.height = h;
ctx.clearRect(0,0,w,h);
//offset x,y, size, spokes, rotation 0 to 1.
function star(ox, oy, s = 10, n = 5, r = 0.25){
ctx.beginPath();

for(let i=0;i<n*2;i++){
let rotation = Math.PI*2*r;
let angle = (i/(n*2))*Math.PI*2+rotation;
let dist = s*(i%2)+s;
let x = ox+Math.cos(angle)*dist;
let y = oy+Math.sin(angle)*dist;
if(i === 0) {
ctx.moveTo(x, y);
continue; //skip
}
ctx.lineTo(x, y);
}
ctx.closePath();
}

ctx.fillStyle = 'magenta';
star(w/2, h/2, 10, 4, 0.125);
ctx.fill();

ctx.fillStyle = 'red';
star(w*0.2, h*0.2, 5, 5);
ctx.fill();

ctx.fillStyle = 'orange';
star(w*0.33, h*0.33, 20, 5);
ctx.fill();

ctx.fillStyle = 'yellow';
star(w*0.66, h*0.25, 30, 6);
ctx.fill();

ctx.fillStyle = 'green';
star(w*0.2, h*0.75, 40, 7);
ctx.fill();

ctx.fillStyle = 'cyan';
star(w*0.82, h*0.8, 50, 8, 1/16);
ctx.fill();``````
``````canvas {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: hidden;
background-color: black;
}``````
``<canvas id="c"></canvas>``