<jupyter_start><jupyter_text>Go from for loops and while loops to functions> **The main idea behind this section**. Reusing instructions (programmatic operations and instructions, storing instruction on files(scripts), and reusing functions)> **How to run this section?** Build up the need of using multipline commands, to source files, to functions (Demonstrate by example what reusability means in this section, this is where functions come into place)Reflect on the notion of objectsIn order to work with python built in IDLE do `py -3 -m idlelib`  Excercise, drawing polygons with turtleThis excercise also aims to show the<jupyter_code>import turtle
from polygon import sort_points, get_angle
## Explain differences
# from turtle import * 

## Explain how to use the help()
# help(turtle)

## Excercise: 
# Do this first manually
# turtle.shape("turtle")
# turtle.forward(100)
# turtle.setheading(90)
# turtle.forward(100)
# turtle.setheading(180)
# turtle.forward(100)
# turtle.setheading(270)
# turtle.forward(100)

## Do it then with a for loop
## Explain the need for a foor loop
# length = 100
# directions = [0,90,180,270]
# for dir in directions:
#     turtle.setheading(dir)
#     turtle.forward(length)

## Example of a function
## triangle = [[a_side, C_angle], [b_side, A_angle], [c_side, B_angle]]
def draw_triangle(triangle):
    for entry in triangle:
        print(entry[0])
        turtle.forward(entry[0])
        turtle.left(entry[1])



## Excercise: 
## Building a polygon with a better data model
def draw_polygon(coordinates):
    '''
    By doing myself this excercise I figured out problems to solve
    like for instance the if statement at the end of the function
    '''
    angle_list = []
    for point in coordinates:
        angle_list.append(get_angle(point))

    print(angle_list)

    turtle.penup()
    count = 0
    for coord in coordinates:
        count += 1 
        turtle.goto(coord[0], coord[1])
        turtle.pendown()
        # turtle.circle(5)
        # turtle.penup()
        if(count == len(coordinates)):
            turtle.goto(coordinates[0][0], coordinates[0][1])


def draw_better(coordinates):
    coordinates = sort_points(coordinates)
    draw_polygon(coordinates)

## Another triangle data model
## A polygon model
##   Latitude,Longitude
##   [[51.984338,4.136685],
##   [51.852919,5.290831],
##   [52.861383,5.024489]]
draw_coordinates()
polygon = [[300,-150],[20,30],[100,200],[-50,180], [100,80]]
## Explain this example first and why is it so bad
# draw_triangle([[100,100],[264,19],[300,60]])
# draw_triangle([[100,100],[264,180-19],[300,60]])

## Explain these example and the data model and why is better.
# draw_polygon([[20,30],[100,200],[-50,180],[300,-150]])
draw_polygon(polygon)
# print(polygon)
# print(sort_points(polygon))
# draw_polygon([[-50,180],[100,200],[300,-150],[20,30]])

turtle.exitonclick()

import turtle
def draw_coordinates():
    turtle.goto(0,300)
    turtle.goto(0,-300)
    turtle.penup()
    turtle.goto(0,0)
    turtle.pendown()
    turtle.goto(300,0)
    turtle.goto(-300,0)
import turtle
from polygon import *
turtle.speed(2)
def draw_polygon(coordinates):

    # To properly draw the polygon we need to center the centroid
    # Into the turtle coordinate system
    center = centroid(coordinates)
    for idx, point in enumerate(coordinates):
        coordinates[idx][0] = coordinates[idx][0] - center[0]
        coordinates[idx][1] = coordinates[idx][1] - center[1]

    coordinates = sort_points(coordinates)
    print(list_ordered_angles(coordinates))
    print(sort_points(coordinates))

    turtle.penup()
    count = 0
    for coord in coordinates:
        count += 1 
        turtle.goto(coord[0], coord[1])
        turtle.pendown()
        
        # Close the polygon by going back to the first point in the list
        if(count == len(coordinates)):
            turtle.goto(coordinates[0][0], coordinates[0][1])
    
draw_coordinates()

## Examples of polygons
# polygon = [[-50,180],[20,30],[100,200],[300,-150], [400,100]] # Working
# polygon = [[50,10],[30,20],[10,50], [40,60]] # Working
# polygon = [[-50,180],[20,30],[100,200],[300,-150], [400,100],[300,300]] # Working
polygon = [[-50,180],[20,30],[100,200],[300,-150], [400,100],[300,300],[100,30],[-400,-200],[70,55], [30,40],[12,13]]
# polygon = [[-100,80],[200,150],[140,-30],[-60,-80]]
# polygon = [[300,-150],[20,30],[100,200],[-50,180], [100,80]]



draw_polygon(polygon)
turtle.exitonclick()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15129971117529437, 'fraction_numerical': 0.08975783159297934, 'mean_word_length': 4.174712643678161, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '548479', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1481, 'n_words': 382, 'path': 'Instructor_notes/Day_2/old/03_lesson.ipynb', 'repo_name': 'TU-Delft-DCC/Intro-to-Python-forGIS', 'size': '4501'}