Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
cosinekitty / EarthRadius.js
Created December 5, 2019 01:52
Function for calculating Earth radius at a specific geodetic latitude.
function EarthRadiusInMeters(latitudeRadians)
{
// latitudeRadians is geodetic, i.e. that reported by GPS.
// http://en.wikipedia.org/wiki/Earth_radius
var a = 6378137.0; // equatorial radius in meters
var b = 6356752.3; // polar radius in meters
var cos = Math.cos(latitudeRadians);
var sin = Math.sin(latitudeRadians);
var t1 = a * a * cos;
var t2 = b * b * sin;
@cosinekitty
cosinekitty / LocationToPoint.js
Created December 5, 2019 02:06
Converting GPS coordinates to Cartesian coordinates and normal vector.
function LocationToPoint(c)
{
// Convert (lat, lon, elv) to (x, y, z).
var lat = c.lat * Math.PI / 180.0;
var lon = c.lon * Math.PI / 180.0;
var radius = EarthRadiusInMeters(lat);
var clat = GeocentricLatitude(lat);
var cosLon = Math.cos(lon);
var sinLon = Math.sin(lon);
@cosinekitty
cosinekitty / RotateGlobe.js
Created December 5, 2019 02:16
Rotate coordinate systems to facilitate calculating azimuth and altitude angles.
function RotateGlobe(b, a, bradius, aradius)
{
// Get modified coordinates of 'b' by rotating the globe so that 'a' is at lat=0, lon=0.
var br = {lat:b.lat, lon:(b.lon - a.lon), elv:b.elv};
var brp = LocationToPoint(br);
// Rotate brp cartesian coordinates around the z-axis by a.lon degrees,
// then around the y-axis by a.lat degrees.
// Though we are decreasing by a.lat degrees, as seen above the y-axis,
// this is a positive (counterclockwise) rotation (if B's longitude is east of A's).
#!/usr/bin/env python3
#
# naive.py - by Don Cross
#
# Illustrates why 4*arctan(1) is not a good way to calculate pi.
# Calculates the series 4*(1/1 - 1/3 + 1/5 - 1/7 + ...)
import math
n = 1
#!/usr/bin/env python3
#
# better.py - by Don Cross
#
# Test the formula pi = 4*(arctan(1/2) + arctan(1/3)).
#
import math
def ArctanDenom(d):
#!/usr/bin/env python3
#
# machin.py - by Don Cross
#
# Test Machin's Formula: pi = 4*(4*arctan(1/5) - arctan(1/239)).
#
import math
def ArctanDenom(d):
@cosinekitty
cosinekitty / picrunch.py
Created December 28, 2019 20:54
Python program to calculate pi to one million digits after the decimal point.
#!/usr/bin/env python3
#
# picrunch.py - by Don Cross
#
# Use Machin's Formula
# pi = 4*(4*arctan(1/5) - arctan(1/239))
# to calculate pi to one million places after the decimal.
#
import sys
@cosinekitty
cosinekitty / harmonograph.html
Created January 14, 2020 01:56
HTML code for the harmonograph simulator
<!DOCTYPE html>
<html>
<head>
<title>Harmonograph simulation by Don Cross</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="harmonograph.css">
</head>
<body>
<div><canvas id="GraphCanvas" class="HarmonographCanvas"></canvas></div>
<div>
@cosinekitty
cosinekitty / harmonograph.js
Created January 14, 2020 01:58
JavaScript code for the harmonograph simulator.
'use strict';
window.onload = function() {
function Radians(d) {
return d * (Math.PI / 180.0);
}
class Pendulum {
constructor(xamplitude, yamplitude, frequency, phase, halflife) {
this.xamplitude = xamplitude;
@cosinekitty
cosinekitty / harmonograph.css
Created January 14, 2020 02:15
CSS code for harmonograph simulator.
html, body {
/* Fit rendered page to screen layout */
top:0;
bottom:0;
left:0;
right:0;
position:fixed;
}
.HarmonographCanvas {