-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.cpp
More file actions
98 lines (65 loc) · 1.82 KB
/
Copy pathtask1.cpp
File metadata and controls
98 lines (65 loc) · 1.82 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
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
struct punto {
int n, i, j;
double x, y;
};
struct arco {
int e, n1, n2;
};
int main(int argc, char* argv[]) { // si deve passare N: "./task1 N"
// generazione griglia
int N = atoi(argv[1]);
double h = 1.0/(N+1);
vector<punto> punti;
int n = 0;
for (int i=1; i<=N; i++) { //i e j da 1 a N per escudere i punti di bordo
for (int j=1; j<=N; j++) {
punto P; // creo nuovo punto
P.n = n;
P.i = i;
P.j = j;
P.x = i*h;
P.y = j*h;
punti.push_back(P); // aggiungo a vector
n++; // aggiorna indice
}
}
// scrittura su file
ofstream fileCoord("coords.txt");
for (const punto& p : punti) {
fileCoord << p.n << " " << p.i << " " << p.j << " " << p.x << " " << p.y << endl;
}
fileCoord.close();
// generazione archi
int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};
vector<arco> archi;
int e = 0;
for (const punto& p : punti) {
for (int k=0; k<4; k++) {
int iv = p.i + di[k];
int jv = p.j + dj[k];
if (iv >= 1 && iv <= N && jv >= 1 && jv <= N) {
int nv = (iv - 1)*N + (jv - 1);
if (p.n<nv) {
arco A;
A.e = e;
A.n1 = p.n;
A.n2 = nv;
archi.push_back(A); // aggiungo a vector
e++; // aggiorna indice
}
}
}
}
// scrittura su file
ofstream fileConn("connectivity.txt");
for (const arco& a : archi) {
fileConn << a.e << " " << a.n1 << " " << a.n2 << endl;
}
fileConn.close();
return 0;
}