public void TestClipPolygon() { Clipper2D clipper2d = new Clipper2D(); List <Vector2> clipWindow = new List <Vector2>() { new Vector2(0, 0), new Vector2(10, 0), new Vector2(10, 10), new Vector2(0, 10) }; List <Vector2> subjectPolygon = new List <Vector2>() { //new Vector2(5, 5), //new Vector2(20, 20), //new Vector2(5, 20) new Vector2(5, 5), new Vector2(20, 5), new Vector2(20, 20), new Vector2(5, 20) }; List <Vector2> clipResult = new List <Vector2>(100); clipper2d.Clip(clipWindow, subjectPolygon, clipResult); int i = 0; foreach (Vector2 vector2 in clipResult) { Console.WriteLine($"{i}: {vector2}"); i++; } }
private static unsafe void ConvertItemSurface(ISurface surface, out byte[] rawdata) { if (surface.Stride / surface.Width != 2) throw new NotImplementedException(); var bitmap = surface.GetSurface();// as BitmapSurface; var imrect = bitmap.GetImageRect(); var imavrx = bitmap.Width / 2; var imlenx = Math.Min(Math.Max(imavrx - imrect.X1, imrect.X2 + 1 - imavrx), imavrx); imrect = new Clipper2D((short)(imavrx - imlenx), imrect.Y1, (short)(imavrx + imlenx - 1), (short)(bitmap.Height - 1)); var data = new BinaryWriter(new MemoryStream()); data.Write((uint)1234); // header data.Write((ushort)imrect.Width); data.Write((ushort)imrect.Height); var lookup = (int)data.BaseStream.Position; for (var y = 0; y < imrect.Height; ++y)// fill lookup data.Write((ushort)0); lock (surface) { uint delta = surface.Stride >> 1; ushort* line = surface.ImageWordPtr + imrect.Y1 * delta; for (int y = 0; y < imrect.Height; ++y, line += delta) { data.BaseStream.Seek(lookup + (y << 1), SeekOrigin.Begin); var lineoffs = ((data.BaseStream.Length - lookup) >> 1) - imrect.Height; if (lineoffs > 0xFFFF) throw new ClassicFactoryException(); data.Write( (ushort)lineoffs ); data.BaseStream.Seek(0, SeekOrigin.End); ushort* cur = line + imrect.X1; int x = 0, j = 0, i = 0; while (i < imrect.Width) { for (i = x; i < imrect.Width; ++i) { //first pixel set if (cur[i] != 0) break; } if (i < imrect.Width) { for (j = (i + 1); j < imrect.Width; ++j) { //next non set pixel // if (cur[j] == 0) if (cur[j] == 0 && (j + 1 == imrect.Width || cur[j + 1] == 0)) break; } data.Write((ushort)(i - x)); //xoffset data.Write((ushort)(j - i)); //run for (int p = i; p < j; ++p) //data.Write((ushort)(cur[p] ^ 0x8000); data.Write((ushort)(cur[p] > 0 ? (cur[p] ^ 0x8000) : cur[p])); x = j; } } data.Write((ushort)0); //xOffset data.Write((ushort)0); //Run } } data.Flush(); rawdata = (data.BaseStream as MemoryStream).ToArray(); data.Close(); }
private static unsafe void ConvertLandSurface(ISurface surface, out byte[] rawdata) { if (surface.Stride / surface.Width != 2) throw new NotImplementedException(); var bitmap = surface.GetSurface();// as BitmapSurface; var imrect = bitmap.GetImageRect(); var imavrx = bitmap.Width / 2; var imlenx = Math.Min(Math.Max(imavrx - imrect.X1, imrect.X2 + 1 - imavrx), imavrx); imrect = new Clipper2D((short)(imavrx - imlenx), imrect.Y1, (short)(imavrx + imlenx - 1), (short)(bitmap.Height - 1)); if (imrect.Width != 44 || imrect.Height != 44) { imrect = new Clipper2D((short)0, (short)0, (ushort)bitmap.Width, (ushort)bitmap.Height); if (imrect.Width != 44 || imrect.Height != 44) throw new ClassicFactoryException(); } rawdata = new byte[0x07E8]; lock (surface) { uint delta = surface.Stride >> 1; ushort* line1 = surface.ImageWordPtr + imrect.Y1 * delta + imrect.X1; ushort* line2 = surface.ImageWordPtr + imrect.Y2 * delta + imrect.X1; fixed (byte* _data = &rawdata[0]) { var data = (uint*)_data; //data[0] = 0x00000000; // header var i1 = 0; var i2 = 505; for (var y = 0; y < 22; ++y, line1 += delta, line2 -= delta) { var cur1 = (uint*)(line1 + 21 - y); var cur2 = (uint*)(line2 + 21 + y); for (var x = 0; x <= y; ++x, ++i1, --i2, ++cur1, --cur2) { data[i1] = *cur1 ^ 0x80008000; data[i2] = *cur2 ^ 0x80008000; } } } } }