Skip to content

Instantly share code, notes, and snippets.

View oliverbooth's full-sized avatar
🔷
Reticulating splines

Oliver Booth oliverbooth

🔷
Reticulating splines
View GitHub Profile
@oliverbooth
oliverbooth / dow
Created October 16, 2012 11:09
Day of Week from Year/Month/Day
dow(y, m d) {
t = { 0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4 };
y -= m < 3;
return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
}
@oliverbooth
oliverbooth / make-me-a-sandwich.c
Last active March 28, 2023 14:36
Make Me A Sandwich
// Written by Oliver Booth
// Based on an XKCD comic
// http://xkcd.com/149/
#include <stdio.h>
#include <stdbool.h>
#include <unistd.h>
int main() {
return printf(getuid() ? "What? Make it yourself.\n" : "Okay.\n");
@oliverbooth
oliverbooth / locationfromzone.php
Created June 10, 2013 16:45
San Andreas Zones & Coordinates
function locationfromzone($lx, $ly) {
$gSAZones = array(
array("The Big Ear", array(-410.00,1403.30,-3.00,-137.90,1681.20,200.00)),
array("Aldea Malvada", array(-1372.10,2498.50,0.00,-1277.50,2615.30,200.00)),
array("Angel Pine", array(-2324.90,-2584.20,-6.10,-1964.20,-2212.10,200.00)),
array("Arco del Oeste", array(-901.10,2221.80,0.00,-592.00,2571.90,200.00)),
array("Avispa Country Club", array(-2646.40,-355.40,0.00,-2270.00,-222.50,200.00)),
array("Avispa Country Club", array(-2831.80,-430.20,-6.10,-2646.40,-222.50,200.00)),
array("Avispa Country Club", array(-2361.50,-417.10,0.00,-2270.00,-355.40,200.00)),
array("Avispa Country Club", array(-2667.80,-302.10,-28.80,-2646.40,-262.30,71.10)),
@oliverbooth
oliverbooth / xor.c
Last active December 29, 2015 15:59
Logical XOR in C
#define XOR(a,b) ((!a&&b)||(a&&!b))
@oliverbooth
oliverbooth / rangerand.cpp
Last active January 4, 2016 05:59
Ranged random numbers
#include <stdlib.h>
#include <time.h>
void initrand() {
srand((unsigned) time(NULL));
rand();
}
int rangerand(int max, int min) {
if(max < min) max = min;
@oliverbooth
oliverbooth / happy-birthday.c
Last active August 29, 2015 13:56
Happy Birthday!
#include <stdio.h>
int main(int argc, char *argv[]) {
if(argc == 1)
printf("Whose birthday is it?\n");
else if (argc == 2)
for(int i = 1; i <= 4; i++)
printf("Happy birthday %s %s\n", i % 3 == 0 ? "dear" : "to", i % 3 == 0 ? argv[1] : "you");
else
printf("Too many.\n");
@oliverbooth
oliverbooth / FloatObject.cs
Last active October 9, 2021 21:58
Floating-like objects in Unity
using UnityEngine;
/// <summary>
/// Represents a behavior script which will cause the object to float following a sine wave pattern.
/// </summary>
public class FloatObject : MonoBehaviour
{
[SerializeField] private float _amplitude = 0.25f;
[SerializeField] private float _frequency = 1f;
private float _t = 0f;
@oliverbooth
oliverbooth / math.c
Created July 9, 2014 21:39
Even checker
bool is_even(int i) {
char buf[10]; // Probably big enough
float f = (float)i;
f /= 2;
sprintf(buf, "%lf", f);
for (int ii = 0; ii < 10 - 1; ++ii) {
if (buf[ii] == '.' && buf[ii+1] == '5') {
return true;
}
}
@oliverbooth
oliverbooth / randomFile.php
Last active August 29, 2015 14:06
Randomly choose a file
<?php
/**
* Randomly chooses a file from the given directory.
* @param directory Optional. The directory to search. Defaults to the current directory.
* @param ext Optional. An array of accepted file extensions to match. Defaults to standard images.
* @return A string representing the name of the file
*/
function randomFile($directory = ".", $ext = array("jpg", "jpeg", "gif", "png", "bmp")) {
// Find file matches using glob
$files = glob($directory."/*.{".implode(",",$ext)."}", GLOB_BRACE);
@oliverbooth
oliverbooth / swap.c
Created October 19, 2014 14:50
Integer swap in C
#define SWAP(x,y) do{ x^=y; y^=x; x^=y; } while (0);