Python Turtle Workshop: Complete Bipartite Graph

Following is a sample code for drawing a complete bipartite graph using HSL color for the gradient change. First we import turtle module and declare some constant values:

'from CodeGuruAcademy import commlib as lib'
import turtle as t
import colorsys
SCREEN_WIDTH = 1000
SCREEN_HEIGHT = 800
POS_CNT = 12
HUE_HEAD = 0
HUE_TAIL = 1

Then we prepare the screen and define a jump function.
screen = t.Screen()
screen.setup(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0)
t.bgcolor(0,0,0)

def jump(pos):
t.pu()
t.goto(pos[0], pos[1])
t.pd()

t.ht()
t.tracer(0)

Then we prepare the left and right hand side dot positions:
'prepare the left/right-side dot position'
pos_left = [0] * POS_CNT
pos_right = [0] * POS_CNT
colors = [0] * POS_CNT

for n in range(POS_CNT):
y = - SCREEN_HEIGHT * 0.4 + n * (SCREEN_HEIGHT-200)/POS_CNT
pos_left[n] = (- SCREEN_WIDTH / 3, y)
pos_right[n] = ( SCREEN_WIDTH / 3, y)
hue = HUE_HEAD + (HUE_TAIL-HUE_HEAD) * n/POS_CNT
colors[n] = colorsys.hls_to_rgb(hue, 0.5, 1)

'''lib.paint_bgcolor(t, 1200, (81, 197, 255), (31, 79, 117))
lib.init_screen(t, "Complete Bipartite Graph", "11/09/20",
SCREEN_WIDTH, SCREEN_HEIGHT, (249,249,0), (0,0,0))
'''

Finally we can draw the lines using imbedded for loops:
t.pensize(1)
t.color(1, 1, 0)
t.colormode(1.0)

for lpos, color in zip(pos_left, colors):
jump(lpos)
t.color(color)
t.dot(10)
for rpos in pos_right:
t.goto(rpos)
t.dot(10)
jump(lpos)

t.done()

The result for this sample code is shown below:

a