How to Normalize a Vector

Unity3D 2018. 9. 6. 21:49
반응형




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


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp_20180827
{
    class Program
    {
        static void Main(string[] args)
        {
            Vector2 a = new Vector2(34);
            var normal = a.normalize;
            Console.WriteLine(normal.ToString());
            Console.ReadKey();
            
        }
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApp_20180827
{
    public class Vector2
    {
        public float x { get; private set; }
        public float y { get; private set; }
        public Vector2 normalize {
            get {
                float c;
 
                var powX = (float)Math.Pow(this.x, 2);
                var powY = (float)Math.Pow(this.y, 2);
 
                c = (float)Math.Sqrt(powX + powY);
                return new Vector2(this.x / c, this.y / c);
            }
        }
 
        public Vector2(float x, float y)
        {
            this.x = x;
            this.y = y;
        }
 
        public static Vector2 operator - (Vector2 b, Vector2 a)
        {
            return new Vector2(b.x - a.x, b.y - a.y);
        }
 
        public static float Distance(Vector2 b, Vector2 a)
        {
            var result = b - a;
            return (float)Math.Sqrt((Math.Pow(result.x, 2+ Math.Pow(result.y, 2)));
        }
 
 
        public override string ToString()
        {
            return string.Format("({0},{1})"this.x, this.y);
        }
 
 
 
    }
}
 
cs




반응형

'Unity3D' 카테고리의 다른 글

Socket.IO-Client-Unity3D  (0) 2018.10.19
Sending a form to an HTTP server (POST) Using IMultipartFormSection  (0) 2018.10.19
피타고라스 정리  (0) 2018.09.06
벡터란?  (0) 2018.09.06
벡터 크기와 정규화  (0) 2018.09.06
: