diff --git a/Calculators/Vector-Calculator/index.html b/Calculators/Vector-Calculator/index.html index 9d6222038..a4c389d88 100644 --- a/Calculators/Vector-Calculator/index.html +++ b/Calculators/Vector-Calculator/index.html @@ -23,6 +23,15 @@

Vector Calculator

X: Y: +
+ + + +
diff --git a/Calculators/Vector-Calculator/script.js b/Calculators/Vector-Calculator/script.js index d1f6e57e1..5dadf4210 100644 --- a/Calculators/Vector-Calculator/script.js +++ b/Calculators/Vector-Calculator/script.js @@ -9,18 +9,38 @@ function calc() { ay = parseInt(document.getElementById("ay").value); bx = parseInt(document.getElementById("bx").value); by = parseInt(document.getElementById("by").value); + + const operation = document.getElementById("operation").value; + if (isNaN(ay) || isNaN(bx) || isNaN(by)) { alert("Please enter proper values for x and y"); return; } - var cx = ax + bx; - var cy = ay + by; - var cxdot = ax * bx; - var cydot = ay * by; - var angle = radToDeg(Math.atan(cy / cx)); - // A + B, angel = tan^-1(y/x) - var easyAnswerHighlight = '
\
\ Vector C = \
\ <' + cx + ", " + cy + '>

\ -
\
\ Vector C Dot = \
\ <' + cxdot + ", " + cydot + '>

\ -
\
\ Angle = \
\ ' + angle + '
'; - document.getElementById("results").innerHTML = easyAnswerHighlight; + let result, angleResult; + switch (operation) { + case "add": + const cx_add = ax + bx; + const cy_add = ay + by; + result = `Vector C Add = <${cx_add}, ${cy_add}>`; + break; + case "subtract": + const cx_subtract = ax - bx; + const cy_subtract = ay - by; + result = `Vector C Subtract = <${cx_subtract}, ${cy_subtract}>`; + break; + case "dot": + const dotProduct = ax * bx + ay * by; + const magnitudeA = Math.sqrt(ax * ax + ay * ay); + const magnitudeB = Math.sqrt(bx * bx + by * by); + const cosineTheta = dotProduct / (magnitudeA * magnitudeB); + const angleRadians = Math.acos(cosineTheta); + const angleDegrees = radToDeg(angleRadians); + result = `Vector C Dot = ${dotProduct}`; + angleResult = `Angle between vectors = ${angleDegrees} degrees`; + break; + default: + result = "Invalid operation"; + } + document.getElementById("results").innerHTML = result + "
" + (angleResult ? angleResult : ""); + } \ No newline at end of file diff --git a/Calculators/Vector-Calculator/style.css b/Calculators/Vector-Calculator/style.css index 6ee653dd3..306163b33 100644 --- a/Calculators/Vector-Calculator/style.css +++ b/Calculators/Vector-Calculator/style.css @@ -50,12 +50,6 @@ input { line-height: 25px; } -/*#results:hover{ - -webkit-transform: scale(1.2); - -moz-transform: scale(1.2); - -o-transform: scale(1.2); - transform: scale(1.2); -}*/ .vect { display: block; font-size: 24px; @@ -65,4 +59,21 @@ input { .vect>p { font-weight: bold; -} \ No newline at end of file +} + +.operation { + margin: 35px 0; +} + +.operation label { + color: #C5EBFE; + font-size: 24px; +} + +.operation select { + border: 0; + border-radius: 5px; + font-size: 16px; + padding: 10px; + margin: 10px; +}