-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCMyStar.cpp
More file actions
133 lines (111 loc) · 5.1 KB
/
CMyStar.cpp
File metadata and controls
133 lines (111 loc) · 5.1 KB
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
#include "pch.h"
#include "CMyStar.h"
#include <cmath> // sqrt, cos, sin 함수를 사용하기 위해 추가
// PI 값 정의 (cmath에 M_PI가 없을 수 있으므로 직접 정의)
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// CMyStar 클래스 생성자 구현
CMyStar::CMyStar(CPoint p1, CPoint p2)
{
m_p1 = p1; // 첫 번째 포인트: 별의 중심
m_p2 = p2; // 두 번째 포인트: 별의 외곽선 결정점
}
// 별을 그리는 메서드 구현
void CMyStar::draw(CDC& dc)
{
// 브러시 설정 (도형 내부를 채울 색상)
CBrush fillBrush(m_fillColor);
CBrush* pOldBrush = dc.SelectObject(&fillBrush);
// 별의 중심
int center_x = m_p1.x;
int center_y = m_p1.y;
// 별의 외곽선 결정점
int outline_x = m_p2.x;
int outline_y = m_p2.y;
// 중심과 외곽선 결정점 사이의 거리를 이용하여 바깥쪽 반지름(r_outer) 계산
long dx = outline_x - center_x;
long dy = outline_y - center_y;
double r_outer = sqrt(static_cast<double>(dx * dx + dy * dy));
// 별의 안쪽 반지름(r_inner) 계산
// 이 값은 별의 모양(뾰족함의 정도)을 조절합니다.
// 0.382는 황금비율과 관련된 값으로, 일반적인 별 모양에 적합합니다.
double r_inner = r_outer * 0.382;
// 5각 별은 총 10개의 꼭짓점(5개의 바깥쪽 뾰족한 부분, 5개의 안쪽 움푹 들어간 부분)으로 구성됩니다.
const int num_points = 10;
CPoint star_points[num_points]; // 별의 꼭짓점들을 저장할 배열
// 각 꼭짓점 사이의 각도 간격 (360도를 10개의 꼭짓점으로 나눔)
const double angle_increment_deg = 360.0 / num_points;
// 별의 가장 위쪽 뾰족한 부분이 위를 향하도록 시작 각도 설정 (-90도 = 270도)
const double start_angle_deg = -90.0;
for (int i = 0; i < num_points; ++i) {
double current_radius;
if (i % 2 == 0) { // 짝수 인덱스 (0, 2, 4, 6, 8): 바깥쪽 뾰족한 부분
current_radius = r_outer;
}
else { // 홀수 인덱스 (1, 3, 5, 7, 9): 안쪽 움푹 들어간 부분
current_radius = r_inner;
}
// 현재 꼭짓점의 각도 계산 (도 단위)
double current_angle_deg = start_angle_deg + i * angle_increment_deg;
// 각도를 라디안으로 변환 (삼각 함수는 라디안을 사용)
double current_angle_rad = current_angle_deg * M_PI / 180.0;
// 극좌표를 직교좌표로 변환하여 꼭짓점의 x, y 좌표 계산
star_points[i].x = static_cast<int>(center_x + current_radius * cos(current_angle_rad));
star_points[i].y = static_cast<int>(center_y + current_radius * sin(current_angle_rad));
}
// 계산된 꼭짓점들을 이용하여 별을 그리고 내부를 채웁니다.
dc.Polygon(star_points, num_points);
// 원래 브러시로 되돌립니다. (리소스 누수 방지)
dc.SelectObject(pOldBrush);
}
CRect CMyStar::getBoundingRect() const {
int dx = m_p2.x - m_p1.x;
int dy = m_p2.y - m_p1.y;
double r_outer = sqrt(dx * dx + dy * dy);
return CRect(
static_cast<int>(m_p1.x - r_outer),
static_cast<int>(m_p1.y - r_outer),
static_cast<int>(m_p1.x + r_outer),
static_cast<int>(m_p1.y + r_outer)
);
}
bool CMyStar::isInRect(const CRect& selectRect) const {
return selectRect.PtInRect(m_p1) || selectRect.PtInRect(m_p2);
}
bool CMyStar::isInPoint(CPoint point)
{
// draw() 메서드와 동일하게 별의 꼭짓점들 계산
int center_x = m_p1.x;
int center_y = m_p1.y;
long dx = m_p2.x - m_p1.x;
long dy = m_p2.y - m_p1.y;
double r_outer = sqrt(static_cast<double>(dx * dx + dy * dy));
double r_inner = r_outer * 0.382;
const int num_points = 10;
CPoint star_points[num_points];
const double angle_increment_deg = 360.0 / num_points;
const double start_angle_deg = -90.0;
for (int i = 0; i < num_points; ++i) {
double current_radius = (i % 2 == 0) ? r_outer : r_inner;
double current_angle_deg = start_angle_deg + i * angle_increment_deg;
double current_angle_rad = current_angle_deg * M_PI / 180.0;
star_points[i].x = static_cast<int>(center_x + current_radius * cos(current_angle_rad));
star_points[i].y = static_cast<int>(center_y + current_radius * sin(current_angle_rad));
}
// 점이 다각형(별) 내부에 있는지 확인하는 알고리즘
// Ray casting 알고리즘 사용
bool inside = false;
for (int i = 0, j = num_points - 1; i < num_points; j = i++) {
if (((star_points[i].y > point.y) != (star_points[j].y > point.y)) &&
(point.x < (star_points[j].x - star_points[i].x) * (point.y - star_points[i].y) /
(star_points[j].y - star_points[i].y) + star_points[i].x)) {
inside = !inside;
}
}
return inside;
}
void CMyStar::moveBy(int dx, int dy) {
m_p1.x += dx; m_p1.y += dy;
m_p2.x += dx; m_p2.y += dy;
}