본문 바로가기
study/math

[이득우 게임수학] 벡터

by foooo828 2024. 5. 16.

3.1 데카르트 좌표계

직선의 수 집합을 수직으로 배치해 평면에 표기하는 방식
곱집합의 원어가 데카르트 곱이다 (cartatian product)
한 원소는 곱집합과 동일하게 순서쌍이라고 표현하며 좌표(coordinate)라고 부름
크기, 방향 두가지 속성을 가짐

3.2 벡터공간과 벡터

평면은 두 실수를 결합해 만들어지며, 실수는 체의 구조를 가지고 있기떄문에 평면에서의 움직임을 표현하기 위해서는 덧셈과 곱셈 연산체계를 사용한다.

  • 벡터공간 : 두개 이상의 실수를 곱집합으로 묵어 형성한 집합을 공리적집합론 관점으로 규정한 것 $V$
  • 벡터 : 벡터 공간의 원소 $\vec{v} = (x,y)$
  • 스칼라 : 체의 구조를 지니는 수집합의 원소

3.2.1 벡터 연산

  • 벡터와 벡터의 덧셈
    $\vec{v}_1 + \vec{v}_2 = (x_1,y_1)+(x_2,y_2) = (x_1+x_2,y_1+y_2)​$
  •  

  • 스칼라와 벡터의 곱셈
    $a\cdot\vec{v} = a\cdot(x,y) = (a\cdot x,a\cdot y)​$

시각적으로 보았을때 스칼라 곱셈으로 생성된 벡터는 원점을 지나 벡터와 평행한 직선 위에 있다.

3.2.2 벡터의 크기, 정규화

  • 노름 (Norm) 
  • 원점으로부터의 최단거리를 의미함
  • 피타고리스 정리를 사용해 거리를 측정
  • $c^2 = a^2 + b^2$ $\therefore c = \sqrt{a^2+b^2}$
  • 단위벡터 $\hat{v}$ : 크기가 1 인 벡터

  • 벡터 정규화 : 임의의 벡터를 단위벡터로 다듬는 작업
    어떤 벡터/ 벡터의 크기 = 단위벡터
    $\hat{v} = \frac{\vec{v}}{\vert\vec{v}\vert}$
  • 예시 (3 ,4) x ( 1 / 5 ) = (0.6 , 0.8);

3.2.3 벡터로 원그리기

유니티로 실습예제를 만들어봤따

마우스 입력 받아서 텍스처 클릭한 위치에 원을 그리게함

using System.Collections.Generic;
using UnityEngine;

public class DrawCircleWithVector2 : MonoBehaviour
{
    public Material mat;
    private Texture2D texture;
    public int radius = 15;
    public int height = 50;
    public int width = 50;

    private int cy;
    private int cx;
    private Vector2Int centerPos;
    private List<Vector2Int> container;
    private Color[] texturePixelsColors;

    void Start()
    {
        cx = (int)(width * .5f);
        cy = (int)(height * .5f);

        centerPos = new Vector2Int(cx, cy);

        texture = new Texture2D(height, width, TextureFormat.RGBA32, true);
        texture.wrapMode = TextureWrapMode.Clamp;
        texture.filterMode = FilterMode.Point;
        DrawCircle(centerPos);
    }

    void Update()
    {
        if (!Input.GetMouseButton(0))
            return;
        RaycastHit hit;
        if (!Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit))
            return;

        Renderer rend = hit.transform.GetComponent<Renderer>();
        MeshCollider meshCollider = hit.collider as MeshCollider;

        if (rend == null || rend.sharedMaterial == null || rend.sharedMaterial.mainTexture == null ||
            meshCollider == null)
            return;

        Texture2D tex = rend.material.mainTexture as Texture2D;
        Vector2 pixelUV = hit.textureCoord;
        pixelUV.x *= tex.width;
        pixelUV.y *= tex.height;

        DrawCircle(pixelUV);
    }


    void DrawCircle(Vector2 centPos)
    {
        if (container != null)
            container.Clear();
        container = new List<Vector2Int>();
        // 반지름 내 벡터를 컨테이너에 담음
        for (int y = -radius; y < radius; y++)
        {
            for (int x = -radius; x < radius; x++)
            {
                var vec = new Vector2Int(x, y);
                if (vec.magnitude <= radius)
                {
                    container.Add(vec);
                }
            }
        }

        texturePixelsColors = new Color[height * width];
        for (int i = 0; i < texturePixelsColors.Length; i++)
        {
            texturePixelsColors[i] = new Color(1, 1, 1);
        }

        for (int i = 0; i < container.Count; i++)
        {
            var idx = container[i];
            var containerIndex = GetIndex(idx, new Vector2Int(width, height));
            var textureIndex = GetIndex(new Vector2Int((int)centPos.x, (int)centPos.y), new Vector2Int(width, height));
            var addedIndex = idx + centPos;

            if (addedIndex.x > 0 && addedIndex.y > 0 && addedIndex.x < width && addedIndex.y < height)
            {
                texturePixelsColors[containerIndex + textureIndex] = new Color(0.55f, 0.44f, 1f);
            }
        }

        texture.SetPixels(texturePixelsColors);
        texture.Apply();
        mat.mainTexture = texture;
    }

    int GetIndex(Vector2Int coord, Vector2Int size)
    {
        return size.x * coord.y + coord.x;
    }
}

3.3 벡터의 결합과 생성

벡터 공간의 벡터 합과 스칼라 곱은 선형성이 있어 선형 연산이라고 하며,선형 연산을 사용해 n 개의 스칼라와 벡터를 결합하는 수식을 선형 결합 이라고 함 $a_1\vec{v_1} + a_2\vec{v_2} +...a_n\vec{v_n} =$ $\vec{v^1}$

  • 선형 종속관계 : 벡터에 곱하는 스칼라가 0 이 아님에도 결과가 0벡터가 된다면 선형 결합에 사용된 벡터가 선형종속 관계를 갖는다. 
    .

    • 평행한 벡터 결합으로 평행하지 않은 다른 벡터를 생성하는것이 가능할까?
    • $a(1,2)+b(2,4) = (7,−1)$ ??
      $2a+4b = −1$
      $2𝑎+4𝑏 = 14$
    • 위와 같이 해가 존재하지 않으므로 실수의 모든 벡터를 만들 수 없다
    • 선형 종속관계의 두 벡터는 평행한 모양이며 한 벡터의 스칼라 곱 적용한 결과와 같다

 

 

  • 선형 독립 관계 : 0벡터가 나오기 위해 모든 스칼라값이 0 이어야함
    독립관계를 가지는 벡터의 결합은 벡터공간의 모든 벡터를 생성할 수 있음
    • $(w_x,w_y) = a\cdot(2,1)+b\cdot(1,3)$
      $a + b = w_x $ $a + 3b = w_y$
      $b =2w_y-w_x \over 5$ $a =3w_x-w_y \over 5$
      → 해가 언제나 존재하므로 모든 벡터 생성 가능
  • 기저 : 선형 독립관계를 가지는 벡터의 집합
    세 백터로 구성된 선형결합식의 경우 $-c(x,y) + c(x,y) = (0,0)$ 다음과 같이 스칼라 c 를 이용해 영벡터를 만들 수 있기때문에 선형 독립관계를 만족하지 않으므로 기저 집합의 원소 수는 언제나 2개임
  • 표준 기저벡터 : 표준기저( 2차원을 구성하는 기저 중 한 축만 사용하는 단위벡터로 구성된 집합 ) 의 각 원소
    $e_1$ = (1,0)
    $e_2$ = (0,1)

 

 

 

 

https://ko.khanacademy.org/computing/computer-programming/programming-natural-simulations/programming-vectors/a/vector-magnitude-normalization

댓글