-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsvg1.cpp
More file actions
72 lines (57 loc) · 2.12 KB
/
Copy pathsvg1.cpp
File metadata and controls
72 lines (57 loc) · 2.12 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
#include <iostream> // for cout, cerr
#include <cmath> // for log2
#include <string> // for string
using namespace std;
// Function to decide line color based on intersections
string colorFor(int val, int threshold) {
if (val == 0) return "green";
else if (val <= threshold) return "blue";
else return "red";
}
int main() {
int n = 7;
int perm[7] = {3, 7, 1, 5, 2, 6, 4};
int perm1[7] = {3, 7, 1, 5, 2, 6, 4};
// Count intersections (simple O(n^2))
int perSeg[7] = {0};
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if ((perm[i] - perm[j]) * (i - j) < 0) {
perSeg[i]++;
perSeg[j]++;
}
}
}
int threshold = (int)floor(log2(n));
// SVG setup
int width = 600, height = 300;
int margin = 50;
int spacing = (width - 2 * margin) / (n - 1);
int yTop = 50, yBottom = 250;
int bottomX[8], topX[8];
for (int i = 1; i <= n; i++) bottomX[i] = margin + (i - 1) * spacing;
for (int i = 0; i < n; i++) topX[perm[i]] = margin + i * spacing;
// Output SVG
cout << "<svg xmlns='http://www.w3.org/2000/svg' width='"
<< width << "' height='" << height + 40 << "'>\n";
cout << "<rect width='100%' height='100%' fill='white'/>\n";
// Draw connecting lines + numbers
for (int i = 1; i <= n; i++) {
// Line
cout << "<line x1='" << bottomX[i] << "' y1='" << yBottom
<< "' x2='" << topX[i] << "' y2='" << yTop
<< "' stroke='" << colorFor(perSeg[i - 1], threshold)
<< "' stroke-width='2'/>\n";
cout << "<line x1='" << bottomX[1]<< "' y1='" << yBottom
<< "' x2='" << bottomX[n] << "' y2='" << yBottom
<< "' stroke='black' stroke-width='1.5' />\n";
cout << "<line x1='" << topX[perm[0]]<< "' y1='" << yTop
<< "' x2='" << topX[perm[n-1]] << "' y2='" << yTop
<< "' stroke='black' stroke-width='1.5' />\n";
// Label (number at midpoint of the line)
}
cout << "</svg>\n";
// Debug marker (goes to console, not SVG if redirected)
cerr << "----- END SVG -----" << endl;
return 0;
}