Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more functionality in Vector Calculator #874

Merged
merged 7 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Calculators/Vector-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ <h1>Vector Calculator</h1>
X:<input type="text" id="bx">
Y:<input type="text" id="by">
</div>
<div class="operation">
<!-- Added dropdown for selecting operation -->
<label for="operation">Operation:</label>
<select id="operation">
<option value="add">Add</option>
<option value="subtract">Subtract</option>
<option value="dot">Dot Product</option>
</select>
</div>
<button id="calc" onmouseup="calc();">Calculate</button>
<div id="results"></div>

Expand Down
40 changes: 30 additions & 10 deletions Calculators/Vector-Calculator/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<div id="vectC" style="display:inline;"> \ <div id="vectCchild" style="display:inline; -moz-user-select: none; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -ms-user-select: none; user-select: none;"> \ Vector C = \ </div> \ <' + cx + ", " + cy + '></div><br> \
<div id="vectCDot" style="display:inline;"> \ <div id="vectCDotchild" style="display:inline; -moz-user-select: none; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -ms-user-select: none; user-select: none;"> \ Vector C Dot = \ </div> \ <' + cxdot + ", " + cydot + '></div><br> \
<div id="angle" style="display:inline;"> \ <div id="angleChild" style="display:inline; -moz-user-select: none; -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: none; -ms-user-select: none; user-select: none;"> \ Angle = \ </div> \ ' + angle + '</div>';
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 + "<br>" + (angleResult ? angleResult : "");

}
25 changes: 18 additions & 7 deletions Calculators/Vector-Calculator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -65,4 +59,21 @@ input {

.vect>p {
font-weight: bold;
}
}

.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;
}