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;
namespace _10._24_step2
{
class App
{
public App()
{
Vector2 zeroVector = new Vector2(0, 0);
Vector2 VectorA = new Vector2(-5, -6);
Vector2 VectorB = new Vector2(7, 3);
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(0, 0);
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 c;
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;
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
|
'C# > 실습' 카테고리의 다른 글
10.23 Stack 활용하기 (0) | 2019.10.24 |
---|---|
10.23 배열에서 숫자이동시키기 (0) | 2019.10.24 |
2048 프로그램 (저장기능x) (0) | 2019.10.23 |
10.17 상속 클래스를 이용한 뽑기박스 (0) | 2019.10.17 |
10.15 Dictionary 활용해 업적 보상 UIPanel 만들기 (0) | 2019.10.15 |