Skip to content

Instantly share code, notes, and snippets.

@root-talis
Created January 9, 2017 16:42
Show Gist options
  • Save root-talis/ed5d31c203b94f7a9807c105a591f394 to your computer and use it in GitHub Desktop.
Save root-talis/ed5d31c203b94f7a9807c105a591f394 to your computer and use it in GitHub Desktop.
Special relativity velocity calculator
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>GistRun</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="container-fluid" id="main">
<div class="page-header">
<h1>Velocity calculator</h1>
</div>
<div class="row">
<div class="col-sm-8">
<label>Calculation</label>
<div class="input-group">
<input id="v1" class="form-control" value="2000000"/>
<span class="input-group-addon">+</span>
<input id="v2" class="form-control" value="2000000"/>
<span class="input-group-addon">=</span>
<input id="v3" class="form-control" disabled/>
</div>
</div>
<div class="col-sm-4">
<label>Formula</label>
<select id="strategySelector" class="form-control">
<option value="galilean">Galilean</option>
<option value="specialRelativity">Special relativity</option>
</select>
</div>
</div>
<br >
<div class="alert alert-info">
<div class="row">
<div class="col-sm-4">
<strong>Speed of light <em>(c)</em></strong><br/>
299792458 m/s
</div>
<div class="col-sm-4">
<strong>Galilean formula</strong><br/>
v<sub>3</sub> = v<sub>1</sub> + v<sub>2</sub>
</div>
<div class="col-sm-4">
<strong>Special relativity formula</strong><br/>
v<sub>3</sub> = (v<sub>1</sub> + v<sub>2</sub>) / (1 + (v<sub>1</sub> &times; v<sub>2</sub>) / c<sup>2</sup>)
</div>
</div>
</div>
<hr/>
<div>
<a href="https://en.wikipedia.org/wiki/Velocity-addition_formula">Velocity-addition formula &ndash; Wikipedia</a>
<div class="pull-right">
<a href="https://gist.github.com/root-talis/ed5d31c203b94f7a9807c105a591f394">GitHub gist</a>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="script.js"></script>
</body>
</html>
var c = 299792458; // m/s
var additionStrategies = {
galilean: function (v1, v2) {
return v1 + v2;
},
specialRelativity: function(v1, v2) {
return (v1 + v2) / (1 + (v1 * v2) / (c*c));
}
}
function galileanAddition(v1, v2) {
return v1 + v2;
}
function galileanAddition(v1, v2) {
return v1 + v2;
}
$(function() {
var $form = $('#main');
var $v1 = $form.find('#v1');
var $v2 = $form.find('#v2');
var $v3 = $form.find('#v3');
var velocityAddition = null;
$form.find('input').off('change').on('change', function() {
var v1 = parseFloat($v1.val());
var v2 = parseFloat($v2.val());
var v3 = velocityAddition(v1, v2);
$v3.val(v3);
});
$form.find('#strategySelector').off('change').on('change', function() {
var activeStrategy = $(this).val();
console.log(activeStrategy);
velocityAddition = additionStrategies[activeStrategy];
$v1.trigger('change');
}).trigger('change');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment