-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKoch_Curve.cpp
More file actions
76 lines (60 loc) · 2.44 KB
/
Koch_Curve.cpp
File metadata and controls
76 lines (60 loc) · 2.44 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
#include <bits/stdc++.h>
#include <iomanip>
using namespace std;
typedef long long llong;
static const double pi = 3.141592653589793;
// 再帰的にコッホ曲線の座標を求める
// 関数の初めでdepthを-1するよりも
// 再帰で渡すときに-1しておけばよい
void CalcKochPoint(vector<pair<long double, long double>> point, int depth) {
vector<pair<long double, long double>> nextpoint(100);
pair<long double, long double> s, t, u;
if (depth == 0) return;
s.first = (2 * point[0].first + point[1].first) / 3;
s.second = (2 * point[0].second + point[1].second) / 3;
t.first = (point[0].first + 2 * point[1].first) / 3;
t.second = (point[0].second + 2 * point[1].second) / 3;
u.first = (t.first - s.first) * cos(pi/3) - (t.second - s.second) * sin(pi/3) + s.first;
u.second = (t.first - s.first) * sin(pi / 3) + (t.second - s.second) * cos(pi / 3) + s.second;
// // 線分(P1,s)について求める
nextpoint[0].first = point[0].first;
nextpoint[0].second = point[0].second;
nextpoint[1].first = s.first;
nextpoint[1].second = s.second;
CalcKochPoint(nextpoint, depth-1);
std::cout << s.first << setprecision(15) << " " << s.second << setprecision(15) <<"\n";
// 線分(s,u)について求める
nextpoint[0].first = s.first;
nextpoint[0].second = s.second;
nextpoint[1].first = u.first;
nextpoint[1].second = u.second;
CalcKochPoint(nextpoint, depth-1);
std::cout << u.first << setprecision(15) <<" " << u.second << setprecision(15) <<"\n";
// 線分(u,t)について求める
nextpoint[0].first = u.first;
nextpoint[0].second = u.second;
nextpoint[1].first = t.first;
nextpoint[1].second = t.second;
CalcKochPoint(nextpoint, depth-1);
std::cout << t.first << setprecision(15) <<" " << t.second <<setprecision(15) << "\n";
// 線分(t,p2)について求める
nextpoint[0].first = t.first;
nextpoint[0].second = t.second;
nextpoint[1].first = point[1].first;
nextpoint[1].second = point[1].second;
CalcKochPoint(nextpoint, depth-1);
}
int main() {
vector<pair<long double, long double>> point(100);
// 始点座標
point[0].first = 0;
point[0].second = 0;
// 終点座標
point[1].first = 100;
point[1].second = 0;
int n, k;
cin >> n;
std::cout << point[0].first << " " << point[0].second << "\n";
CalcKochPoint(point, n);
std::cout << point[1].first << " " << point[1].second << "\n";
}