-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSDF.cpp
More file actions
232 lines (189 loc) · 7.81 KB
/
Copy pathSDF.cpp
File metadata and controls
232 lines (189 loc) · 7.81 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include "SDF.h"
#include <algorithm>
using namespace std;
float SDF::circle_sdf(float x, float y, float center_x, float center_y, float radius)
{
float dx = x - center_x;
float dy = y - center_y;
float distance = sqrt(dx * dx + dy * dy);
return distance - radius;//内负外正边界0
}
float SDF::smoothstep(float edge0, float edge1, float x)
{
float t = max(0.0f, min(1.0f, (x - edge0) / (edge1 - edge0)));// 将 x 限制在 [edge0, edge1] 范围内
return t * t * (3.0f - 2.0f * t);//三次平滑曲线
}
// 混合两个颜色
COLORREF SDF::blend_color(COLORREF bg, COLORREF fg, float alpha)
{
int bg_r = GetRValue(bg);
int bg_g = GetGValue(bg);
int bg_b = GetBValue(bg);
int fg_r = GetRValue(fg);
int fg_g = GetGValue(fg);
int fg_b = GetBValue(fg);
int r = (int)(bg_r * (1.0f - alpha) + fg_r * alpha);
int g = (int)(bg_g * (1.0f - alpha) + fg_g * alpha);
int b = (int)(bg_b * (1.0f - alpha) + fg_b * alpha);
return RGB(r, g, b);
}
//抗锯齿实心圆
void SDF::draw_aa_circle(int center_x, int center_y, int radius, COLORREF color)
{
int aa_width = 2; // 抗锯齿宽度
for (int y = center_y - radius - aa_width; y <= center_y + radius + aa_width; y++)
{
for (int x = center_x - radius - aa_width; x <= center_x + radius + aa_width; x++)
{
float sdf = circle_sdf((float)x, (float)y, (float)center_x, (float)center_y, (float)radius);
// 根据 SDF 值计算 alpha(边界附近平滑过渡)
// sdf < -1: 完全不透明 (alpha=1)
// sdf = 0: 半透明 (alpha=0.5)
// sdf > 1: 完全透明 (alpha=0)
float alpha = 1.0f - smoothstep(-1.0f, 1.0f, sdf);
if (alpha > 0.01f)//只绘制有明显颜色的像素
{
// 读取当前像素颜色作为背景色,确保混合正确
COLORREF bg_color = getpixel(x, y);
COLORREF blended_color = blend_color(bg_color, color, alpha);
putpixel(x, y, blended_color);
}
}
}
}
//抗锯齿空心圆环
void SDF::draw_aa_ring(int center_x, int center_y, int radius, int thickness, COLORREF color)
{
int aa_width = 2; // 抗锯齿宽度
int outer_radius = radius + thickness / 2;
int inner_radius = radius - thickness / 2;
for (int y = center_y - outer_radius - aa_width; y <= center_y + outer_radius + aa_width; y++)
{
for (int x = center_x - outer_radius - aa_width; x <= center_x + outer_radius + aa_width; x++)
{
float dx = (float)x - center_x;
float dy = (float)y - center_y;
float dist = sqrt(dx * dx + dy * dy);
float sdf_outer = dist - outer_radius;//到外圆的距离
float sdf_inner = inner_radius - dist;//到内圆的距离
float sdf = max(sdf_outer, sdf_inner);//圆环SDF
float alpha = 1.0f - smoothstep(-1.0f, 1.0f, sdf);
if (alpha > 0.01f)
{
// 读取当前像素颜色作为背景色
COLORREF bg_color = getpixel(x, y);
COLORREF pixel_color = blend_color(bg_color, color, alpha);
putpixel(x, y, pixel_color);
}
}
}
}
//抗锯齿圆弧(用于进度条)
//start_angle: 起始角度(度),0度是3点钟方向,逆时针为正
//sweep_angle: 扫描角度(度),正数顺时针,负数逆时针
void SDF::draw_aa_arc(int center_x, int center_y, int radius, int thickness,
double start_angle, double sweep_angle, COLORREF color)
{
const double PI = 3.14159265358979323846;
int aa_width = 2;
int outer_radius = radius + thickness / 2;
int inner_radius = radius - thickness / 2;
// 计算起始和结束角度(弧度)
double start_rad = start_angle * PI / 180.0;
double end_rad = (start_angle + sweep_angle) * PI / 180.0;
// 确保 start_rad <= end_rad(用于范围判断)
if (start_rad > end_rad)
{
double temp = start_rad;
start_rad = end_rad;
end_rad = temp;
}
for (int y = center_y - outer_radius - aa_width; y <= center_y + outer_radius + aa_width; y++)
{
for (int x = center_x - outer_radius - aa_width; x <= center_x + outer_radius + aa_width; x++)
{
float dx = (float)x - center_x;
float dy = (float)y - center_y;
float dist = sqrt(dx * dx + dy * dy);
// 计算圆环SDF
float sdf_outer = dist - outer_radius;
float sdf_inner = inner_radius - dist;
float ring_sdf = max(sdf_outer, sdf_inner);
// 计算当前像素的角度
double pixel_angle = atan2((double)dy, (double)dx);
// 检查是否在圆弧范围内
bool in_arc = false;
// 处理跨越-PI/PI边界的情况
double angle_range = end_rad - start_rad;
if (angle_range > PI * 2) angle_range = PI * 2;
// 标准化pixel_angle使其与start_rad可比较
double normalized_angle = pixel_angle;
// 将normalized_angle调整到start_rad附近
while (normalized_angle < start_rad - PI) normalized_angle += 2 * PI;
while (normalized_angle > start_rad + PI) normalized_angle -= 2 * PI;
// 再次调整确保在正确范围
while (normalized_angle < start_rad) normalized_angle += 2 * PI;
if (normalized_angle >= start_rad && normalized_angle <= end_rad)
{
in_arc = true;
}
// 也检查减去2PI后的情况
normalized_angle -= 2 * PI;
if (normalized_angle >= start_rad && normalized_angle <= end_rad)
{
in_arc = true;
}
if (!in_arc) continue;
float alpha = 1.0f - smoothstep(-1.0f, 1.0f, ring_sdf);
if (alpha > 0.01f)
{
COLORREF bg_color = getpixel(x, y);
COLORREF pixel_color = blend_color(bg_color, color, alpha);
putpixel(x, y, pixel_color);
}
}
}
}
//计算点到线段的SDF
float SDF::line_sdf(float x, float y, float x1, float y1, float x2, float y2)
{
float dx = x2 - x1;
float dy = y2 - y1;
float len_sq = dx * dx + dy * dy;
if (len_sq < 0.0001f) // 线段退化为点
return sqrt((x - x1) * (x - x1) + (y - y1) * (y - y1));
// 计算点到线段的投影参数 t
float t = max(0.0f, min(1.0f, ((x - x1) * dx + (y - y1) * dy) / len_sq));
// 计算投影点
float proj_x = x1 + t * dx;
float proj_y = y1 + t * dy;
// 返回点到投影点的距离
return sqrt((x - proj_x) * (x - proj_x) + (y - proj_y) * (y - proj_y));
}
//抗锯齿线段
void SDF::draw_aa_line(int x1, int y1, int x2, int y2, int thickness, COLORREF color)
{
int aa_width = 2; // 抗锯齿宽度
float half_thickness = thickness / 2.0f;
// 计算边界框
int min_x = min(x1, x2) - thickness - aa_width;
int max_x = max(x1, x2) + thickness + aa_width;
int min_y = min(y1, y2) - thickness - aa_width;
int max_y = max(y1, y2) + thickness + aa_width;
for (int y = min_y; y <= max_y; y++)
{
for (int x = min_x; x <= max_x; x++)
{
float dist = line_sdf((float)x, (float)y, (float)x1, (float)y1, (float)x2, (float)y2);
float sdf = dist - half_thickness;
float alpha = 1.0f - smoothstep(-1.0f, 1.0f, sdf);
if (alpha > 0.01f)
{
// 读取当前像素颜色作为背景色
COLORREF bg_color = getpixel(x, y);
COLORREF pixel_color = blend_color(bg_color, color, alpha);
putpixel(x, y, pixel_color);
}
}
}
}