Skip to content

Instantly share code, notes, and snippets.

@SheepTester
Last active October 15, 2017 04:23
Show Gist options
  • Save SheepTester/9945b4a49fad9ce059ccab7d56761b90 to your computer and use it in GitHub Desktop.
Save SheepTester/9945b4a49fad9ce059ccab7d56761b90 to your computer and use it in GitHub Desktop.
school math calculations

Math/School stuff

A01
B1000
C1010
D100
E0
F0010
G110
H0000
I00
J0111
K101
L0100
M11
N10
O111
P0110
Q1101
R010
S000
T1
U001
V0001
W011
X1001
Y1011
Z1100
101111
200111
300011
400001
500000
610000
711000
811100
911110
011111
.010101
,110011
?001100
/10010
@011010
'011110
!101011
(10110
)101101
&01000
:111000
;101010
=10001
+01010
-100001
_001101
"010010
$000100
00000000
var primesto=n=>{var p=[],i,j;for(i=2;i<n;i++)if((_=>{for(j=0;j<p.length;j++)if(!(i%p[j]))return 0;return 1;})())p.push(i);return p;};
class Point {
constructor(x,y) {
this.x=x;
this.y=y;
}
}
class Line {
constructor(point1,point2) {
this.pt1=point1;
this.pt2=point2;
this.slope=(this.pt1.y-this.pt2.y)/(this.pt1.x-this.pt2.x);
if (this.slope===-Infinity) this.slope=Infinity;
}
contains(point) {
if (this.slope===Infinity) {
return this.pt1.x=point.x;
} else {
return this.pt1.y-point.y===this.slope*(this.pt1.x-point.x);
}
}
intersect(line) {
if (this.slope===line.slope) return null;
if (this.slope===Infinity) {
return new Point(this.pt1.x,line.yAt(this.pt1.x));
} else if (line.slope===Infinity) {
return new Point(line.pt1.x,this.yAt(line.pt1.x));
} else {
var x=(this.slope*this.pt1.x-line.slope*line.pt1.x+line.pt1.y-this.pt1.y)/(this.slope-line.slope);
return new Point(Math.round(x*10000)/10000,Math.round((this.slope*(x-this.pt1.x)+this.pt1.y)*10000)/10000);
}
}
xAt(y) {
if (this.slope===0) return NaN;
else return (y-this.pt1.y)*this.slope+this.pt1.x;
}
yAt(x) {
if (this.slope===Infinity) return NaN;
else return this.slope*(x-this.pt1.x)+this.pt1.y;
}
}
class Rectangle {
constructor(point1,point2) {
this.minpt=new Point(Math.min(point1.x,point2.x),Math.min(point1.y,point2.y));
this.maxpt=new Point(Math.max(point1.x,point2.x),Math.max(point1.y,point2.y));
this.top=new Line(new Point(this.minpt.x,this.minpt.y),new Point(this.maxpt.x,this.minpt.y));
this.left=new Line(new Point(this.minpt.x,this.minpt.y),new Point(this.minpt.x,this.maxpt.y));
this.right=new Line(new Point(this.maxpt.x,this.minpt.y),new Point(this.maxpt.x,this.maxpt.y));
this.bottom=new Line(new Point(this.minpt.x,this.maxpt.y),new Point(this.maxpt.x,this.maxpt.y));
}
contains(point) {
return point.x>=this.minpt.x&&point.x<=this.maxpt.x&&point.y>=this.minpt.y&&point.y<=this.maxpt.y;
}
clip(line) {
if (this.contains(line.pt1)&&this.contains(line.pt2)) return line;
if (!this.contains(line.pt1)&&!this.contains(line.pt2)) return null;
if (line.slope===Infinity) {
if (!this.contains(new Point(line.pt1.x,this.minpt.y))) return null;
} else if (line.slope===0) {
if (!this.contains(new Point(this.minpt.x,line.pt1.y))) return null;
} else if (!this.contains(line.intersect(this.top))&&!this.contains(line.intersect(this.left))&&!this.contains(line.intersect(this.right))) return null;
var pt1=line.pt1.y<line.pt2.y?line.pt1:line.pt2,
pt2=line.pt1.y<line.pt2.y?line.pt2:line.pt1;
if (!this.contains(pt1)) {
pt1=line.intersect(this.top);
if (!pt1||!this.contains(pt1)) pt1=line.intersect(line.slope<=0?this.right:this.left);
};
if (!this.contains(pt2)) {
pt2=line.intersect(this.bottom);
if (!pt2||!this.contains(pt2)) pt2=line.intersect(line.slope<=0?this.left:this.right);
};
return new Line(pt1,pt2);
}
}
var aos=(a,b)=>-b/(2*a),
vertex=(a,b,c)=>a*aos(a,b)**2+b*aos(a,b)+c,
xints=(a,b,c)=>[(-b+Math.sqrt(b**2-4*a*c))/(2*a),(-b-Math.sqrt(b**2-4*a*c))/(2*a)],
y=(x,a,b,c)=>a*x**2+b*x+c;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment