Skip to content

Instantly share code, notes, and snippets.

View omaraflak's full-sized avatar
👨‍💻
hey you

Omar Aflak omaraflak

👨‍💻
hey you
View GitHub Profile
@omaraflak
omaraflak / donut.py
Created August 2, 2021 08:36
Spinning Donut
import numpy as np
import pyglet
def draw_points(points, format="v2f"):
pyglet.graphics.draw(
len(points),
pyglet.gl.GL_POINTS,
(format, tuple([x for p in points for x in p]))
)
@omaraflak
omaraflak / game_of_life.py
Last active June 2, 2021 12:37
Game Of Life - Python Efficient Implementation
import numpy as np
from scipy import signal
from matplotlib import animation, pyplot as plt
def animate(i):
global board
neighbors = signal.correlate2d(board, kernel, mode="same")
board = ((board == 1) & ((neighbors == 2) | (neighbors == 3))) | ((board == 0) & (neighbors == 3))
image.set_data(board)
return image
@omaraflak
omaraflak / Makefile
Created February 16, 2021 21:32
Rotating 3D Cube - SDL2
all:
g++ main.cpp -o main -std=c++11 `sdl2-config --cflags --libs`
./main
@omaraflak
omaraflak / client.js
Last active February 11, 2021 15:43
Fast way to send arbitrarily large files using socket.io. The code will send the file chunks by chunks and never load the whole file into memory.
const client = require('socket.io-client')('http://localhost:5000')
const siof = require('./socket.io-file')
client.on('connect', () => {
console.log('connected to server')
client.once('ready', () => {
const metadata = { filename: 'file2.pdf' } // filename on the receiving end
siof(client).emit('file', 'filepath/to/file.pdf', metadata, (sent, total) => {
const percentage = (100 * sent / total).toFixed(2)
console.log(`${percentage}%`)
import numpy as np
import matplotlib.pyplot as plt
N = 100 # in how much sub pieces we should break a 1sec interval
T = 15 # total duration of the simulation
dt = 1 / N # dt
g = 9.81 # acceleration of gravity
L = 1 # pendulum rope length
k = 0.8 # air resistance coefficient
m = 1 # mass of the pendulum
@omaraflak
omaraflak / gmm.py
Last active October 18, 2020 13:47
Gaussian Mixture Model - From scratch
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
I = 300 # samples per class
N = 3 # features
J = 2 # clusters
mean_true = np.random.randint(-20, 20, (J, N))
variance_true = np.array([np.identity(N) * np.random.randint(1, 20, N) for j in range(J)])
import numpy as np
import matplotlib.pyplot as plt
def normalize(vector):
return vector / np.linalg.norm(vector)
def reflected(vector, axis):
return vector - 2 * np.dot(vector, axis) * axis
def sphere_intersect(center, radius, ray_origin, ray_direction):
# global variable along with width, height, etc.
max_depth = 3
# everything that follows is inside the double for loop
color = np.zeros((3))
reflection = 1
for k in range(max_depth):
nearest_object, min_distance = # ...
# ...
def reflected(vector, axis):
return vector - 2 * np.dot(vector, axis) * axis
# ...
if is_shadowed:
break
# RGB
illumination = np.zeros((3))
# ambiant
illumination += nearest_object['ambient'] * light['ambient']