Light refraction

ddanbe 3 Tallied Votes 2K Views Share

This is my first try with turtle graphics in Python.
It is in fact a sort of "translation" from some old LOGO code.
When you run it, this should be the result:
dbf0a4f76975c5cc4dee5df30139f144
Insiders can perhaps play the "Dark side of the moon" record by Pink Floyd :)
Any comments (good or bad) on my code are more than welcome.

''' 
Refraction of sunlight through a prism
Python 3.3
28.05.2014 Danny Marivoet

'''
import turtle as tu

TOPLEFT = (-470,350)
#left = (-470,0)
OFFSET = 60
INDIGO = '#4B0082'

def start():    
    tu.hideturtle()
    tu.speed('fastest')
    tu.bgcolor('black')
    tu.pencolor('lightblue')
    tu.penup()
    tu.goto(TOPLEFT)
    tu.write('Refraction of light through a prism.', True, align='left',font=('Ariel',24,'normal'))
    tu.goto(0,0)

def draw_prism(x,y,side,color,pensize):
    tu.goto(x,y)
    tu.pendown()
    tu.pencolor(color)
    tu.pensize(pensize)
    tu.backward(side/2)
    tu.left(60)
    tu.forward(side)
    tu.right(120)
    tu.forward(side)
    tu.right(120)
    tu.forward(side/2)

def draw_light_beam(x,y,length):  
    tu.penup()
    tu.goto(x-length,y+OFFSET)
    tu.pendown()   
    tu.pencolor('white')
    tu.pensize(15)
    tu.goto(x,y+OFFSET)
    #tu.right(180)
    #tu.forward(length)

def draw_refraction(color,x,tox,y,toy):
    tu.penup()
    tu.goto(x,y+OFFSET)
    tu.pendown()
    tu.pencolor(color)
    tu.pensize(15)
    tu.goto(tox,toy)

start()
draw_prism(0,-50,200,"white",2)
tu.speed('slowest')
draw_light_beam(0,0,400)
draw_refraction('red',0,400,0,-80)
draw_refraction('orange',0,400,0,-105)
draw_refraction('yellow',0,400,0,-130)
draw_refraction('green',0,400,0,-155)
draw_refraction('mediumblue',0,400,0,-180)
draw_refraction('navy',0,400,0,-205)
draw_refraction(INDIGO,0,400,0,-230)
tu.done()
TrustyTony 888 pyMod Team Colleague Featured Poster

The writing style is clear and crisp, but I would prefer turtle graphics code to be more sparing of gotos, even they are not program gotos. Usually you do routine so that is relative to turtle direction and maybe returns to starting position. Then you could just loop colors, turn fixed amount and change the pen color and draw fixed shape.

ddanbe 2,724 Professional Procrastinator Featured Poster

Thanks for your comment. Yes, I know I could have used more "turtle style" I will try to do that in the future. What do you mean by "grisp"?

TrustyTony 888 pyMod Team Colleague Featured Poster

Sorry my ghastly spelling, hope you understand now after edit. You are quite clear and traditional with all caps 'constants'. You managed to keep to Python style naming, x and y are quite traditional and understandable albeit short in their meaning. Of course the turtle is normally more concerned only going forward and turning left or right.

Here some editing of your code, noticed also that length parameter was unused:

''' 
Refraction of sunlight through a prism
Python 3.3
28.05.2014 Danny Marivoet

'''
import turtle as tu

TOPLEFT = (-470,350)
OFFSET = 60
INDIGO = '#4B0082'

def start():    
    tu.hideturtle()
    tu.speed('fastest')
    tu.bgcolor('black')
    tu.pencolor('lightblue')
    tu.penup()
    tu.goto(TOPLEFT)
    tu.write('Refraction of light through a prism.', True, align='left',font=('Ariel',24,'normal'))
    tu.goto(0,0)

def draw_prism(side,color,pensize):
    tu.pendown()
    tu.pencolor(color)
    tu.pensize(pensize)
    tu.backward(side/2)
    tu.left(60)
    tu.forward(side)
    tu.right(120)
    tu.forward(side)
    tu.right(120)
    tu.forward(side/2)

def draw_light_beam(x,y):  
    tu.pendown()   
    tu.pencolor('white')
    tu.pensize(15)
    tu.goto(x,y+OFFSET)

def draw_refraction(tox,toy):
    tu.pendown()
    tu.pensize(15)
    tu.goto(tox,toy)

start()
draw_prism(200,"white",2)
tu.speed('slowest')
tu.penup()
tu.goto(-400, OFFSET)
draw_light_beam(0,0)
end = -80
for color in ('red', 'orange', 'yellow', 'green', 'mediumblue','navy', INDIGO):
    tu.penup()
    tu.goto(0, OFFSET)
    tu.pencolor(color)
    draw_refraction(400+OFFSET,end+OFFSET)
    end -= 25

tu.done()
ddanbe commented: This is great help, thanks! +15
ddanbe 2,724 Professional Procrastinator Featured Poster

I must admit I was struggling a bit with how to put all my 'refraction beams' into a loop. Great response Tony! Thanks!

ZZucker 342 Practically a Master Poster

pytony, red is a nice color

ddanbe commented: Keen eye! +15
TrustyTony 888 pyMod Team Colleague Featured Poster

Added.

TrustyTony 888 pyMod Team Colleague Featured Poster

Still working on this or is this solved?

ddanbe 2,724 Professional Procrastinator Featured Poster

Perhaps I'd better posted it as a normal thread, so I could mark this solved.
I don't always find the time to work with Python, but I love it as a language. :) And Tony, I very much appreciated your contribution!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.