Skip to content

Instantly share code, notes, and snippets.

@SeijiEmery
SeijiEmery / gist:639d48fded11bcf9279a
Last active November 28, 2023 18:06
From-scratch PI approximation (based on Archimedes' method using inscribed regular hexagons)
def approx_pi (iterations):
''' pi approximation based on Archimedes' geometric method (find the
perimeter of a regular hexagon subdivided n times, and use that
as an approximation to C = 2*pi*r, with r = 1.0).
Note: Becomes increasingly inaccurate when iterations > 15 due to
the limitations of Python's floating point numbers. Would have to
implement bignums w/ an extremely accurate sqrt fcn to get better
precision.
@SeijiEmery
SeijiEmery / arithmetic-derivative.cpp
Last active January 5, 2017 00:27
Arithmetic Derivative Problem (Project Euler)
#include "primes.hpp"
#include <vector>
#include <iostream>
// #include <sstream>
#include <cstdio>
// #include <thread>
// #include <future>
// #include <queue>
#include <algorithm>
@SeijiEmery
SeijiEmery / example.cpp
Last active January 5, 2017 00:27
Entity Example
typedef int64_t entity_id;
entity_id newUUID () {
entity_id id = 0;
return ++id;
}
enum class EntityType {
Empty,
@SeijiEmery
SeijiEmery / stack.hpp
Last active August 29, 2015 14:08
Stack implementation
// Copyright (c) 2014, Seiji Emery
// All rights reserved.
#ifndef __STACK_HPP__
#define __STACK_HPP__
#include <algorithm>
#include <cassert>
// Quick guide to some C++11 features and good programming practices
// Constructors
// Suppose we have a class like this, and want to add constructors to it
class MyClass {
public:
// public members and methods...
@SeijiEmery
SeijiEmery / avl_tree.hpp
Last active January 5, 2017 00:27
AVL Tree
//
// avl_tree2.hpp
// AVLTree
//
// Created by Seiji Emery on 11/18/14.
// Copyright (c) 2014 Seiji Emery. All rights reserved.
//
#ifndef AVLTree_avl_tree2_hpp
#define AVLTree_avl_tree2_hpp
@SeijiEmery
SeijiEmery / polymorphism_example.hpp
Created November 14, 2014 03:23
On polymorphism...
// Kind of a boring example, but suppose you're writing a vector graphics renderer and need to
// both store and render several types of shapes including circles, rectangles, and polygons
// made up of multiple lines. These all have different type signatures (a circle is not a box),
// so to store them as one type you'll have to use polymorphism.
// There's two ways to do this in C++: via OOP, or using the older, procedural C-style.
// The object-oriented approach:
@SeijiEmery
SeijiEmery / ncubegen.cpp
Last active August 29, 2015 14:09
Generate vertices/edges for a n-dimensional cube
// Generate n-dimensional cube vertex/edges (opengl)
void genVerts (int dimensions, float scale = 1.0f) {
assert(dimensions > 1 && dimensions <= 4);
auto posVal = scale * 0.5f;
auto negVal = -posVal;
@SeijiEmery
SeijiEmery / quadtree.hpp
Last active August 29, 2015 14:10
Quadtree (wip)
// Copyright (c) 2014 Seiji Emery. All rights reserved.
#pragma once
#include "aabb.hpp"
#include "vec2.hpp"
// Quadtree impl.
// Note: This implementation relies on bulk insertion (list of objects is known ahead of time);
@SeijiEmery
SeijiEmery / vec2.hpp
Last active August 29, 2015 14:10
Vec2 impl
#ifndef __math2d_Vec2_h__
#define __math2d_Vec2_h__
#include <cmath>
#include <ostream>
#define DEFN_IMM_VECTOR_OP(op) \
friend Vec2 operator op (const Vec2 &a, const Vec2 &b) { \
return { a.x op b.x, a.y op b.y }; \
}