Skip to content

Instantly share code, notes, and snippets.

@andr1972
Created February 3, 2017 07:51
Show Gist options
  • Save andr1972/36808830520c059ae474061c46c282f5 to your computer and use it in GitHub Desktop.
Save andr1972/36808830520c059ae474061c46c282f5 to your computer and use it in GitHub Desktop.
INdex 2D
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace run2d
{
public class ComplexComparer : IComparer<Coord>
{
public int Compare(Coord c1, Coord c2)
{
if (c1.bits < c2.bits) return -1;
else if (c1.bits > c2.bits) return 1;
else return 0;
}
}
public class Coord
{
internal int x, y;
internal long bits = 0;
private void DivideRangeEncode(int value, int[] range)
{
int mid = (range[0] + range[1]) / 2;
bits <<= 1;
if (value >= mid)
{
bits = bits | 0x1;
range[0] = mid;
}
else
{
range[1] = mid;
}
}
private void DivideRangeDecode(ref int bits, bool b)
{
bits <<= 1;
if (b)
{
bits = bits | 0x1;
}
}
public Coord(long bits)
{
this.bits = bits;
bool isEvenBit = true;
long mask = 1<<15;
for (int i = 0; i < 16; i++)
{
if (isEvenBit)
{
DivideRangeDecode(ref x, (bits & mask) != 0);
}
else
{
DivideRangeDecode(ref y, (bits & mask) != 0);
}
isEvenBit = !isEvenBit;
mask >>= 1;
}
}
public Coord(int x, int y)
{
this.x = x;
this.y = y;
bits = 0;
int[] rangeX = { 0, 256 };
int[] rangeY = { 0, 256 };
bool isEvenBit = true;
for (int i = 0; i < 16; i++)
{
if (isEvenBit)
{
DivideRangeEncode(x, rangeX);
}
else
{
DivideRangeEncode(y, rangeY);
}
isEvenBit = !isEvenBit;
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace run2d
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<Coord> list = new List<Coord>();
ComplexComparer comparer = new ComplexComparer();
private void button1_Click(object sender, EventArgs e)
{
Bitmap drawBitmap;
drawBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = drawBitmap;
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.Dispose();
foreach (Coord c in list)
{
drawBitmap.SetPixel(c.x, c.y,Color.Red);
// Thread.Sleep(1);
pictureBox1.Refresh();
}
}
private void SetPoint(int x,int y)
{
Bitmap drawBitmap;
drawBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = drawBitmap;
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.Dispose();
Coord cleft = new Coord(x - 10, y);
Coord cright = new Coord(x + 10, y);
Coord ctop = new Coord(x, y-10);
Coord cbot = new Coord(x, y + 10);
Coord cc = new Coord(x, y);
Coord cfirst;
if (cleft.bits < ctop.bits) cfirst = cleft; else cfirst = ctop;
Coord clast;
if (cright.bits > cbot.bits) clast = cright; else clast = cbot;
int istart = list.BinarySearch(cfirst, comparer);
int iend = list.BinarySearch(clast, comparer);
for (long i=istart; i<= iend; i++)
{
Coord c = list[(int)i];
if (c.bits==cc.bits)
drawBitmap.SetPixel(c.x, c.y, Color.Yellow);
else
drawBitmap.SetPixel(c.x, c.y, Color.Red);
}
}
private void SetPointSimple(int x, int y)
{
Bitmap drawBitmap;
drawBitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height);
pictureBox1.Image = drawBitmap;
Graphics g = Graphics.FromImage(pictureBox1.Image);
g.Dispose();
Coord cc = new Coord(x, y);
Coord cfirst = new Coord(Math.Max(cc.bits - 256,0));
Coord clast = new Coord(Math.Min(cc.bits + 256,65535));
int istart = list.BinarySearch(cfirst, comparer);
int iend = list.BinarySearch(clast, comparer);
for (long i = istart; i <= iend; i++)
{
Coord c = list[(int)i];
if (c.bits == cc.bits)
drawBitmap.SetPixel(c.x, c.y, Color.Yellow);
else
drawBitmap.SetPixel(c.x, c.y, Color.Red);
}
}
private void button2_Click(object sender, EventArgs e)
{
Random rand = new Random();
SetPoint(rand.Next(256), rand.Next(256));
}
private void Form1_Load(object sender, EventArgs e)
{
for (int y = 0; y < 256; y++)
for (int x = 0; x < 256; x++)
{
Coord c = new Coord(x, y);
list.Add(c);
}
list.Sort(comparer);
}
private void button3_Click(object sender, EventArgs e)
{
Random rand = new Random();
SetPointSimple(rand.Next(256), rand.Next(256));
}
}
}
namespace run2d
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.button3 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(12, 12);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(256, 256);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(280, 23);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(280, 65);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(280, 103);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 3;
this.button3.Text = "button3";
this.button3.UseVisualStyleBackColor = true;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(367, 289);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment