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 Ballistic Coefficient Calculator #1948

Merged
merged 12 commits into from
Dec 28, 2024
48 changes: 48 additions & 0 deletions Calculators/Ballistic-Coefficient-Calculator/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ballistic Coefficient Calculator</title>
<link rel="stylesheet" href="./style.css">
</head>
<body>
<div class="calculator">
<h1>Ballistic Coefficient Calculator</h1>
<div class="form-group">
<label for="mass">Mass (grams):</label>
<input type="number" id="mass" placeholder="Enter mass" onkeydown="moveToNextField(event, 'diameter')">
<div class="info">Mass affects the projectile's inertia. Higher mass typically increases the ballistic coefficient.</div>
</div>
<div class="form-group">
<label for="diameter">Diameter (mm):</label>
<input type="number" id="diameter" placeholder="Enter diameter" onkeydown="moveToNextField(event, 'drag-coefficient')">
<div class="info">Diameter is used to calculate the cross-sectional area, influencing air resistance.</div>
</div>
<div class="form-group">
<label for="drag-coefficient">Drag Coefficient (Cd):</label>
<input type="number" id="drag-coefficient" placeholder="Enter drag coefficient" onkeydown="moveToNextField(event, 'velocity')">
<div class="info">The drag coefficient quantifies how streamlined the projectile is. Lower Cd reduces air resistance.</div>
</div>
<div class="form-group">
<label for="velocity">Initial Velocity (m/s):</label>
<input type="number" id="velocity" placeholder="Enter initial velocity" onkeydown="moveToNextField(event, 'wind-speed')">
<div class="info">Initial velocity determines the speed of the projectile at launch, affecting its range and trajectory.</div>
</div>
<div class="form-group">
<label for="wind-speed">Wind Speed (m/s):</label>
<input type="number" id="wind-speed" placeholder="Enter wind speed" onkeydown="moveToNextField(event, 'angle')">
<div class="info">Wind speed influences the drift of the projectile during flight.</div>
</div>
<div class="form-group">
<label for="angle">Launch Angle (degrees):</label>
<input type="number" id="angle" placeholder="Enter launch angle">
<div class="info">The launch angle determines the trajectory and maximum range of the projectile.</div>
</div>
<button class="btn" onclick="calculateBallisticDetails()">Calculate</button>
<div id="result" class="result" style="display: none;"></div>
</div>

<script src="./script.js"></script>
</body>
</html>
51 changes: 51 additions & 0 deletions Calculators/Ballistic-Coefficient-Calculator/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
function calculateBallisticDetails() {
const mass = parseFloat(document.getElementById('mass').value);
const diameter = parseFloat(document.getElementById('diameter').value);
const dragCoefficient = parseFloat(document.getElementById('drag-coefficient').value);
const velocity = parseFloat(document.getElementById('velocity').value);
const windSpeed = parseFloat(document.getElementById('wind-speed').value);
const angle = parseFloat(document.getElementById('angle').value);

if (isNaN(mass) || isNaN(diameter) || isNaN(dragCoefficient) || isNaN(velocity) || isNaN(windSpeed) || isNaN(angle)) {
alert('Please enter valid numeric values.');
return;
}

// Convert mass to kilograms and diameter to meters
const massKg = mass / 1000;
const diameterM = diameter / 1000;

// Calculate the cross-sectional area
const area = Math.PI * Math.pow(diameterM / 2, 2);

// Calculate the ballistic coefficient
const ballisticCoefficient = massKg / (dragCoefficient * area);

// Calculate trajectory details
const gravity = 9.81; // Acceleration due to gravity (m/s^2)
const angleRad = (Math.PI / 180) * angle; // Convert angle to radians
const timeOfFlight = (2 * velocity * Math.sin(angleRad)) / gravity;
const maxHeight = Math.pow(velocity * Math.sin(angleRad), 2) / (2 * gravity);
const range = (Math.pow(velocity, 2) * Math.sin(2 * angleRad)) / gravity;
const windDrift = windSpeed * timeOfFlight;

// Display the result
const resultElement = document.getElementById('result');
resultElement.style.display = 'block';
resultElement.innerHTML = `
<p>Ballistic Coefficient: ${ballisticCoefficient.toFixed(4)} kg/m²</p>
<p>Time of Flight: ${timeOfFlight.toFixed(2)} s</p>
<p>Maximum Height: ${maxHeight.toFixed(2)} m</p>
<p>Range: ${range.toFixed(2)} m</p>
<p>Wind Drift: ${windDrift.toFixed(2)} m</p>
`;
}
function moveToNextField(event, nextFieldId) {
if (event.key === 'Enter') {
event.preventDefault(); // Prevent form submission on Enter
const nextField = document.getElementById(nextFieldId);
if (nextField) {
nextField.focus(); // Focus the next input field
}
}
}
100 changes: 100 additions & 0 deletions Calculators/Ballistic-Coefficient-Calculator/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #0d1117;
background-image:"./assets/background.png";
color:white;
}

.calculator {
background: #9198a1;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
max-width: 400px;
width: 100%;
}

.calculator h1 {
text-align: center;
margin-bottom: 20px;
font-size: 1.5rem;
}

.form-group {
margin-bottom: 15px;
position: relative;
}

.form-group label {
display: block;
margin-bottom: 5px;
font-weight: bold;
}

.form-group input {
width: 100%;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 1rem;
}

.form-group input:focus {
border-color: #007BFF;
outline: none;
}

.info {
display: none;
font-size: 0.9rem;
color: #555;
margin-top: 5px;
position: absolute;
/* top: -50%; */
left: 50%;
right: 0;
background: #f9f9f9;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
z-index: 10;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.form-group input:focus + .info {
display: block;
}

.btn {
width: 100%;
padding: 10px;
background-color: #007BFF;
color: #fff;
border: none;
border-radius: 5px;
font-size: 1rem;
cursor: pointer;
margin-top: 10px;
}

.btn:hover {
background-color: #0056b3;
}

.result {
margin-top: 20px;
padding: 10px;
background-color: #e9ecef;
color: black;
border-radius: 5px;
text-align: center;
font-size: 1.2rem;
font-weight: bold;
}
6 changes: 6 additions & 0 deletions calculators.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@
"link": "./Calculators/Averages-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Averages-Calculator"
},
{
"title": "Ballistic-Coefficient-Calculator",
"description": "Calculates the ballaistic coefficient and trajectory related details",
"link": "./Calculators/Ballistic-Coefficient-Calculator/index.html",
"source": "https://github.com/Rakesh9100/CalcDiverse/tree/main/Calculators/Ballistic-Coefficient-Calculator"
},
{
"title": "BMI Calculator",
"description": "Calculates the Body Mass Index of a person using Height & Weight.",
Expand Down
Loading