-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentry.js
118 lines (101 loc) · 2.89 KB
/
entry.js
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
require("./styles/style.css");
let d3 = require("d3");
let $ = require("jquery");
let radius = 150;
let multiple = 2;
let iterations = 200;
let points = 100;
function dim() {
let $svg = $("svg");
return {
height: $svg.height(),
width: $svg.width()
};
}
function pointPositions(numberOfPoints) {
let results = [];
let dimensions = dim();
let x = dimensions.width / 2;
let y = dimensions.height / 2;
let incr = (2 * Math.PI) / numberOfPoints;
let range = numberOfPoints - 1;
for(let i = 1; i <= numberOfPoints; i++) {
let angle = i * incr;
results.push({
x: Math.cos(angle) * radius + x,
y: Math.sin(angle) * radius + y
});
}
return results;
}
function linePositions(pointData) {
let lineData = [];
for(let i = 0; i < iterations; i++) {
let start = i % pointData.length;
let end = (multiple * i) % pointData.length;
lineData.push({
x1: pointData[start].x,
y1: pointData[start].y,
x2: pointData[end].x,
y2: pointData[end].y
});
}
return lineData;
}
function draw() {
let data = [dim()];
let svg = d3.select("svg");
let circle = svg.selectAll("circle").data(data)
.attr("cx", (d) => d.width / 2)
.attr("cy", (d) => d.height / 2)
.attr("r", (d) => radius);
circle.enter().append("circle")
.attr("cx", (d) => d.width / 2)
.attr("cy", (d) => d.height / 2)
.attr("stroke", "black")
.attr("r", (d) => radius)
.attr("stroke-width", "2")
.attr("fill", "none");
circle.exit().remove();
let pointData = pointPositions(points);
let pointGroup = svg.append("g").attr("class", "points");
let point = pointGroup.selectAll("circle").data(pointData);
point.enter().append("circle")
.attr("cx", (d) => d.x)
.attr("cy", (d) => d.y)
.attr("r", 2)
.attr("fill", "black");
point.exit().remove();
let lineData = linePositions(pointData);
svg.select("g.lines").remove();
let lineGroup = svg.append("g").attr("class", "lines");
let line = lineGroup.selectAll("line").data(lineData)
line.enter().append("line")
.attr("x1", (d) => d.x1)
.attr("x2", (d) => d.x2)
.attr("y1", (d) => d.y1)
.attr("y2", (d) => d.y2)
.attr("stroke", "black")
.attr("stroke-width", 1);
line.exit().remove();
}
$(function() {
$("#radius").change(function() {
radius = $(this).val();
draw();
});
$("#multiple").change(function() {
multiple = $(this).val();
draw();
});
$("#iterations").change(function() {
iterations = $(this).val();
draw();
});
$("#points").change(function() {
points = $(this).val();
draw();
});
draw();
$(window).resize(function() { draw(); });
});