-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added initial height option in Projectile Motion Calculator (#1941)
- Loading branch information
1 parent
8618740
commit c1ef7b5
Showing
5 changed files
with
45 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>`; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters