Skip to content

Instantly share code, notes, and snippets.

View benpm's full-sized avatar
🕶️
pushin pixels

Benjamin Mastripolito benpm

🕶️
pushin pixels
View GitHub Profile
@benpm
benpm / spherical_y.py
Last active January 29, 2023 09:03
Spherical coordinates for Y-up coordinate system
def spherical_y_up(theta, phi):
return (
sin(pi/2 - theta) * sin(phi),
cos(pi/2 - theta),
sin(pi/2 - theta) * cos(phi)
)
@benpm
benpm / lab10.py
Created November 4, 2022 16:23
comp1020 lab 10
class Sprite:
def __init__(self, image):
self.image = image
self.rect = image.get_rect()
def draw(self, screen):
screen.blit(self.image, self.rect)
def set_pos(self, pos):
@benpm
benpm / sum_seq.py
Created January 2, 2022 19:52
Contiguous enumeration of fixed-length sequences of integers which sum to a given value
# n: sequence length
# S: the sequence
# s: the integer value which each sequence sums to
# The computed index, starts at 0
v = 0
# algorithm: Consider each element of the sequence S as a subsequence, where each element is an increasingly smaller subsequence. By summing together the indices of each subsequence, we get the index of the whole sequence. We start with the longest subsequence.
for i in range(n - 1):
# l: Max index of subsequence that sums to n minus whatever we've seen so far
@benpm
benpm / example.cpp
Last active November 19, 2021 23:30
C++ Simple Heterogenous Memory Pool
#include <memory>
#include <vector>
#include <spdlog/fmt/fmt.h>
#include "pool.hpp"
struct A {
int x = 0;
uint64_t arr[16];
A(int x) {
@benpm
benpm / transformMap.cpp
Last active March 24, 2025 15:09
C++ Invert or Transform Map at Compile Time
/*
Transform one unordered map to another, useful for inverting maps at compile time
See it run here: https://replit.com/@_bm/transformmap
*/
#include <iostream>
#include <functional>
#include <unordered_map>
//Function which can transform an std::unordered_map of one type to another with a given transform function
@benpm
benpm / .vimrc
Created March 4, 2018 18:14
Useful Vim Keymaps
" C and S write the buffer
nnoremap cs :update<cr>
" Shift + C and S writes and quits
nnoremap CS :wq<cr>
" F7 fixes indention automagically! Thanks @haydn-jones
map <F7> mzgg=G`z
" J and K quits insert mode
@benpm
benpm / loop.js
Created January 16, 2018 19:09
Radiating Diamond Loop
for (var i = 0; i < 16; i++) {
for (var ii = 0; ii < i + 1; ii++) {
console.log(x - i + ii, y + ii);
console.log(x + ii, y - i + ii);
console.log(x - i + ii, y - ii);
console.log(x + ii, y + i - ii);
}
}
@benpm
benpm / lex.py
Created January 13, 2018 01:31
Quick Binary Lexicographic Ordering
def lex(i, n):
return (i >> n) & 1;
@benpm
benpm / chessmath.js
Created October 4, 2017 05:44
A couple "chess" math functions.
//Returns the chess distance between x and y
function chessDistance(x1, y1, x2, y2) {
return Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
}
//Returns the chess 'direction' between x and y
function chessDirection(x1, y1, x2, y2) {
return [Math.sign(x2 - x1), Math.sign(y2 - y1)];
}
@benpm
benpm / runlength.js
Created July 2, 2017 22:39
Basic Run-Length Encoding in Javascript
var run = {
encode: function (data) {
var result = new Uint16Array(data.length * 2 + 1);
let ii = 1;
result[0] = data.length;
result[ii] = data[0];
for (let i = 0; i < data.byteLength; i++) {
if (result[ii] != data[i]) {
ii += 2;
result[ii] = data[i];