public unsafe void UpdateBitmap() { if (m_Owner == null) { Invalidate(); return; } // Build histogram const int BUCKETS_COUNT = 100; int[] histo = new int[BUCKETS_COUNT]; for (int i = 0; i < Form1.PIXELS_COUNT; i++) { Form1.Pixel P = m_Owner.m_Pixels[i]; float2 N = P.Normal; if (N.x < 0.0f) { N = -N; } float angle = (float)Math.Atan2(N.y, N.x); float normAngle = 0.5f + angle / (float)Math.PI; int bucketIndex = Math.Max(0, Math.Min(BUCKETS_COUNT - 1, (int)(BUCKETS_COUNT * normAngle))); histo[bucketIndex]++; } int maxBucket = 0; for (int i = 0; i < BUCKETS_COUNT; i++) { maxBucket = Math.Max(maxBucket, histo[i]); } // Render int W = m_Bitmap.Width; int H = m_Bitmap.Height; using (Graphics G = Graphics.FromImage(m_Bitmap)) { G.FillRectangle(Brushes.White, 0, 0, W, H); for (int X = 0; X < W; X++) { int bucketIndex = X * BUCKETS_COUNT / W; float count = (float)histo[bucketIndex] / maxBucket; G.DrawLine(Pens.Black, X, H, X, H * (1.0f - 0.9f * count)); } } Invalidate(); }
float2 SamplePosition(float _angle) { float pixelAngle = _angle * Form1.PIXELS_COUNT / (float)(2.0 * Math.PI); int i0 = Math.Max(0, Math.Min(Form1.PIXELS_COUNT - 1, (int)Math.Floor(pixelAngle))); int i1 = Math.Min(Form1.PIXELS_COUNT - 1, i0 + 1); float t = pixelAngle - i0; Form1.Pixel P0 = m_Owner.m_Pixels[i0]; Form1.Pixel P1 = m_Owner.m_Pixels[i1]; float dist = P0.Distance * (1.0f - t) + P1.Distance * t; dist = Math.Min(1000.0f, dist); float2 result = dist * new float2((float)Math.Cos(_angle), (float)Math.Sin(_angle)); return(result); }