Skip to content

Instantly share code, notes, and snippets.

@einarwh
einarwh / WheelPrimeEnumerator.cs
Created October 5, 2011 01:12
A prime number enumerator that uses wheel factorization.
public class WheelPrimeEnumerator : IEnumerator<int>
{
private readonly IEnumerator<int> _candidates =
new WheelSequence(11,
new BrokenRecord<int>(
new[] { 4, 2, 4, 2, 4, 6, 2, 6 }
)
).GetEnumerator();
private readonly IPriorityQueue<NumberEnumerator> _pq =
new IntervalHeap<NumberEnumerator>();
@einarwh
einarwh / WheelPrimeSequence.cs
Created October 5, 2011 01:16
Infinite sequence of prime numbers that uses wheel factorization.
public class WheelPrimeSequence : IEnumerable<int>
{
public IEnumerator<int> GetEnumerator()
{
yield return 2;
yield return 3;
yield return 5;
var e = new WheelPrimeEnumerator();
while (e.MoveNext())
{
@einarwh
einarwh / invader.json
Created November 2, 2011 20:11
JSON description of a space invader.
{
"pixelsWide": 13,
"pixelsHigh": 10,
"pixelSize": 20,
"payload":
[
{
"color" : '#000000',
"pixels" :
[
@einarwh
einarwh / PixItData.cs
Created November 2, 2011 20:38
Holds data describing a pix-it image.
public class PixItData
{
private readonly Color? _bgColor;
private readonly int _pixelsWide;
private readonly int _pixelsHigh;
private readonly int _pixelSize;
private readonly Dictionary<Color, IEnumerable<Pixel>> _data;
public PixItData(Color? bgColor, int pixelsWide,
int pixelsHigh, int pixelSize,
@einarwh
einarwh / ToPixItData.cs
Created November 2, 2011 20:55
Extracts JSON data into .NET object.
private static PixItData ToPixItData(string json)
{
JObject o = JObject.Parse(json);
int size = o.SelectToken("pixelSize").Value<int>();
int wide = o.SelectToken("pixelsWide").Value<int>();
int high = o.SelectToken("pixelsHigh").Value<int>();
JToken bg = o.SelectToken("background");
Color? bgColor = null;
if (bg != null)
{
@einarwh
einarwh / Pixel.cs
Created November 2, 2011 20:58
A Pix-It Pixel.
public class Pixel
{
private readonly int _x;
private readonly int _y;
public Pixel(int x, int y)
{
_x = x;
_y = y;
}
@einarwh
einarwh / CreateImage.cs
Created November 2, 2011 21:02
Create a pix-it image.
private static Image CreateImage(PixItData data)
{
int width = data.PixelSize * data.PixelsWide;
int height = data.PixelSize * data.PixelsHigh;
var image = new Bitmap(width, height);
using (Graphics g = Graphics.FromImage(image))
{
if (data.Background.HasValue)
{
Color bgColor = data.Background.Value;
@einarwh
einarwh / PixItHandler.cs
Created November 2, 2011 21:05
A custom HTTP handler to produce Pix-It images.
public class PixItHandler : IHttpHandler
{
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
@einarwh
einarwh / gist:1334939
Created November 2, 2011 21:13
Including the custom HTTP handler.
<configuration>
<system.web>
<httpHandlers>
<add verb="POST" path="pix.it"
type="ample.code.pixit.PixItHandler, PixItHandler" />
</httpHandlers>
</system.web>
</configuration>
@einarwh
einarwh / laffer.json
Created November 4, 2011 19:44
JSON description of an anti-hero looking to get laid.
{
"background": "#54FCFC",
"pixelsWide": 18,
"pixelsHigh": 36,
"pixelSize": 4,
"payload":
[
{
"color" : "#000000",
"pixels" :