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


문제 설명

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

/strike>
</p>

이에 대한 더 나은 공식이 있습니까?

업데이트

실수로 인해 일부 예상 값이 부정확했습니다. 첫 번째 위치에서 두 번째 좌표의 원점.

게임 조각이 정사각형 그리드에서 한 좌표에서 다른 좌표로 이동하는 데 필요한 이동 수를 구하려고 합니다. 한 번에 한 칸씩 이동하면 대각선도 1 이동으로 계산됩니다.

따라서 ...

  • 1,1에서 5,5로 이동하려면 4개의 대각선 이동과 한 번의 이동이 필요합니다. 수평/수직 이동으로 총 5회 이동합니다. 정확하게 반환됩니다.
  • 1,1에서 5,3으로 이동하려면 대각선으로 3회 이동하고 수평/수직으로 1회 이동하여 총 4회 이동합니다. 이제 올바르게 반환됩니다.
  • 1,1에서 1,2/2,1/2,2 중 하나로 이동하면 됩니다. 각각 올바르게 반환됩니다.
  • 0,2에서 5,8까지는 대각선으로 5번 이동하고 수직으로 1번 이동하여 총 6번 이동합니다. 7을 잘못 반환합니다.

결과에 Math.Round()를 사용하면 다음을 얻습니다. 다음 오류...

  • 0,2에서 5,8은 6 대신 8을 반환합니다.
  • 1,1에서 5,5는 5 대신 6을 반환합니다.

    li>

Math.Ceiling()을 사용하면 다음 오류가 발생합니다...

  • 0,2 ~ 5,8 반환 6 대신 8
  • 1,1에서 2,2는 1 대신 2를 반환
  • 1,1에서 5,3은 5 대신 4를 반환<


    참조 솔루션

    방법 1:

    The problem is that performing a cast ((int)) effectively does a "floor" operation on your number, cutting off any decimal values, even if those decimal values are .99999. It sounds like you're looking instead to round the value to the nearest integer, which can be done with the Math.Round() function.

    Try this:

    result = (int)Math.Round(Math.Sqrt(underRadical));
    

    Update

    The pythagorean theorem‑based approach you're taking won't work for what you're trying to calculate. This is a completely different use case. Good news is, your use case is a lot simpler. Just figure out the distance you'd have to travel on each axis, then subtract whatever shortcuts you can take by moving diagonally. Something like this, I think:

    var yDist = Math.Abs(y2 ‑ y1);
    var xDist = Math.Abs(x2 ‑ x1);
    var diagShortcut = Math.Min(yDist, xDist);
    return yDist + xDist ‑ diagShortcut;
    

    (by HadesStriplingWarrior)

    참조 문서

    1. How do I calculate the distance between two squares on a grid, including the target square (CC BY‑SA 2.5/3.0/4.0)

#coordinates #C# #distance






관련 질문

지구의 모든 좌표를 생성하시겠습니까? (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)







코멘트