b427. 漸層色彩

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

給予兩個顏色的像素,在一張空白影像中使用漸層效果。

  • 水平由左到右的漸層
  • 從中心點擴散的漸層

Sample Input

1
3 3 0 0 0 0 255 255 255 255 255

Sample Output

1
2
3
4
3 3
0 0 0 255 128 128 128 255 255 255 255 255
0 0 0 255 128 128 128 255 255 255 255 255
0 0 0 255 128 128 128 255 255 255 255 255

Solution

線性內插,特別小心中心點 (x, y) 會是一個浮點數情況。

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
#include <bits/stdc++.h>
using namespace std;
class IMAGE {
public:
struct Pixel {
double r, g, b, a;
Pixel(double x = 0, double y = 0, double z = 0, double w = 0):
r(x), g(y), b(z), a(w) {}
void read() {
scanf("%lf %lf %lf %lf", &r, &g, &b, &a);
}
Pixel operator-(const Pixel &x) const {
return Pixel(r-x.r, g-x.g, b-x.b, a-x.a);
}
Pixel operator+(const Pixel &x) const {
return Pixel(r+x.r, g+x.g, b+x.b, a+x.a);
}
Pixel operator*(const double x) const {
return Pixel(r*x, g*x, b*x, a*x);
}
Pixel operator/(const double x) const {
return Pixel(r/x, g/x, b/x, a/x);
}
void print() {
printf("%d %d %d %d", (int)round(r), (int)round(g), (int)round(b), (int)round(a));
}
};
int W, H;
static const int MAXN = 300;
Pixel data[MAXN][MAXN];
void read() {
scanf("%d %d", &W, &H);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
data[i][j].read();
}
void alloc() {
int TYPE;
Pixel st, ed;
scanf("%d %d %d", &W, &H, &TYPE);
st.read(), ed.read();
gradient(st, ed, TYPE);
}
void gradient(Pixel st, Pixel ed, int TYPE) {
if (TYPE != 0 && TYPE != 1)
return;
if (TYPE == 0) {
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
data[i][j] = W-1 ? st + (ed - st) * j / (W-1) : st;
} else {
double cx = (H-1)/2.0, cy = (W-1)/2.0;
double cr = hypot(cx, cy);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
data[i][j] = cr ? st + (ed - st) * hypot(i-cx, j-cy) / cr : st;
}
}
void print() {
printf("%d %d\n", W, H);
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++)
data[i][j].print(), printf("%c", j == W-1 ? '\n' : ' ');
}
} test;
int main() {
test.alloc();
test.print();
return 0;
}