solution_117_6.py

#!/usr/bin/python3
# ====================================================================
# plot curve #6
# ====================================================================

import numpy as np
import graphics as gr
import draw_xy_axes as ax

# --------------------------------------------------------------------
# ---- curve #6
# --------------------------------------------------------------------

def curve06(x):
    y = (np.sin(np.pi*x)/(np.pi*x))**2
    return y

# --------------------------------------------------------------------
# ---- range function for floats
# --------------------------------------------------------------------

def frange(start,end,inc):
    while start < end:
        yield start
        start += inc

# --------------------------------------------------------------------
# ---- main
# --------------------------------------------------------------------

if __name__ == '__main__':

    # ---- draw a point (small circle) using window coordinates

    def draw_point(x,y,color='red',size=4):
        p = gr.Circle(gr.Point(x,y),size)
        p.setFill(color)
        p.setOutline('black')
        p.draw(win)
        return p

    # ---- setup graphics window

    win = gr.GraphWin('Curve #6',801,801)
    win.setBackground("white")

    # ---- draw X,Y axes

    ax.draw_xy_axes(win)

    # ---- plot curve #6
    # ---- X scale factor   200 offset 400
    # ---- Y scale factor  -200 offset 400

    for x in frange(-2.0,2.0,0.1):
        y = curve06(x)
        print(f'x={x:>6.2f}  y={y:>6.2f}  (raw values)')
        x = 400 + (x * 200)
        y = 400 + (y * -200)

        draw_point(x,y)

    # ---- pause program (click in window to continue)

    click = win.getMouse()
    win.close()