class fraction {
n;
d;
s;
constructor(n, d) {
const g = this.gcd(Math.abs(n), Math.abs(d));
this.n = Math.abs(n / g);
this.d = Math.abs(d / g);
this.s = n * d < 0 ? -1 : 1;
}
add(f) {
const n = this.s * this.n * f.d + f.s * f.n * this.d;
const d = this.d * f.d;
return new fraction(n, d);
}
sub(f) {
const n = this.s * this.n * f.d - f.s * f.n * this.d;
const d = this.d * f.d;
return new fraction(n, d);
}
mul(f) {
const n = this.s * this.n * f.s * f.n;
const d = this.d * f.d;
return new fraction(n, d);
}
div(f) {
const n = this.s * this.n * f.d;
const d = this.d * f.s * f.n;
return new fraction(n, d);
}
gcd(a, b) {
return b === 0 ? a : this.gcd(b, a % b);
}
}
export function frac(f, mid=false) {
const pos = mid ? "+" : "";
const sgn = f.s == -1 ? "-" : pos;
if (f.d == 1) return `${sgn}${f.n}`;
return `${sgn}\\frac{${f.n}}{${f.d}}`;
}
function gcd(a ,b){
if(b == 0) return a;
return gcd(b, a % b);
}
function equation(ptA, ptB){
const [xA, yA] = ptA;
const [xB, yB] = ptB;
let dx = xB - xA;
let dy = yB - yA;
if(dx == 0) return `x=${xA}`;
if(dy == 0) return `y=${yA}`;
const a = new fraction(dy, dx);
const b = new fraction(dx * yA - dy * xA, dx)
return `${frac(a)}x${b.n === 0 ? "" : frac(b, true)}`;
}
console.log(equation([2, 2], [0, 0]))
To embed this program on your website, copy the following code and paste it into your website's HTML: