Skip to content

Instantly share code, notes, and snippets.

@kimsama
Last active January 8, 2021 08:40
Show Gist options
  • Select an option

  • Save kimsama/10437486 to your computer and use it in GitHub Desktop.

Select an option

Save kimsama/10437486 to your computer and use it in GitHub Desktop.
근사값을 구하는 함수 예, 주어진 배열에서 p와 가장 근사한 배열값의 인덱스를 리턴. A function which returns the array index of most approximate value with the p.
/// <summary>
/// 주어진 배열에서 p와 가장 근사한 배열값의 인덱스를 리턴.
/// </summary>
public static int Near(float[] array, float p)
{
float tmp = 0f;
float min = 100f;
float near = 0f;
int found = 0;
for(int i=0; i<array.Length; i++)
{
tmp = array[i] - p;
if (Math.Abs(min) > Math.Abs(tmp))
{
min = tmp;
near = array[i];
found = i;
}
}
//Debug.Log ("Near: " + near.ToString ());
return found;
}
@dev-ruby
Copy link

dev-ruby commented Jan 8, 2021

땡큐베리망치

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment