使用外部点查找矩形内部

Find inside of rectangle using outside points

我有一个 csv 文件,所有数字(即 0、2、34、0、2...)都在一行中。我正在使用扫描仪从文件中读取它们。该文件代表一个相当大的 in-game 地图。我知道这张地图的宽度和高度。扫描此文件时,我只想捕获下图中的棕色矩形(游戏地图的一部分)。我正在扫描文件并想将 csv 文件中的值放入 short[].

int mapWidth        = 8;
int mapHeight       = 6;
int rectangleWidth  = 5;
int rectangleHeight = 3;
short[] tmpMap = new short[rectangleWidth * rectangleHeight];

Scanner scanner = new Scanner(new File(path));
scanner.useDelimiter(", ");

我也很幸运知道我需要的矩形的4个角。深棕色方块(我需要捕获的矩形的四个角)可以表示为:

int topLeftIndex     = 17;
int topRightIndex    = 21;
int bottomLeftIndex  = 33;
int bottomRightIndex = 37;

我知道在我的扫描方法中,我可以检查我是否在矩形和蓝色突出显示框的边界内,具有以下内容:

if (count >= topLeftIndex && count <= bottomRightIndex){
//within or outside (east and west) of rectangle

我无法思考识别和不存储突出显示的蓝色方块的逻辑。

这个矩形的大小、整个地图的大小和深棕色点只是数字示例,会发生变化,但我会一直知道它们。谁能帮帮我?

这是我目前的情况:

private static short[] scanMapFile(String path, int topLeftIndex, 
    int topRightIndex, int bottomLeftIndex, int bottomRightIndex)
    throws FileNotFoundException {
    Scanner scanner = new Scanner(new File(path));
    scanner.useDelimiter(", ");

    short[] tmpMap = new short[mapWidth * mapHeight];
    int count = 0;
    int arrayIndex = 0;

    while(scanner.hasNext()){
        if (count >= topLeftIndex && count <= bottomRightIndex){ 
        //within or outside (east and west) of rectangle
            if (count == bottomRightIndex){ //last entry
                tmpMap[arrayIndex] = Short.parseShort(scanner.next());
                break;
            } else { //not last entry
                tmpMap[arrayIndex] = Short.parseShort(scanner.next());
                arrayIndex++;
            }
        } else {
            scanner.next(); //have to advance scanner
        }
        count++;
    }
    scanner.close();
    return tmpMap;
}

注意:这不是学校作业 :) 任何帮助将不胜感激。

你可以这样:

public boolean isInside(int n) {
    if(n >= topLeftIndex && n <= bottomRightIndex) {
        if(n % mapWidth >= topLeftIndex % mapWidth
          && mapWidth % mapWidth <= bottomRightIndex % mapWidth) {
            return true;
        }
    }
    return false;
}

这首先检查您已经检查过的内容,然后检查 "column" 是否也正确。 如果您知道左上角索引、右下角索引和地图宽度,这总是有效的。

这是我捕捉所有侧面的方法:

int topLeftChunkIndex = characterX - (chunkWidth / 2) + ((characterY - (chunkHeight / 2)) * mapWidth);
int topRightChunkIndex = topLeftChunkIndex + chunkWidth - 1;
//int bottomRightChunkIndex = characterX + (chunkWidth / 2) + ((characterY + (chunkHeight / 2)) * mapWidth);
//int bottomLeftChunkIndex = bottomRightChunkIndex - chunkWidth + 1;

int[] leftChunkSides = new int[chunkHeight];
int[] rightChunkSides = new int[chunkHeight];

for (int i = 0; i < chunkHeight; i++){
    leftChunkSides[i] = topLeftChunkIndex + (mapWidth * i);
    rightChunkSides[i] = topRightChunkIndex + (mapWidth * i);
}

这是我后来检查的方式:

public static boolean isInsideMapChunk(int n, int[] leftChunkSides, int[] rightChunkSides) {
    for (int i = 0; i < chunkHeight; i++){
        if (n >= leftChunkSides[i] && n <= rightChunkSides[i]){
            return true;
        }
    }
    return false;
}