Skip to content

Commit

Permalink
Added initial height option in Projectile Motion Calculator (#1941)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabhhsinghh authored Dec 27, 2024
1 parent 8618740 commit c1ef7b5
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Calculators/Projectile-Motion-Calculator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ Projectile Motion Calculator calculates the Maximum height, Range, Time of fligh

## Screenshots :-

![image](https://github.com/Rakesh9100/CalcDiverse/assets/73993775/2433e29d-cd24-41f2-ae87-01f2c04622fa)
![image](https://github.com/user-attachments/assets/c4c25b48-f513-4a96-8306-c21edfb4a4e4)
14 changes: 7 additions & 7 deletions Calculators/Projectile-Motion-Calculator/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css" />
<link rel="stylesheet" href="style.css">
<title>Projectile Motion Calculator</title>
</head>

<body>
<h1>Projectile Motion Calculator</h1>
<div class="container">

<h3>Initial Velocity(m/s): </h3>
<input type="number" id="initial-velocity" />
<h3>Launch Angle(degrees):</h3>
<input type="number" id="launch-angle" />
<h3>Initial Velocity (m/s):</h3>
<input type="number" id="initial-velocity">
<h3>Launch Angle (degrees):</h3>
<input type="number" id="launch-angle">
<h3>Initial Height (m):</h3>
<input type="number" id="initial-height">
<br><br>
<button id="btn">Generate Result</button>
<h3>Result:</h3>
Expand All @@ -24,7 +25,6 @@ <h3>Result:</h3>
<li id="msg2">Range:</li>
<li id="msg3">Time of Flight:</li>
</ul>

</div>
<script src="script.js"></script>
</body>
Expand Down
53 changes: 29 additions & 24 deletions Calculators/Projectile-Motion-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,44 @@

let initialVelocity = document.querySelector("#initial-velocity");
let launchAngle = document.querySelector("#launch-angle");
let initialHeight = document.querySelector("#initial-height");
let btn = document.querySelector("#btn");
let msg1 = document.querySelector("#msg1");
let msg2 = document.querySelector("#msg2");
let msg3 = document.querySelector("#msg3");
const g = 9.8;

btn.addEventListener("click", () => {
if (initialVelocity.value == "" || launchAngle.value == "") {
alert("Enter a valid input");

if (initialVelocity.value === "" || launchAngle.value === "" || initialHeight.value === "") {
alert("Enter all valid inputs");
} else {
calculateProjectile();
}
cal_pro();

});

const cal_pro = () => {
let max_height = (((initialVelocity.value) ** 2) * (Math.sin((Math.PI / 180) * launchAngle.value) ** 2)) / (2 * g);
console.log(max_height)
const calculateProjectile = () => {
let velocity = parseFloat(initialVelocity.value);
let angle = parseFloat(launchAngle.value);
let height = parseFloat(initialHeight.value);

let range = (((initialVelocity.value) ** 2) * Math.sin((Math.PI / 180) * 2 * launchAngle.value)) / g
console.log(range)
// Convert angle to radians
let angleRad = (Math.PI / 180) * angle;

let timeOfflight = ((2 * (initialVelocity.value)) * Math.sin((Math.PI / 180) * launchAngle.value)) / g
console.log(timeOfflight)
// Max height formula
let max_height = (velocity ** 2 * Math.sin(angleRad) ** 2) / (2 * g) + height;

if (initialVelocity.value == "" || launchAngle.value == "") {
msg1.innerHTML = `<li>Max Height:-</li>`
msg2.innerHTML = `<li>Range:-</li>`
msg3.innerHTML = `<li>Time of Flight:-</li>`
}
else {
msg1.innerHTML = `<li>Max Height:${max_height}m</li>`
msg2.innerHTML = `<li>Range:${range}m</li>`
msg3.innerHTML = `<li>Time of Flight:${timeOfflight}s</li>`
}
}
// Time to reach max height
let timeToPeak = (velocity * Math.sin(angleRad)) / g;

// Total flight time (up and down)
let totalFlightTime =
timeToPeak +
Math.sqrt((2 * (height + (velocity ** 2 * Math.sin(angleRad) ** 2) / (2 * g))) / g);

// Range formula
let range = velocity * Math.cos(angleRad) * totalFlightTime;

// Update results
msg1.innerHTML = `<li>Max Height: ${max_height.toFixed(2)} m</li>`;
msg2.innerHTML = `<li>Range: ${range.toFixed(2)} m</li>`;
msg3.innerHTML = `<li>Time of Flight: ${totalFlightTime.toFixed(2)} s</li>`;
};
9 changes: 7 additions & 2 deletions Calculators/Projectile-Motion-Calculator/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

* {
font-family: 'Bitter', serif;

}

body {
Expand All @@ -16,6 +15,7 @@ body {
left: 25%;
border-radius: 20px;
background-color: white;
padding: 20px;
}

h1 {
Expand All @@ -27,6 +27,7 @@ h1 {
h3 {
display: flex;
justify-content: center;
margin: 10px 0;
}

button {
Expand All @@ -38,6 +39,8 @@ button {
border: none;
border-radius: 10px;
width: 50%;
padding: 10px;
margin: 20px 0;
}

button:hover {
Expand All @@ -50,14 +53,16 @@ input {
left: 25%;
font-size: 1.5rem;
width: 50%;
padding: 5px;
margin: 10px 0;
}

ul {
position: relative;
left: 30%;
}

@media screen and (max-width:1024px) {
@media screen and (max-width: 1024px) {
input {
position: relative;
left: 15%;
Expand Down
2 changes: 1 addition & 1 deletion calculators.json
Original file line number Diff line number Diff line change
Expand Up @@ -1849,7 +1849,7 @@
},
{
"title": "Projectile Motion Calculator",
"description": "Calculates Max Height, Range, Time Of Flight of Projectile.",
"description": "Calculates max height, range, and time of flight of the projectile.",
"link": "./Calculators/Projectile-Motion-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Projectile-Motion-Calculator"
},
Expand Down

0 comments on commit c1ef7b5

Please sign in to comment.