Skip to content

Instantly share code, notes, and snippets.

@miyu
miyu / 1_Program.cs
Last active December 12, 2017 22:05
Clipper C# to TypeScript Transpilation (12/12/2017 1:51:48 PM); with 8 manual changes
using System;
using System.Collections.Generic;
namespace Clipper {
public class Program {
private static List<IntPoint> MakeRectangle(int left, int top, int right, int bottom) {
var contour = new List<IntPoint>();
contour.Add(new IntPoint(left, top));
contour.Add(new IntPoint(right, top));
contour.Add(new IntPoint(right, bottom));
public static IntVector2[] ConvexHull3(IntVector2 a, IntVector2 b, IntVector2 c) {
var abc = Clockness(a, b, c);
if (abc == Clk.Neither) {
var (s, t) = FindCollinearBounds(a, b, c);
return s == t ? new[] { s } : new[] { s, t };
}
if (abc == Clk.Clockwise) {
return new[] { c, b, a };
using System;
using System.Drawing;
using OpenMOBA;
using OpenMOBA.DevTool.Debugging;
using OpenMOBA.Geometry;
namespace QuadSegmentIntersection {
public class Program {
// assumes p is ccw ordered
public static bool SegmentInConvexPolygon(IntLineSegment2 s, IntVector2[] p) {
public class SectorSnapshotGeometryContext {
private const int kCrossoverAdditionalPathingDilation = 2;
private readonly SectorSnapshot _sectorSnapshot;
private readonly double _holeDilationRadius;
private readonly int _crossoverErosionRadius;
private readonly int _crossoverErosionDiameterSquared;
private readonly int _crossoverDilationFactor;
@miyu
miyu / accumulate.cs
Created April 24, 2017 07:21
Accumulate
IEnumerable<int> nums = Enumerable.Range(0, 10);
var sum = nums.Aggregate(
seed: 0,
func: (accumulator, val) => {
Console.WriteLine($"Acc: {accumulator}, Val: {val} => {accumulator+val}");
return accumulator + val;
}
);
Console.WriteLine("Sum: " + sum);
private bool TestForInnerException<TInnerException>(Exception exception) {
if (exception.GetType() == typeof(TInnerException)) {
return true;
}
var aggregateException = exception as AggregateException;
if (aggregateException != null) {
foreach (var innerException in aggregateException.InnerExceptions) {
if (TestForInnerException<TInnerException>(innerException)) {
return true;
@miyu
miyu / Program.cs
Created December 30, 2016 20:30
Parallel.ForEach thread pool starvation test
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace TaskParallelForEachDeadlock {
class Program {
static void Main(string[] args) {
int workerThreads, iocpThreads;
ThreadPool.SetMaxThreads(4, 4);
/sys/bus/i2c/devices
i2c0: 0x44E0_B000
i2c1: 0x4802_A000
i2c2: 0x4819_C000
echo BB-I2C1 > /sys/devices/bone_capemgr.9/slots
root@beaglebone:/sys/bus/i2c/devices# ls -l
total 0
lrwxrwxrwx 1 root root 0 Jan 1 00:00 0-0024 -> ../../../devices/ocp.3/44e0b000.i2c/i2c-0/0-0024
@miyu
miyu / New
Last active May 18, 2016 07:49
Dargon.Commons.Channels
await new Select {
Case(Time.After(500), async () => {
if (ticksToVictory == 1) {
logger.Info("Party time!");
await Task.FromResult(false);
} else {
await ElectionCandidatePhaseAsync(ticksToVictory - 1, followerIds);
}
}),
Case(electChannel, async message => {
foreach (var networkInterface in NetworkInterface.GetAllNetworkInterfaces()) {
if (!networkInterface.SupportsMulticast ||
networkInterface.OperationalStatus != OperationalStatus.Up ||
networkInterface.IsReceiveOnly) continue;
var ipv4Properties = networkInterface.GetIPProperties()?.GetIPv4Properties();
if (ipv4Properties != null)
sockets.Add(CreateSocket(ipv4Properties.Index));
}