JSFiddle - React, Tailwind, and code Playground
by YouTingKuo
JavaScript
function FindCoefficient (p1,q1,p2,q2) { //依給的點與Maxima結果求出方程式
var x1 = q2-q1;
var x2 = p1-p2;
var x3 = p2*q1-p1*q2;
console.log (x1 + ", " + x2 + ", " + x3);
return [x1,x2,x3];
}
function Point (p1,q1,p2,q2,a1,b1,a2,b2) {
//前4個輸入(2個點)為第一個方程式,後4個輸入(2個點)為第二個方程式,再依Maxima結果求交點
var e1 = FindCoefficient (p1,q1,p2,q2);
var e2 = FindCoefficient (a1,b1,a2,b2);
var use = e2[0]*e1[1]-e1[0]*e2[1];
var x = (e2[1]*e1[2]-e1[1]*e2[2])/use;
var y = -(e2[0]*e1[2]-e1[0]*e2[2])/use;
return [x,y];
}
var ans = Point (6,-5,4,-2,5,2,8,3);
console.log ('x = ' + ans[0] + '; ' + 'y = ' + ans[1]);