Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Point CoordsToTileId(int x, int y) {
- Point approxTileId = TileStartToTileId(x, y);
- start:
- Point approxTileStart = TileIdToTileStart(approxTileId.X, approxTileId.Y);
- var coordsOffset = new Point(x - approxTileStart.X, y - approxTileStart.Y);
- // this is fucking ridiculous
- if (coordsOffset.X < 0) {
- approxTileId = new Point(approxTileId.X - 1, approxTileId.Y);
- goto start;
- }
- if (coordsOffset.X >= 80) {
- approxTileId = new Point(approxTileId.X + 1, approxTileId.Y);
- goto start;
- }
- if (coordsOffset.Y < 0) {
- approxTileId = new Point(approxTileId.X, approxTileId.Y - 1);
- goto start;
- }
- if (coordsOffset.Y >= 36) {
- approxTileId = new Point(approxTileId.X, approxTileId.Y + 1);
- goto start;
- }
- if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 0, 12, 0, 0, 48, 0)) {
- return new Point(approxTileId.X, approxTileId.Y - 1);
- }
- if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 48, 0, 80, 0, 80, 24)) {
- return new Point(approxTileId.X + 1, approxTileId.Y);
- }
- if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 0, 36, 0, 12, 32, 36)) {
- return new Point(approxTileId.X - 1, approxTileId.Y);
- }
- if (IsPointInsideTriangle(coordsOffset.X, coordsOffset.Y, 80, 36, 80, 24, 32, 36)) {
- return new Point(approxTileId.X, approxTileId.Y + 1);
- }
- return approxTileId;
- }
- private static Point TileStartToTileId(int x, int y) {
- x -= LongerCathetus;
- y -= (int) TileCenter;
- return new Point(x/64 - y/48, x/128 + y/32);
- }
- public static Point TileIdToTileStart(int x, int y) {
- return new Point(y*32 + x*48 + LongerCathetus, x*-12 + y*24 - (int) TileCenter);
- }
- private static bool IsPointInsideTriangle(int px, int py, int ax, int ay, int bx, int by, int cx, int cy) {
- 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);
- }
- private static int TriangleArea(int ax, int ay, int bx, int by, int cx, int cy) {
- return Math.Abs(ax*by + bx*cy + cx*ay - ax*cy - cx*by - bx*ay)/2;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement