'피타고라스의 정리를 이용한 메소드'에 해당되는 글 1건

  1. 2019.10.24 10.24 벡터

10.24 벡터

C#/실습 2019. 10. 24. 17:31

예제:

벡터A (-5,-6)

벡터B (7,3)

을 잇는 벡터C의 크기와 성분을 나타내고 

벡터C의 단위벡터를 구하여 그 성분과 크기를 출력한다.

코드:

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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._24_step2
{
    class App
    {
        public App()
        {
            Vector2 zeroVector = new Vector2(00);
            Vector2 VectorA = new Vector2(-5-6);
            Vector2 VectorB = new Vector2(73);
            double vectorCPower = GetDistance(VectorA, VectorB);
            Console.WriteLine(vectorCPower);
            Vector2 VectorC = GetVector(VectorB, VectorA);
            Console.WriteLine("{0},{1}", VectorC.x, VectorC.y);
            Vector2 unitVectorC = GetNormalize(VectorC);
            Console.WriteLine("단위벡터:");
            Console.WriteLine("성분:{0},{1} 크기:{2}", unitVectorC.x, unitVectorC.y,GetDistance(zeroVector,unitVectorC));
        }
        public Vector2 GetNormalize(Vector2 vector)
        {
            Vector2 zeroVector = new Vector2(00);
            double distance = GetDistance(zeroVector, vector);
            Vector2 newVector = new Vector2(vector.x / distance, vector.y / distance);
            return newVector;
        }
        public Vector2 GetVector(Vector2 A , Vector2 B)
        {
            double x = A.x - B.x;
            double y = A.y - B.y;
            Vector2 vector = new Vector2(x, y);
            return vector;
        }
        public double GetDistance(Vector2 A, Vector2 B)
        {
            double a =Math.Abs(A.x - B.x);
            double b = Math.Abs(A.y - B.y);
            double c;
            c = Math.Pow(a, 2+ Math.Pow(b, 2);
            return Math.Sqrt(c);
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace _10._24_step2
{
    class Vector2
    {
        public double x;
        public double y;
        public Vector2(double x, double y)
        {
            this.x = x;
            this.y = y;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
: