랜덤으로 사각형이나 원을 10개 생성하여 충돌하였는지 체크하고 각각 몇 번 충돌했는지 반환하는 프로그램

기본적인 x, y값은 상속받아 그대로 쓰고, 

충돌함수는 가상함수로서 재정의하여 도형에 맞게 충돌체크.


부모인 Point의 헤더파일과 cpp파일

1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
class point
{
protected:
    int x, y;
public:
    point();
    ~point();
 
    int name;
    virtual int collision(int x, int y) = 0;
};
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "point.h"
#include <iostream>
 
point::point()
{
    x = rand() % 5;
    y = rand() % 5;
    name = 0;
}
 
point::~point()
{
}
cs



상속받아 만든 Rectangle와 Circle 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "point.h"
 
#pragma once
class rectangle : public point
{
private:
    int w, h;
public:
    rectangle();
    ~rectangle();
 
    int collision(int x, int y);
};
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
#include "stdafx.h"
#include "rectangle.h"
#include <iostream>
using namespace std;
 
rectangle::rectangle()
{
    w = rand() % 5 + 1;
    h = rand() % 5 + 1;
    name = 1;
}
 
 
rectangle::~rectangle()
{
}
 
int rectangle::collision(int a, int b)
{
    int check = 0;
    if (x < a && a < (x + w))
    {
        if (y < b && b < (y + h))
        {
            check = 1;
        }
    }    
    return check;
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "point.h"
 
#pragma once
class circle : public point
{
private:
    int r;
public:
    circle();
    ~circle();
 
    int collision(int x, int y);
};
 
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
#include "stdafx.h"
#include "circle.h"
#include <iostream>
using namespace std;
 
 
circle::circle()
{
    r = rand() % 5 + 1;
    name = 2;
}
 
 
circle::~circle()
{
}
 
int circle::collision(int a, int b)
{
    //cout << "원" << x << " " << y << " " << r << endl;
    int check = 0;
    float length = sqrt(pow(x - a, 2+ pow(y - b, 2));
    
    if (length < r)
        check = 1;
 
    return check;
}
cs



메인 cpp파일

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "stdafx.h"
#include "point.h"
#include "rectangle.h"
#include "circle.h"
#include <time.h>
#include <iostream>
using namespace std;
 
 
int main()
{
    int sum1 = 0;
    int sum2 = 0;
 
    srand(time(0));
    point* t[10];
    int a[10];
 
    for (int i = 0; i < 10; i++)
    {
        a[i] = rand() % 2;
        // 랜덤으로 사각형이나 원 생성
        if (a[i])
            t[i] = new rectangle;
        else
            t[i] = new circle;
    }
 
    for (int i = 0; i < 10; i++)
    {
        int x = 0;
        int y = 0;
        cout << "x, y를 입력하세요 : ";
        cin >> x >> y;
 
        for (int j = 0; j < 10; j++)
        {
            if (t[j]->name)
            {
                if (a[j])            
                {
                    sum1 += t[j]->collision(x, y);
 
                    if (t[j]->collision(x, y))
                    {
                        cout << "사각형 충돌 \n";
                        t[j]->name = 0;
                    }
                }
                else
                {
                    sum2 += t[j]->collision(x, y);
                    
                    if (t[j]->collision(x, y))
                    {
                        cout << "원 충돌 \n";
                        t[j]->name = 0;
                    }
                }
            }
        }
    }
 
    for (int j = 0; j < 10; j++)
    {
        delete t[j];
    }
 
    cout << "사각형 : " << sum1 << "번 충돌" << endl;
    cout << "원 : " << sum2 << "번 충돌" << endl;    
 
    return 0;
}
cs


'프로그래밍 공부 > C++' 카테고리의 다른 글

2중 포인터  (0) 2018.03.21
콘솔 미니 RPG게임 (상속 예제)  (1) 2018.03.21
상속, 오버라이딩  (0) 2018.03.21
문자열 연결 및 비교(연산자 오버로딩 예제)  (0) 2018.03.21
연산자 오버로딩  (0) 2018.03.21
Posted by misty_
,