0
我正在嘗試創建一個腳本,該腳本可以找到給定三個頂點的三角形的質心(並顯示工作)。我創建了這個,但是出來的答案是隨機的或「NaN」。請幫助調試。JavaScript調試三角形的質心
var question = prompt("What do you want to know?")
if (question === "centroid of triangle") {
var Ax = prompt("What is the x value of point A on the triangle")
var Ay = prompt("What is the y value of point A on the triangle")
var Bx = prompt("What is the x value of point B on the triangle")
var By = prompt("What is the y value of point B on the triangle")
var Cx = prompt("What is the x value of point C on the triangle")
var Cy = prompt("What is the y value of point C on the triangle")
var MidpointABx = Ax + Bx/2
var MidpointABy = Ay + By/2
var SlopeDC = (MidpointABy - Cy)/(MidpointABx - Cx)
var InterceptDC = Cy - (SlopeDC * Cx)
var MidpointACx = Ax + Cx/2
var MidpointACy = Ay + Cy/2
var SlopeEB = (MidpointACy - By)/(MidpointACx - Bx)
var InterceptEB = By - (SlopeEB * Bx)
console.log("A: (" + Ax + "," + Ay + ")")
console.log("B: (" + Bx + "," + By + ")")
console.log("C: (" + Cx + "," + Cy + ")")
console.log("Midpoint of AB (Point D): (" + Ax + "+" + Bx + "/2 , " + Ay + "+" + By + "/2)")
console.log("Midpoint of AB (Point D): (" + MidpointABx + "," + MidpointABy + ")")
console.log("Slope of DC: " + MidpointABy + "-" + Cy + "/" + MidpointABx + "-" + Cx)
console.log("Slope of DC: " + SlopeDC)
console.log("y-intercept of DC: " + Cy + " = " + SlopeDC + "*" + Cx + "+b")
console.log("y-intercept of DC: " + InterceptDC)
console.log("Line DC: y = " + SlopeDC + " * x + " + InterceptDC)
console.log("Midpoint of AC (Point E): (" + Ax + "+" + Cx + "/2 , " + Ay + "+" + Cy + "/2)")
console.log("Midpoint of AC (Point E): (" + MidpointACx + "," + MidpointACy + ")")
console.log("Slope of EB: " + MidpointACy + "-" + By + "/" + MidpointACx + "-" +Bx)
console.log("Slope of EB: " + SlopeEB)
console.log("y-intercept of EB: " + By + " = " + SlopeEB + "*" + Bx + "+b")
console.log("y-intercept of EB: " + InterceptEB)
console.log("Line EB: y = " + SlopeEB + " * x + " + InterceptEB)
}所以你必須將其轉換成一個像這樣的整數
哪裏錯誤發生?我在Chrome,IE和Firefox上嘗試了這一點。它在所有三個都沒有NaN錯誤。 –