Skip to content

Instantly share code, notes, and snippets.

@nigelbrady
nigelbrady / PointInPolygon.cs
Created April 11, 2019 20:17
Detect if a point is within a polygon
/* Taken from StackOverflow:
https://stackoverflow.com/questions/217578/how-can-i-determine-whether-a-2d-point-is-within-a-polygon */
public bool IsPointInPolygon( Point p, Point[] polygon )
{
double minX = polygon[ 0 ].X;
double maxX = polygon[ 0 ].X;
double minY = polygon[ 0 ].Y;
double maxY = polygon[ 0 ].Y;
for ( int i = 1 ; i < polygon.Length ; i++ )
@nigelbrady
nigelbrady / ImageRotator.cs
Created October 12, 2018 19:30
Rotate A Texture2D by an arbitrary angle
// From 'BeauWorlds' on Unity Answers:
// https://answers.unity.com/questions/685656/rotate-an-image-by-modifying-texture2dgetpixels32.html
using System;
public class ImageRotator {
public static Texture2D RotateImage(Texture2D originTexture, int angle){
Texture2D result;
result = new Texture2D(originTexture.width, originTexture.height);
Color32[] pix1 = result.GetPixels32();
Color32[] pix2 = originTexture.GetPixels32();
int W = originTexture.width;
@nigelbrady
nigelbrady / detectStopTyping.js
Created August 13, 2018 20:01
Wait for User to Stop Typing, Using JavaScript
//From: https://schier.co/blog/2014/12/08/wait-for-user-to-stop-typing-using-javascript.html
// Get the input box
var textInput = document.getElementById('test-input');
// Init a timeout variable to be used below
var timeout = null;
// Listen for keystroke events
textInput.onkeyup = function (e) {
@nigelbrady
nigelbrady / Shuffle.cs
Last active May 23, 2018 04:47
Shuffle x,y coordinates with LINQ
var coords = Enumerable.Range(0, width)
.SelectMany(x => Enumerable.Range(0, height)
.Select(y => new Vector2(x, y)))
.OrderBy(x => random.Next());
foreach (var c in coords)
{
var i = (int)c.x;
var j = (int)c.y;
//blah blah blah
@nigelbrady
nigelbrady / queue.lua
Created February 23, 2018 22:38
Double-ended Queue in Lua
-- Adapted from Programming in Lua (first edition)
-- by Roberto Ierusalimschy
-- Lua.org, December 2003
-- ISBN 8590379817
Queue={}
function Queue:new()
local o = {first=0, last=-1, size=0}
o.keys={}
@nigelbrady
nigelbrady / CollisionWatcher.cs
Created February 22, 2018 07:30
.NET Event-based CollisionWatcher for Unity
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace CommonTimeGames
{
public class CollisionWatcher : MonoBehaviour
{
public delegate void CollisionEventHandler(Collision collision);
@nigelbrady
nigelbrady / CameraFader.cs
Created January 23, 2018 07:03
Fade a Unity Camera in or out
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace CommonTimeGames.NekoMaze.UI
{
public class CameraFader : MonoBehaviour
{
public Color FadeOutColor = Color.black;
@nigelbrady
nigelbrady / Dockerfile
Created October 4, 2017 08:50
ASP.NET Core Dockerfile
FROM microsoft/aspnetcore-build:2.0 AS build-env
WORKDIR /app
# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore
# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out
@nigelbrady
nigelbrady / CharacterCamera.cs
Created August 25, 2017 03:27
3rd Person Follow Camera
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharacterCamera : MonoBehaviour {
public GameObject Character;
//0, 1, -4
//15 0 0
@nigelbrady
nigelbrady / WeightedRandom.cs
Created August 23, 2017 04:21
Weighted Random Selection (C#)
public static T Random<T>(this IEnumerable<T> enumerable, Func<T, int> weightFunc)
{
int totalWeight = 0; // this stores sum of weights of all elements before current
T selected = default(T); // currently selected element
foreach (var data in enumerable)
{
int weight = weightFunc(data); // weight of current element
int r = Random.Next(totalWeight + weight); // random value
if (r >= totalWeight) // probability of this is weight/(totalWeight+weight)
selected = data; // it is the probability of discarding last selected element and selecting current one instead