Skip to content

Instantly share code, notes, and snippets.

@bradmartin333
Created January 4, 2024 23:24
Show Gist options
  • Save bradmartin333/0d715b9a0c3aac33ce07a1d826cf8cbd to your computer and use it in GitHub Desktop.
Save bradmartin333/0d715b9a0c3aac33ce07a1d826cf8cbd to your computer and use it in GitHub Desktop.
scaled plotting in raylib
import random
import csv
def rim():
return random.randint(0, 2**32 - 1)
def write_to_csv(data, filename):
with open(filename, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerows(data)
data = [(rim(), rim(), rim()) for _ in range(10000)]
write_to_csv(data, "data.csv")
from pyray import *
from time import sleep
import threading
user_exit = False
width = 800
height = 600
data = []
scaling_factor = height / (2**32 - 1)
def scale(col):
return int(float(col) * scaling_factor)
def read_by_line():
with open("data.csv", "r") as file:
for line in file:
if user_exit:
return
if len(data) > width:
data.pop(0)
cols = line.split(",")
data.append((scale(cols[0]), scale(cols[1]), scale(cols[2])))
sleep(0.01)
init_window(width, height, "Hello")
threading.Thread(target=read_by_line).start()
while not window_should_close():
begin_drawing()
clear_background(WHITE)
if len(data) > 1:
for i in range(len(data) - 1):
draw_line(i, data[i][0], i + 1, data[i + 1][0], RED)
draw_line(i, data[i][1], i + 1, data[i + 1][1], GREEN)
draw_line(i, data[i][2], i + 1, data[i + 1][2], BLUE)
end_drawing()
close_window()
user_exit = True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment