function Point (x, y) {
this.x = x || 0;
this.y = y || 0;
}
function Line (p1, p2, size, color) {
this.p1 = p1;
this.p2 = p2;
this.size = size || 1;
this.color = color || 'black';
this.a = 0;
this.b = 0;
// 使用在繪圖用對應的 forever
this.instance = createSprite('dot.jpg');
this.instance.hidden = true;
// y = ax + b
// a:斜率 b:常數
this.update = function () {
// 計算斜率
this.a = (this.p1.y - this.p2.y) / (this.p1.x - this.p2.x);
// 這招真的很無恥,斜率 1000 畫面上分辨不出是否垂直線
// 在計算交會點的時候就很方便,不用再判斷是否為垂直線
if (Math.abs(this.a) > 1000) this.a = 1000;
// 計算常數項
this.b = this.p1.y - this.p1.x*this.a;
}
this.update(); // 初始化更新
// 取得兩線斷相交的座標,如果沒有相交就回傳 undefined
this.touched = function (line) {
if (!this.isCrossWith(line)) return; // undefined
let x = (line.b - this.b)/(this.a - line.a);
let y = this.a*x + this.b;
return {x: x, y: y};
}