从网格中选定的切片绘制大矩形

时间:2015-02-17 01:00:22

标签: c++ grid

我有一个2d地图网格系统。当我在鼠标上按住鼠标并将鼠标移动到其他图块时,我想绘制一个较大的矩形,其中第一个图块作为一个角,而当前图块作为较大绘制矩形的另一个角。

目前我有以下情况,但这只是一团糟而且还在增长。必须有一种更简单的方法来做到这一点。请注意,在我将鼠标按住瓷砖后,我可以向上/向下/向左/向右移动以制作矩形。我的大脑是油炸的,无法将其分解为我所知道的更少的代码。

enter image description here

if (startBuyTile.col < mposTile.col)
                {
                    // use startBuyTile as top/left
                    // use current tile as bottom/right
                    int i = 0;

                    Vec2 spos = TileToWorldCoords(startBuyTile.row, startBuyTile.col);
                    spos.x = spos.x / 100;
                    spos.y = spos.y / 100;

                    if (startBuyTile.row < mposTile.row)
                    {
                        topLeft.x = spos.x - .64;
                        topLeft.y = 0;
                        topLeft.z = spos.y + .64;

                        bottomRight.x = pos.x + .64;
                        bottomRight.y = 0;
                        bottomRight.z = pos.y - .64;

                        topRight.x = spos.x + .64 + (1.28 * Math::Abs(colCount));
                        topRight.y = 0;
                        topRight.z = spos.y + .64;

                        bottomLeft.x = spos.x - .64;
                        bottomLeft.y = 0;
                        bottomLeft.z = pos.y - .64;
                    }
                    else if (startBuyTile.row > mposTile.row)
                    {
                        topLeft.x = pos.x - .64;
                        topLeft.y = 0;
                        topLeft.z = pos.y + .64;

                        topRight.x = spos.x + .64;
                        topRight.y = 0;
                        topRight.z = pos.y + .64;

                        bottomLeft.x = pos.x + .64;
                        bottomLeft.y = 0;
                        bottomLeft.z = spos.y - .64;

                        bottomRight.x = spos.x - .64;
                        bottomRight.y = 0;
                        bottomRight.z = spos.y - .64;
                    }
                    else
                    {
                        topLeft.x = spos.x - .64;
                        topLeft.y = 0;
                        topLeft.z = spos.y + .64;

                        bottomRight.x = pos.x + .64;
                        bottomRight.y = 0;
                        bottomRight.z = pos.y - .64;

                        topRight.x = pos.x + .64;
                        topRight.y = 0;
                        topRight.z = pos.y + .64;

                        bottomLeft.x = spos.x - .64;
                        bottomLeft.y = 0;
                        bottomLeft.z = spos.y - .64;
                    }
                }
                else if (startBuyTile.col > mposTile.col)
                {
                    // use current tile as top/left
                    // use startBuyTile as bottom/right
                    int j = 0;
                }
                else
                {
                    topLeft.x = pos.x - .64;
                    topLeft.y = 0;
                    topLeft.z = pos.y + .64;

                    topRight.x = pos.x + .64;
                    topRight.y = 0;
                    topRight.z = pos.y + .64;

                    bottomLeft.x = pos.x - .64;
                    bottomLeft.y = 0;
                    bottomLeft.z = pos.y - .64;

                    bottomRight.x = pos.x + .64;
                    bottomRight.y = 0;
                    bottomRight.z = pos.y - .64;
                }
            }
            else
            {
                topLeft.x = pos.x - .64;
                topLeft.y = 0; 
                topLeft.z = pos.y + .64;

                topRight.x = pos.x + .64; 
                topRight.y = 0; 
                topRight.z = pos.y + .64;

                bottomLeft.x = pos.x - .64; 
                bottomLeft.y = 0; 
                bottomLeft.z = pos.y - .64;

                bottomRight.x = pos.x + .64; 
                bottomRight.y = 0; 
                bottomRight.z = pos.y - .64;
            }

1 个答案:

答案 0 :(得分:0)

我明白了。在起始区块和当前打开的区块之间,我需要获得它们之间的最低行/列和最高行/列,这将给我我的角落,从中我可以得到我的左上/右上角。左下角/右下角指向我的矩形。

我用min()和max()来获取这些角落瓷砖。