본문 바로가기
Projects/Foot

발의 아치를 찾는 알고리즘

by wenect 2023. 4. 13.

발가락 사이를 인식하고 그 점들을 이어서 원을 만든다.

이원의 선상에 발의 끝점을 각각 구해서 이점을 발의 아치에 사용한다.

네 점을 지나는 원의 방정식을 찾기 위해 삼각형의 외심 개념을 사용할 수 있습니다.

  1. 먼저 주어진 4개의 점 중에서 임의의 3개의 점을 선택해야 합니다. 세 점이 (x1,y1), (x2,y2) 및 (x3,y3)이라고 가정해 보겠습니다.
  2. 그런 다음 이 세 점에 의해 형성된 변의 수직 이등분선을 찾아야 합니다. 수직이등분선은 변에 수직이고 변의 중간점을 통과하는 선입니다.
  3. 주어진 네 점에서 세 점의 가능한 모든 조합에 대해 2단계를 반복합니다.
  4. 임의의 두 수직 이등분선의 교점은 세 점에 의해 형성된 삼각형의 외심입니다. 외심은 이 세 점을 지나는 원의 중심입니다.
  5. 세 점의 가능한 모든 조합에 대한 외심을 찾으면 모두 한 점에서 일치한다는 것을 알 수 있습니다. 이 점은 네 점을 모두 통과하는 원의 중심입니다.
  6. 마지막으로 네 점 중 하나와 원의 중심 사이의 거리를 계산하여 원의 반지름을 찾을 수 있습니다.
  7. 그러면 원의 방정식은 (x - h)^2 + (y - k)^2 = r^2입니다. 여기서 (h,k)는 원의 중심이고 r은 반지름입니다.

 

public static void FindCircleEquation(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
{
    float a1 = x2 - x1;
    float b1 = y2 - y1;
    float c1 = (a1 * a1) + (b1 * b1);
    float a2 = x3 - x1;
    float b2 = y3 - y1;
    float c2 = (a2 * a2) + (b2 * b2);
    float a3 = x4 - x1;
    float b3 = y4 - y1;
    float c3 = (a3 * a3) + (b3 * b3);

    float d = (a1 * b2) + (a2 * b3) + (a3 * b1) - (b1 * a2) - (b2 * a3) - (b3 * a1);

    float centerX = ((c1 * (b2 - b3)) + (c2 * (b3 - b1)) + (c3 * (b1 - b2))) / (2 * d);
    float centerY = ((c1 * (a3 - a2)) + (c2 * (a1 - a3)) + (c3 * (a2 - a1))) / (2 * d);

    float radius = (float)Math.Sqrt(((centerX - x1) * (centerX - x1)) + ((centerY - y1) * (centerY - y1)));

    Console.WriteLine($"Equation of circle: (x - {centerX})^2 + (y - {centerY})^2 = {radius * radius}");
}

x^2 + y^2 = r^2 을 구했습니다.

이제 특정한 x점을 알았을때 y의 점을 구할수 있습니다.

구한 원의 방정식에 x의 값을 입력했을 때 y의 값을 찾으려면 원의 방정식의 표준 형식을 사용할 수 있습니다.

(x - h)^2 + (y - k)^2 = r^2

여기서 (h, k)는 원의 중심이고 r은 반지름입니다. h, k, r의 값이 있는 경우 방정식에서 x 값을 대체하고 y를 풀 수 있습니다.

원의 중심이 (2, 3)이고 반지름이 5라고 가정해 보겠습니다. 그러면 원의 방정식은 다음과 같습니다.

(x - 2)^2 + (y - 3)^2 = 25

x = 4일 때 y의 값을 찾으려면 방정식에서 x = 4를 대체하고 y를 풀 수 있습니다.

(4 - 2)^2 + (그리고 - 3)^2 = 25

2^2 + (및 - 3)^2 = 25

(y - 3)^2 = 25 - 4

(y - 3)^2 = 21

y - 3 = ±√21

y = 3 ± √21

따라서 x = 4일 때 y 값은 점 (4, y)가 원의 어느 쪽에 있는지에 따라 3 + √21 또는 3 - √21이 될 수 있습니다.

using UnityEngine;

public class CircleEquation : MonoBehaviour
{
    public float centerX = 2f; // Center X coordinate of the circle
    public float centerY = 3f; // Center Y coordinate of the circle
    public float radius = 5f; // Radius of the circle

    // Function to find the value of y for a given value of x
    public float FindY(float x)
    {
        float y = centerY + Mathf.Sqrt(radius * radius - (x - centerX) * (x - centerX));
        return y;
    }

    void Start()
    {
        // Example usage: Find the value of y when x = 4
        float x = 4f;
        float y = FindY(x);
        Debug.Log("When x = " + x + ", y = " + y);
    }
}

다음은 4개의 무작위 점에 대한 사용자 입력을 통합하고 FindCircleEquation 함수를 사용하여 원의 centerX, centerYradius를 찾고 의 좌표를 찾는 함수를 구현하는 업데이트된 C# 스크립트입니다. 임의의 x 값이 입력될 때 원 방정식의 y:

using UnityEngine;

public class CircleEquation : MonoBehaviour
{
    public Vector2 point1; // First point on the circle
    public Vector2 point2; // Second point on the circle
    public Vector2 point3; // Third point on the circle
    public Vector2 point4; // Fourth point on the circle

    // Function to obtain the equation of the circle
    public string FindCircleEquation(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
    {
        float a1 = x2 - x1;
        float b1 = y2 - y1;
        float c1 = (a1 * a1) + (b1 * b1);
        float a2 = x3 - x1;
        float b2 = y3 - y1;
        float c2 = (a2 * a2) + (b2 * b2);
        float a3 = x4 - x1;
        float b3 = y4 - y1;
        float c3 = (a3 * a3) + (b3 * b3);

        float d = (a1 * b2) + (a2 * b3) + (a3 * b1) - (b1 * a2) - (b2 * a3) - (b3 * a1);

        centerX = ((c1 * (b2 - b3)) + (c2 * (b3 - b1)) + (c3 * (b1 - b2))) / (2 * d);
        centerY = ((c1 * (a3 - a2)) + (c2 * (a1 - a3)) + (c3 * (a2 - a1))) / (2 * d);

        radius = Mathf.Sqrt(((centerX - x1) * (centerX - x1)) + ((centerY - y1) * (centerY - y1)));

        string equation = "(x - " + centerX + ")^2 + (y - " + centerY + ")^2 = " + (radius * radius);
        return equation;
    }

float centerX;
float centerY;
float radius;

    // Function to find the coordinates of y for a given value of x
    public Vector2 FindY(float x)
    {
        float y1 = centerY + Mathf.Sqrt((radius * radius) - Mathf.Pow((x - centerX), 2));
        float y2 = centerY - Mathf.Sqrt((radius * radius) - Mathf.Pow((x - centerX), 2));
        return new Vector2(x, y1); // return both y1 and y2 if desired
    }

    void Start()
    {
        // Example usage: Find the equation of the circle
        string equation = FindCircleEquation(point1.x, point1.y, point2.x, point2.y, point3.x, point3.y, point4.x, point4.y);
        Debug.Log("Equation of circle: " + equation);

        // Example usage: Find the coordinates of y when x = 4
        float x = 4f;
        Vector2 yCoords = FindY(x);
        Debug.Log("When x = " + x + ", y = " + yCoords.y);
    }
}

발의 엄지와 검지사이점을 point1이라고 할때 , point1과 point4의 각각의 바깥쪽 방향으로 증가시키면서 발의 영역을 벝어날때까지 증가시키면서 찾아본다.

각각 찾은 점을 이용해 아치를 계산하는데 사용한다.

댓글