sites := {(x0, y0), (x1, y1), ..., (xn-1, yn-1)}
pad := padding (some number)
n := number of sites
function ray_casting(point, polygon){ | |
var n=polygon.length, | |
is_in=false, | |
x=point[0], | |
y=point[1], | |
x1,x2,y1,y2; | |
for(var i=0; i < n-1; ++i){ | |
x1=polygon[i][0]; | |
x2=polygon[i+1][0]; |
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]); |
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 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; | |
}, |
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: |
// 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 |
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 |
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,- |