좌표가 영역 내부에 있는지 감지하는 방법은 무엇입니까? (How to detect if coordinate is inside region?)


문제 설명

좌표가 영역 내부에 있는지 감지하는 방법은 무엇입니까? (How to detect if coordinate is inside region?)

플레이어가 특정 지역 내에 있는지 감지하려고 합니다. 현재 cornerOnecornerTwo, 모서리는 기본적으로 X, Y, Z를 포함하는 벡터 변수입니다. 모든 영역을 MutableSet.

내가 전달하는 새 벡터가 영역 내부에 있는지 확인하고 싶습니다. 현재 시도한 것은 다음과 같습니다.

fun isInRegion(location: Location): Boolean {
    return regions.none { inside(location, it.cornerOne, it.cornerTwo) }
}

private fun inside(location: Location, cornerOne: Location, cornerTwo: Location): Boolean {
    return (location.x >= cornerOne.x && location.x <= cornerTwo.x) && 
           (location.z >= cornerOne.z && location.z <= cornerTwo.z)
}

Y를 무시합니다. 가로로만 표시되므로 높이는 무시하겠습니다. 현재 가지고 있는 방식으로 처음 3 개 지역에서 작동하지만 4 번째 지역을 만들 자마자 완전히 작동을 멈추고 첫 번째 지역은 감지하지만 다른 지역은 감지하지 못합니다. 이 작업을 수행하는 더 좋은 방법이 있습니까? 쿼드트리가 더 나을 수 있다는 말을 들었지만 이 상황에서 어떻게 작동하는지 이해가 되지 않습니다.

추신: Java에 태그를 지정하고 있습니다. 누군가 Java 섹션에서 이를 본다면 그렇게 하지 않을 것이기 때문입니다. Java 도움말도 염두에 두십시오.

편집: 지역 코드에 if (!isValidRegion()) return이 있어 영역이 너무 작지 않음:

fun isValidRegion(): Boolean {
    return !(getXSelected() < 5 || getZSelected() < 5)
}

이렇게 하면 cornerOne.x <= cornerTwo.xcornerOne.z <= cornerTwo.z.

선택한 X를 가져오는 방법입니다.'


참조 솔루션

방법 1:

Change your code in one of two ways:

1) change the definition of inside to be (less preferred):

private fun inside(location: Location, cornerOne: Location, cornerTwo: Location): Boolean {
    return (location.x >= Math.min(cornerOne.x, cornerTwo.x) && location.x <= Math.max(cornerOne.x, cornerTwo.x)) && 
           (location.z >= Math.min(cornerOne.z, cornerTwo.z) && location.z <= Math.max(cornerOne.z, cornerTwo.z))
}

Or change the way you generate cornerOne and cornerTwo:

2.1) do without the `abs in your generation (you will need more iterations of generation)

2.2) after you generate the initial candidates of corners swap cornerOne xs if their order is not as expected and do the same on the z axis (separately!!)

(by User12322341232142Boris Strandjev)

참조 문서

  1. How to detect if coordinate is inside region? (CC BY‑SA 2.5/3.0/4.0)

#coordinates #Kotlin #java






관련 질문

지구의 모든 좌표를 생성하시겠습니까? (Generate all coordinates of earth?)

Spritekit이 부모를 변경하면 노드가 사라집니다. (Spritekit changing parent makes the node disappear)

C++의 파일에서 읽은 값을 사용하는 데 도움이 되나요? (Some help using my values read from a file in C++?)

Java의 캔버스에 마우스가 그리는 좌표만 화면에 인쇄하려면 어떻게 해야 합니까? (How do I only print to the screen the coordinates of what the mouse is drawing on a canvas in Java?)

좌표가 영역 내부에 있는지 감지하는 방법은 무엇입니까? (How to detect if coordinate is inside region?)

대상 사각형을 포함하여 그리드의 두 사각형 사이의 거리를 계산하는 방법 (How do I calculate the distance between two squares on a grid, including the target square)

두 좌표 배열 간의 대응 관계 찾기 (Find correspondence between 2 arrays of coordinates)

Postgres 쿼리에서 포인트 유형을 사용하는 방법 (Postgres how to use point type in query)

좌표 목록으로 작업하고 싶습니다 [Haskell] (I want to work with a list of coordinates [Haskell])

C 프로그램에서 해당 선의 특정 점에서 특정 거리, 선 위의 점 찾기 (Find a point on a line, a certain distance from a certain point on that line in c program)

점이 있는 geopandas 데이터 프레임에서 다각형 만들기 (Creating a polygon from a geopandas dataframe with points)

Netlogo는 특정 xycor를 설정합니다. (Netlogo set specific xycor)







코멘트