Created
June 20, 2015 04:40
-
-
Save calebmadrigal/81f3b9de14f54ac355f7 to your computer and use it in GitHub Desktop.
Drawing individual pixels with Tkinter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import math | |
from tkinter import Tk, Canvas, PhotoImage, mainloop | |
width = 1000 | |
height = 600 | |
window = Tk() | |
canvas = Canvas(window, width=width, height=height, bg="#000000") | |
canvas.pack() | |
img = PhotoImage(width=width, height=height) | |
canvas.create_image((width//2, height//2), image=img, state="normal") | |
def center_and_invert(y, height): | |
return int(height/2 - y) | |
def f(x): | |
num_cycles = 4 | |
amplitude = 200 | |
return amplitude * math.sin(2 * math.pi * (num_cycles / width) * x) | |
def graph(f, x_range, height): | |
for x in x_range: | |
y = center_and_invert(f(x), height) | |
img.put("#ffffff", (x, y)) | |
graph(f, range(width), height) | |
mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just a remark, If you want to get faster your drawing,
replace :
img.put("#ffffff", (x, y))
by
canvas.create_rectangle(x,y,x,y,outline="white")
To see the difference make set width = 1000