Skip to content

Instantly share code, notes, and snippets.

@cosinekitty
cosinekitty / circle.py
Created October 16, 2019 00:05
Calculate 360 points equally spaced around a unit circle.
#!/usr/bin/env python3
import math
angle = math.radians(1)
a = math.cos(angle)
b = math.sin(angle)
x = 1.0
y = 0.0
for n in range(361):
print('{:3d} {:11.8f} {:11.8f}'.format(n, x, y))
@cosinekitty
cosinekitty / almost_quine.c
Created November 7, 2019 00:10
Not quite a quine
#include <stdio.h>
int main()
{
printf("#include <stdio.h>\n");
printf("int main()\n");
printf("{\n");
printf(" return 0;\n");
printf("}\n");
return 0;
}
@cosinekitty
cosinekitty / almost_quine_2.c
Created November 7, 2019 00:19
Trying to printf a printf, but still not a quine yet.
#include <stdio.h>
int main()
{
printf("#include <stdio.h>\n");
printf("int main()\n");
printf("{\n");
printf(" printf(\"#include <stdio.h>\\n\");\n");
printf(" return 0;\n");
printf("}\n");
return 0;
@cosinekitty
cosinekitty / raw.c
Created November 7, 2019 01:54
A template for the quine
#include <stdio.h>
int main()
{
int i, k;
char s[] = $;
for(i=0;s[i]!=1;++i)printf("%c",s[i]);
for(k=0;s[k];++k)printf("%c%d", k ? ',' : '{', s[k]);
printf(",0}");
for(++i;s[i];++i)printf("%c",s[i]);
return 0;
@cosinekitty
cosinekitty / factory.py
Created November 7, 2019 02:03
Program to translate raw quine template into working quine source code.
#!/usr/bin/env python3
with open('raw.c', 'rt') as infile:
raw = infile.read()
data = [('1' if c=='$' else str(ord(c))) for c in raw]
delim = raw.index('$')
quine = raw[:delim] + '{' + ','.join(data) + ',0}' + raw[delim+1:]
with open('quine.c', 'wt') as outfile:
@cosinekitty
cosinekitty / quine.c
Created November 7, 2019 02:08
The working quine.
#include <stdio.h>
int main()
{
int i, k;
char s[] = {35,105,110,99,108,117,100,101,32,60,115,116,100,105,111,46,104,62,10,105,110,116,32,109,97,105,110,40,41,10,123,10,32,32,32,32,105,110,116,32,105,44,32,107,59,10,32,32,32,32,99,104,97,114,32,115,91,93,32,61,32,1,59,10,32,32,32,32,102,111,114,40,105,61,48,59,115,91,105,93,33,61,49,59,43,43,105,41,112,114,105,110,116,102,40,34,37,99,34,44,115,91,105,93,41,59,10,32,32,32,32,102,111,114,40,107,61,48,59,115,91,107,93,59,43,43,107,41,112,114,105,110,116,102,40,34,37,99,37,100,34,44,32,107,32,63,32,39,44,39,32,58,32,39,123,39,44,32,115,91,107,93,41,59,10,32,32,32,32,112,114,105,110,116,102,40,34,44,48,125,34,41,59,10,32,32,32,32,102,111,114,40,43,43,105,59,115,91,105,93,59,43,43,105,41,112,114,105,110,116,102,40,34,37,99,34,44,115,91,105,93,41,59,10,32,32,32,32,114,101,116,117,114,110,32,48,59,10,125,10,0};
for(i=0;s[i]!=1;++i)printf("%c",s[i]);
for(k=0;s[k];++k)printf("%c%d", k ? ',' : '{', s[k]);
printf(",0}");
for(++i;s[i];++i)printf("%
#!/usr/bin/env python3
#
# quadinterp.py - by Don Cross - https://github.com/cosinekitty
#
# Sample code for finding approximate roots of
# an expensive function f(t) using quadratic interpolation.
# This source code is public domain. Use at your own risk.
#
import math
Update(dt) {
var b, s;
// Calculate the force vectors acting on all the balls:
// both from springs and from gravity.
// Start out with just the gravitational force on each ball.
for (b of this.ballList) {
b.fx = 0.0;
b.fy = b.mass * this.gravity;
AddForce() {
// Calculate the length of the spring.
const dx = this.ball2.x - this.ball1.x;
const dy = this.ball2.y - this.ball1.y;
const len = Math.sqrt(dx*dx + dy*dy);
// The difference between the spring's rest length and its current length
// tells how much it is stretched or compressed.
// Multiply by the spring constant to get the magnitude of the force.
const displacement = len - this.restLength;
@cosinekitty
cosinekitty / GeocentriLatitude.js
Created December 5, 2019 01:44
Convert geodetic latitude to geocentric latitude using WGS 84
function GeocentricLatitude(lat)
{
// Convert geodetic latitude 'lat' to a geocentric latitude 'clat'.
// Geodetic latitude is the latitude as given by GPS.
// Geocentric latitude is the angle measured from center of Earth between a point and the equator.
// https://en.wikipedia.org/wiki/Latitude#Geocentric_latitude
var e2 = 0.00669437999014;
var clat = Math.atan((1.0 - e2) * Math.tan(lat));
return clat;
}