JavaScript调试三角形质心

时间:2016-01-15 01:17:39

标签: javascript node.js debugging

我正在尝试创建一个脚本,该脚本将在给定三个顶点的情况下找到三角形的质心(并显示工作)。我创建了这个,但是出来的答案是随机的或“NaN”。请帮忙调试。

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)

}

1 个答案:

答案 0 :(得分:0)

提示以字符串格式返回数据,因此您必须将其转换为如下整数:

req.user