Game

Box shooting

https://simmer.io/@SagacityJang/box-shooting1

Avoid bomb

https://simmer.io/@SagacityJang/avoid-bomb

 

'작품 > 포트폴리오' 카테고리의 다른 글

포트폴리오  (0) 2019.10.17

 

입력

 

최단거리 찾기 완료

 

source code :

https://github.com/gkagm2/cpppractise/blob/master/cpptraining/ch1/Algorithm/BFS/bfsSimulation.cpp

 

백준에서 

내가 만든 코드와 비교하고 있는 와중에 아래와 같이 cin>>n; 후에 int a[n]={};이 된다는 것을 봤다.! 심지어 for문을 보면 a[i] =t;가 적혀져있다..

#include <iostream>

using namespace std;

int main()
{
    int n;
    cin>>n;
    int a[n]={};
    int m=0;
    int t=0;
    for(int i=0; i<n; i++)
    {
        cin>>t;
        if (t > m)
        {
            m=t;
        }
            a[i]=t;
    }

    float s=0;
    for(int i=0; i<n; i++)
    {
        //cout<<s<<endl;
        s=s+(float)a[i]/(float)m*100;
        //cout<<s<<endl;
    }
    cout<<s/(float)n;
    return 0;
}

처음에 내 눈을 의심했다. 안되는걸로 알고 있는데 맞았습니다!!로 뜬다는것에.. 이런 방식도 지원을 하는건가?

VS말고 다른 ide에서는 가변크기배열 된다고들었다는 사람도 있고 c++ 최신 문법일수도 있다고 한다.

문제의 소스코드 링크

https://www.acmicpc.net/source/16987090

 

로그인

 

www.acmicpc.net

 

 

이것저것 찾다보니 답을 스택오버플로우에서 찾았다.

https://stackoverflow.com/questions/40633344/variable-length-arrays-in-c14

 

 

Sphere끼리 충돌 구현 실험.

 

간단하게 실험을 하기 위해 두 개의 구만 충돌이 일어나게 하였다.

닿았을 때

 

 

 

구의 충돌은 구하기 쉽다. 두 점 사이의 거리가 양쪽 구의 반지름들을 더한 값보다 작으면 충돌했다고 판정된다.

아래는 소스코드.

만드는김에 제곱근, 제곱, 두 점 사이의 거리를 구해주는 함수도 직접 만들어봄.

public class CSphereCollider : MonoBehaviour
{
    public Vector3 pivotPos;
    public float radius; // 반지름

    public GameObject otherSphere;

    private Vector3 otherPivotPos;
    private float otherRadius; // 상대방 반지름
    
    // Update is called once per frame
    void Update()
    {
        transform.localScale = new Vector3(radius*2, radius*2, radius*2);
        pivotPos = gameObject.transform.position;
        Vector3 otherPivotPos = otherSphere.gameObject.transform.position;
        otherRadius = otherSphere.gameObject.GetComponent<CSphereCollider>().radius;

            
        CheckCollider(pivotPos, otherPivotPos);
    }

    public void CheckCollider(Vector3 pos1, Vector3 pos2)
    {
        float distance = GetDistance(pos1, pos2);
        // 충돌이 일어났다.
        if (distance < radius + otherRadius)
        {
            Debug.Log("충돌이 일어남");
        }
    }

    // 유클리드 거리, 두 점 사이의 거리를 구한다.
    public float GetDistance(Vector3 currentPos, Vector3 targetPos)
    {
        float distance;
        float _x = targetPos.x - currentPos.x;
        float _y = targetPos.y - currentPos.y;
        float _z = targetPos.z - currentPos.z;
        distance = GetSQRT(GetPow(_x, 2) + GetPow(_y, 2) + GetPow(_z, 2));

        return distance;
    }

    /// <summary>
    /// 제곱을 구해준다.
    /// </summary>
    /// <param name="number">숫자</param>
    /// <param name="n">지수</param>
    /// <returns>제곱된 수</returns>
    public float GetPow(float number, int n)
    {
        float value = 1;
        for(int i=0;i  < n; ++i)
        {
            value *= number;
        }
        return value;
    }

    // 제곱근을 구해준다.
    public float GetSQRT(float value)
    {
        // 바빌로니아 법을 이용한다.
        float x = 3; // 임의의 값.
        
        // 정확도가 높아질때까지 반복
        for(int i =0;i < 10; ++i)
        {
            x = (x + (value / x)) / 2;
        }
        return x;
    }

    private void OnDrawGizmos()
    {
        Gizmos.DrawWireSphere(pivotPos, radius);
    }
}

 

+ Recent posts