2.5D地图坐标转换

作者:追风剑情 发布于:2016-6-21 11:53 分类:Algorithms

语言:C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6.  
  7. namespace MapEditor.Map
  8. {
  9. /// <summary>
  10. /// 2.5D地图坐标转换工具类
  11. /// </summary>
  12. public class CoordinateUtil
  13. {
  14. /// <summary>
  15. /// 像素坐标转网格坐标
  16. /// </summary>
  17. /// <param name="tileWidth">网格宽度</param>
  18. /// <param name="tileHeight">网格高度</param>
  19. /// <param name="px">像素x坐标</param>
  20. /// <param name="py">像素y坐标</param>
  21. /// <returns></returns>
  22. public static Point GetCellPoint(int tileWidth, int tileHeight, int px, int py)
  23. {
  24. int xtile = 0; //网格的x坐标
  25. int ytile = 0; //网格的y坐标
  26. int cx, cy, rx, ry;
  27. cx = (int)(px / tileWidth) * tileWidth + tileWidth/2; //计算出当前X所在的以tileWidth为宽的矩形的中心的X坐标
  28. cy = (int)(py / tileHeight) * tileHeight + tileHeight/2;//计算出当前Y所在的以tileHeight为高的矩形的中心的Y坐标
  29. rx = (px - cx) * tileHeight/2;
  30. ry = (py - cy) * tileWidth/2;
  31. if (Math.Abs(rx)+Math.Abs(ry) <= tileWidth * tileHeight/4)//<=上三角面积
  32. {
  33. xtile = (int)(px / tileWidth);
  34. ytile = (int)(py / tileHeight) * 2;
  35. }
  36. else
  37. {//偶行
  38. px = px - tileWidth/2;
  39. xtile = (int)(px / tileWidth) + 1;
  40. py = py - tileHeight/2;
  41. ytile = (int)(py / tileHeight) * 2 + 1;
  42. }
  43.  
  44. return new Point(xtile - (ytile&1), ytile);
  45. }
  46.  
  47. /// <summary>
  48. /// 网格坐标转像素坐标
  49. /// </summary>
  50. /// <param name="tileWidth">网格宽度</param>
  51. /// <param name="tileHeight">网格高度</param>
  52. /// <param name="tx">网格x坐标</param>
  53. /// <param name="ty">网格y坐标</param>
  54. /// <returns></returns>
  55. public static Point GetPixelPoint(int tileWidth, int tileHeight, int tx, int ty)
  56. {
  57. //偶数行tile中心
  58. int tileCenter = (tx * tileWidth) + tileWidth/2;
  59. // x象素 如果为奇数行加半个宽
  60. int xPixel = tileCenter + (ty&1) * tileWidth/2;
  61. // y象素
  62. int yPixel = (ty + 1) * tileHeight/2;
  63. return new Point(xPixel, yPixel);
  64. }
  65.  
  66. /// <summary>
  67. /// 根据网格索引取得网格坐标
  68. /// </summary>
  69. /// <param name="gindex">网格索引</param>
  70. /// <param name="cols">网格列数</param>
  71. public static Point GetICellPoint( int gindex, int cols )
  72. {
  73. int xtile = gindex % cols;
  74. int ytile = gindex / cols;
  75. return new Point(xtile, ytile);
  76. }
  77. /// <summary>
  78. /// 根据网格索引取得象素坐标
  79. /// </summary>
  80. /// <param name="gindex">网格索引</param>
  81. /// <param name="cols">网格列数</param>
  82. /// <param name="rows">网格行数</param>
  83. /// <param name="tileWidth">网格宽度</param>
  84. /// <param name="tileHeight">网格高度</param>
  85. /// <returns></returns>
  86. public static Point GetIPixelPoint( int gindex, int cols, int rows, int tileWidth, int tileHeight )
  87. {
  88. Point cellPoint = GetICellPoint(gindex, cols);
  89. Point pixelPoint = GetPixelPoint(tileWidth, tileHeight, cellPoint.X, cellPoint.Y);
  90. return pixelPoint;
  91. }
  92. /// <summary>
  93. /// 根据网格坐标取得网格索引
  94. /// </summary>
  95. /// <param name="gx">网格x坐标</param>
  96. /// <param name="gy">网格y坐标</param>
  97. /// <param name="cols">网格列数</param>
  98. /// <returns></returns>
  99. public static int GetCellIndex( int gx, int gy, int cols )
  100. {
  101. int gindex = gy * cols + gx;
  102. return gindex;
  103. }
  104. }
  105. }


标签: Algorithms

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号