Skip to content

Instantly share code, notes, and snippets.

@isedgar
isedgar / tic-tac-toe.csv
Last active February 23, 2024 20:02
TL: top-left, TM: top-middle, TR: top-right, ML: middle-left, MM: middle-middle, MR: middle-right, BL: bottom-left, BM: bottom-middle, BR: bottom-right, class: optimal square
TL TM TR ML MM MR BL BM BR class
X O - - - - - - - BL
X O O - - - X - - ML
X O - O - - X - - BR
X O O O - - X - X MM
X O - O O - X - X BM
X O - O - O X - X MM
X O - O - - X O X MM
X O - - O - X - - ML
X O - - - O X - - ML
@isedgar
isedgar / toe.py
Created February 23, 2024 19:57
Unbeatable tic-tac-toe one-vs-one classifier (assuming the machine always starts in the upper left corner).
import numpy as np
from sklearn.preprocessing import normalize
from sklearn.multiclass import OneVsOneClassifier as ovo
from sklearn.linear_model import Perceptron
X = np.array([[-1,0,0,0,0,0,0,0],[-1,-1,0,0,0,1,0,0],[-1,0,-1,0,0,1,0,0],[-1,-1,-1,0,0,1,0,1],[-1,0,-1,-1,0,1,0,1],[-1,0,-1,0,-1,1,0,1],[-1,0,-1,0,0,1,-1,1],[-1,0,0,-1,0,1,0,0],[-1,0,0,0,-1,1,0,0],[-1,0,0,0,0,1,-1,0],[-1,0,0,0,0,1,0,-1],[0,-1,0,0,0,0,0,0],[-1,-1,0,0,0,1,0,0],[0,-1,-1,0,0,1,0,0],[-1,-1,-1,0,0,1,0,1],[0,-1,-1,-1,0,1,0,1],[0,-1,-1,0,-1,1,0,1],[0,-1,-1,0,0,1,-1,1],[0,-1,0,-1,0,1,0,0],[0,-1,0,0,-1,1,0,0],[0,-1,0,0,0,1,-1,0],[0,-1,0,0,0,1,0,-1],[0,0,-1,0,0,0,0,0],[-1,1,-1,0,0,0,0,0],[-1,1,-1,1,-1,0,0,0],[-1,1,-1,1,0,-1,0,0],[-1,1,-1,1,0,0,-1,0],[-1,1,-1,1,0,0,0,-1],[0,1,-1,-1,0,0,0,0],[0,1,-1,0,-1,0,0,0],[0,1,-1,0,0,-1,0,0],[0,1,-1,0,0,0,-1,0],[0,1,-1,0,0,0,0,-1],[0,0,0,-1,0,0,0,0],[1,-1,0,-1,0,0,0,0],[1,-1,-1,-1,0,1,0,0],[1,-1,-1,-1,1,1,-1,0],[1,-1,-1,-1,1,1,0,-1],[1,-1,0,-1,-1,1,0,0],[1,-1,0,-1,0,1,-1,0],[1,-1,0,-1,0,1,0,-1],[1,0,-1,-
@isedgar
isedgar / perceptron.py
Created February 23, 2024 19:54
An example of the perceptron algorithm
samples = [[1,2], [2,0], [3,1], [2,2]]
labels = [1,1,-1,-1]
def dot(a, b):
return sum([a[j]*b[j] for j in range(len(a))])
w, b = [0,0], 0
for n in range(100):
n_updates = 0
@isedgar
isedgar / voronoi.js
Created September 18, 2023 21:09
Naive code to create 2D Voronoi cells (Cartesian plane)
// Naive code to create 2D Voronoi cells (Cartesian plane)
var voronoi = {
compute: function(sites, pad){ // pad: box padding
var boundary = this.points_boundary(sites);
var x_left = boundary[0] - pad; // min x
var y_bottom = boundary[1] - pad; // min y
var x_right = boundary[2] + pad; // max x
var y_top = boundary[3] + pad; // max y
@isedgar
isedgar / voronoi-short.md
Last active September 18, 2023 21:05
Naive pseudocode to create Voronoi cells (short version).

Voronoi diagram in the Cartesian plane

INPUT:

S := {(x0, y0), (x1, y1), ..., (xn-1, yn-1)}

pad := padding (some number)


ALGORITHM:

xl := minimum x-coordinate in S (left)

@isedgar
isedgar / voronoi-long.md
Last active September 18, 2023 21:07
Naive pseudocode to create Voronoi cells (long version).

Voronoi diagram in the Cartesian plane

INPUT:

sites := {(x0, y0), (x1, y1), ..., (xn-1, yn-1)}

pad := padding (some number)


ALGORITHM:

n := number of sites

def ray_casting(point, polygon):
intersections = 0
x, y = point
for k in range(len(polygon)-1):
x1, y1 = polygon[k]
x2, y2 = polygon[k+1]
if (y < y1) != (y < y2) and\
x < (x2-x1)*(y-y1)/(y2-y1)+x1:
var app={
angle: function(a,b,c){ // angle at point b
var v1=[b[0] - a[0], b[1] - a[1]],
v2=[b[0] - c[0], b[1] - c[1]];
return Math.acos(this.inner(v1, v2) / (this.norm(v1) * this.norm(v2)));
},
is_clockwise: function(a,b,c){
return ((b[0]-a[0]) * (c[1]-a[1]) - (c[0]-a[0]) * (b[1]-a[1])) < 0 ? true : false;
},
@isedgar
isedgar / inner-angle.js
Created July 2, 2021 03:17
Get the inner angle at point "b" in degrees
function inner_angle(a,b,c){ // inner angle at point b in degrees
var v1=[a[0] - b[0], a[1] - b[1]],
v2=[c[0] - b[0], c[1] - b[1]];
return Math.acos(math.inner(v1, v2) / (math.norm(v1) * math.norm(v2))) * 180 / Math.PI;
}
var math={
norm: function(v){
var s=0, n=v.length;
var polygon={
area: function(points){
var sum=0, n=points.length;
if(points[0][0] != points[n-1][0] || points[0][1] != points[n-1][1]){
sum += (points[n-1][0]*points[0][1] - points[0][0]*points[n-1][1]);
}
for(var i=0; i < n-1; ++i){
sum += (points[i][0]*points[i+1][1] - points[i+1][0]*points[i][1]);