안녕하세요 엘체프 입니다.

오늘 공부한 클래스 실습과제 임돠


Q.사각형 영역을 저장하고 처리하는 클래스 만드세요. 파워포인트나 포토샵 같은 프로그램에서 마우스를 드래그해서 사각형 영역을 선택하고, 선택된 영역을 다시 마우스로 드래그해서 이동하거나 크기를 조절하는 등의 조작을 해 보았을 겁니다. 이런 프로그램을 만들 때 활용할 수 있을 법한 클래스를 만들고자 하는 것입니다


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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#pragma once
class Region
{
public:
    Region(void);            // 생성자
    Region(int l, int t, int r, int b);    // 생성자
 
private:
    /////////////////////////////////////////////////////////////
    // 사각형 영역을 저장하기 위한 멤버변수들
    int left;                // 시작점의 x좌표
    int top;                // 시작점의 y좌표
    int right;                // 끝점의 x좌표
    int bottom;                // 끝점의 y좌표
 
    /////////////////////////////////////////////////////////////
    // 출력방식을 지정하기 위한 멤버변수
    static int notation;            // POINT_POINT 또는 POINT_SIZE
public:
    enum { POINT_POINT, POINT_SIZE };    // 시작점과 끝점, 시작점과 길이
 
public:
    /////////////////////////////////////////////////////////////
    // 사각형 영역에 대한 정보를 얻는 함수들
    int GetWidth() const;                    // 가로길이 얻기
    int GetHeight() const;                    // 세로길이 얻기
    void GetStartPoint(int &x, int &y) const;        // 시작점 얻기
    void GetEndPoint(int &x, int &y) const;        // 끝점 얻기
    void GetCenterPoint(int &x, int &y) const;        // 중심점 얻기
    bool IsPointInRegion(int x, int y) const;        // 점이 영역 안에 있는지
 
    /////////////////////////////////////////////////////////////
    // 사각형 영역을 설정하고 처리하는 함수들
    void SetRect(int l, int t, int r, int b);                // 영역 설정
    void Move(int x, int y);                        // 위치 이동
    void Resize(int width, int height);                    // 크기 변경
    void UnionRegion(const Region &r1, const Region &r2);        // 교집합
    void IntersectRegion(const Region &r1, const Region &r2);    // 합집합
 
    /////////////////////////////////////////////////////////////
    // 사각형 영역을 표시하기 위한 함수들
    void Show(void);                        // 영역 출력
    static void SetNotation(int notation);        // 출력 방식 지정
};
-----------------------------------------------------------------------------------
 
#include <stdio.h>
#include <iostream>
//c언어의 stude.h와 같은 역활의 header
#include "Region.h"
 
int Region::notation = Region::POINT_POINT;
 
Region::Region(void)
{
    SetRect(0000);    // 영역을 0으로 초기화
}
Region::Region(int l, int t, int r, int b)
{
    SetRect(l, t, r, b);    // 영역을 주어진 값으로 초기화
}
 
void Region::SetRect(int l, int t, int r, int b)
{
    left = l;            // 시작점의 x좌표 설정
    top = t;            // 시작점의 y좌표 설정
    right = r;            // 끝점의 x좌표 설정
    bottom = b;            // 끝점의 y좌표 설정
}
int Region::GetWidth() const
{
    return right - left;
}
int Region::GetHeight() const
{
    return bottom - top;
}
void Region::GetStartPoint(int &x, int &y) const
{
    x = left;        // 시작점의 x 좌표 얻기
    y = top;        // 시작점의 y좌표 얻기
}
void Region::GetEndPoint(int &x, int &y) const
{
    x = right;        // 끝점의 x 좌표 얻기
    y = bottom;        // 끝점의 y좌표 얻기
}
void Region::GetCenterPoint(int &x, int &y) const
{
    x = (left + right) / 2;    // 중심점의 x 좌표 얻기
    y = (top + bottom) / 2;    // 중심점의 y좌표 얻기
}
 
bool Region::IsPointInRegion(int x, int y) const
{
    return (x >= left && x <= right && y >= top && y <= bottom);
}
void Region::Move(int x, int y)
{
    left += x;            // 시작점의 x좌표 이동
    right += x;            // 끝점의 x좌표 이동
    top += y;            // 시작점의 y좌표 이동
    bottom += y;        // 끝점의 y좌표 이동
}
 
void Region::Resize(int width, int height)
{
    int x, y;
    GetCenterPoint(x, y);    // 중심점 좌표 얻기
    left = x - width / 2;    // 중심으로부터 가로길이의 절반만큼 이동
    top = y - height / 2;    // 중심으로부터 세로길이의 절반만큼 이동
    right = left + width;    // 가로길이가 width가 되도록 조절
    bottom = top + height;    // 세로길이가 height가 되도록 조절
}
void Region::UnionRegion(const Region &r1, const Region &r2)
{
    left = r1.left > r2.left ? r1.left : r2.left;
    right = r1.right < r2.right ? r1.right : r2.right;
    top = r1.top > r2.top ? r1.top : r2.top;
    bottom = r1.bottom < r2.bottom ? r1.bottom : r2.bottom;
    if (left >= right || top >= bottom)      // 교집합이 존재하지 않는 경우
    {
        left = top = right = bottom = 0;
    }
}
 
void Region::IntersectRegion(const Region &r1, const Region &r2)
{
    left = r1.left < r2.left ? r1.left : r2.left;
    right = r1.right > r2.right ? r1.right : r2.right;
    top = r1.top < r2.top ? r1.top : r2.top;
    bottom = r1.bottom > r2.bottom ? r1.bottom : r2.bottom;
}
void Region::SetNotation(int n)
{
    notation = n;
}
 
void Region::Show(void)
{
    if (notation == POINT_POINT)    // 시작점/끝점 형식
    {
        printf("(%d, %d), (%d, %d)\n", left, top, right, bottom);
    }
    else                    // 시작점/가로, 세로/길이 형식
    {
        printf("(%d, %d), [%d x %d]\n", left, top, GetWidth(), GetHeight());
    }
}
 
------------------------------------------------------------------------------------------
 
#include <iostream>
#include "Region.h"
 
using namespace std;
 
int main(void)
{
    Region::SetNotation(Region::POINT_POINT);    // 출력 형식 지정
    Region r1(1010100100);        // 영역 r1 선언
    Region r2(5050120120);        // 영역 r2 선언
    Region r3;
 
    r3.IntersectRegion(r1, r2);        // r1과 r2의 교집합을 r3에 저장
    r3.Show();
    r3.UnionRegion(r1, r2);            // r1과 r2의 합집합을 r3에 저장
    r3.Show();
    r3.Move(100100);            // r3의 위치 이동
    r3.Show();
    r3.Resize(200200);            // r3의 크기 변경
    r3.Show();
 
    Region::SetNotation(Region::POINT_SIZE);    // 출력 형식 변경
    r1.Show();
    r2.Show();
    r3.Show();
 
    system("PAUSE");
    return 0;
}
cs



이상 감사합니다.


'스터디 > C,C++' 카테고리의 다른 글

마지막 C 시험문제 공백 사각찍기  (7) 2018.02.28
C++ 문자열 경사면 인쇄하기  (9) 2018.02.19
C++ 클래스 응용 소스  (6) 2018.02.19
C 문자열 거꾸로 출력하기  (8) 2018.02.12
C 문자열 자르기 문제  (7) 2018.02.12

+ Recent posts