본문 바로가기
Programing/C#

게임 프로그래밍에서 유용하게 쓰이는 함수

by 고니피즈 2017. 2. 15.
반응형

각도계(Degree) 를 호도법(Radian) 으로

public static Double DegreeToRadian(Double deg) {
    return deg * (Math.PI / 180);
}



II. 호도법(Radian) 을 각도계(Degree) 로

public static Double RadianToDegree(Double rad) {
    return rad * (180 / Math.PI);
}



III. 두 점 사이의 거리 구하기

public static Double GetDistance(PointF p1, PointF p2) {
    Double xdf = p2.X - p1.X;
    Double ydf = p2.Y - p1.Y;
    return Math.Sqrt(Math.Pow(xdf, 2) + Math.Pow(ydf, 2));
}



IV. 두 점간의 각도 구하기

public static Double GetAngle(PointF p1, PointF p2) {
    Double xdf = p2.X - p1.X;
    Double ydf = p2.Y - p1.Y;

    Double ang = RadianToDegree(Math.Atan2(ydf, xdf));
    return ang + 90;
}



V. 각도의 방향 구하기

public static PointF GetDirection(Double deg) {
    return new PointF(-(Single)Math.Sin(DegreeToRadian(deg)),
                      (SingleMath.Cos(DegreeToRadian(deg)));
}



출처: http://slaner.tistory.com/109 [꿈꾸는 프로그래머]

반응형