Advertisement
Guest User

Untitled

a guest
Jul 21st, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1.         public static Point CoordsToTileId(int x, int y) {
  2.             Point approxTileId = TileStartToTileId(x, y);
  3.             start:
  4.             Point approxTileStart = TileIdToTileStart(approxTileId.X, approxTileId.Y);
  5.             var coordsOffset = new Point(x - approxTileStart.X, y - approxTileStart.Y);
  6.             // this is fucking ridiculous
  7.             if (coordsOffset.X < 0) {
  8.                 approxTileId = new Point(approxTileId.X - 1, approxTileId.Y);
  9.                 goto start;
  10.             }
  11.             if (coordsOffset.X >= 80) {
  12.                 approxTileId = new Point(approxTileId.X + 1, approxTileId.Y);
  13.                 goto start;
  14.             }
  15.             if (coordsOffset.Y < 0) {
  16.                 approxTileId = new Point(approxTileId.X, approxTileId.Y - 1);
  17.                 goto start;
  18.             }
  19.             if (coordsOffset.Y >= 36) {
  20.                 approxTileId = new Point(approxTileId.X, approxTileId.Y + 1);
  21.                 goto start;
  22.             }
  23.             if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 0, 12, 0, 0, 48, 0)) {
  24.                 return new Point(approxTileId.X, approxTileId.Y - 1);
  25.             }
  26.             if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 48, 0, 80, 0, 80, 24)) {
  27.                 return new Point(approxTileId.X + 1, approxTileId.Y);
  28.             }
  29.             if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 0, 36, 0, 12, 32, 36)) {
  30.                 return new Point(approxTileId.X - 1, approxTileId.Y);
  31.             }
  32.             if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 80, 36, 80, 24, 32, 36)) {
  33.                 return new Point(approxTileId.X, approxTileId.Y + 1);
  34.             }
  35.             return approxTileId;
  36.         }
  37.  
  38.         private static Point TileStartToTileId(int x, int y) {
  39.             x -= LongerCathetus;
  40.             y -= (int) TileCenter;
  41.             return new Point(x/64 - y/48, x/128 + y/32);
  42.         }
  43.  
  44.         public static Point TileIdToTileStart(int x, int y) {
  45.             return new Point(y*32 + x*48 + LongerCathetus, x*-12 + y*24 - (int) TileCenter);
  46.         }
  47.  
  48.         private static bool IsPointInsideTriangle(int px, int py, int ax, int ay, int bx, int by, int cx, int cy) {
  49.             return TriangleArea(px, py, ax, ay, bx, by) + TriangleArea(px, py, bx, by, cx, cy) + TriangleArea(px, py, ax, ay, cx, cy) == TriangleArea(ax, ay, bx, by, cx, cy);
  50.         }
  51.  
  52.         private static int TriangleArea(int ax, int ay, int bx, int by, int cx, int cy) {
  53.             return Math.Abs(ax*by + bx*cy + cx*ay - ax*cy - cx*by - bx*ay)/2;
  54.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement