Based on this question : Question, I tried to create a flower of life.
So far I got the 7 first petals but to create the rest of the flower (19 petals) I’m stuck because I can’t figure out what I’m doing wrong.
I’m not so good in Maths and don’t exactly understand how to proceed.
Code so far (Limited to 8 circles):
JavaScript
x
61
61
1
window.addEventListener('load', function () {
2
let distanceX = 75,
3
distanceY = 75,
4
angle = 60,
5
circles = 8;
6
for (var i = 0; i < circles; i++) {
7
let div = document.createElement('div');
8
div.id = 'c' + i;
9
div.className = 'circle';
10
document.getElementById('fol').appendChild(div);
11
let circle = document.getElementById('c' + i);
12
if (i == 0) {
13
circle.style.transform = "translate(" + 0 + "px," + 0 + "px)";
14
} else {
15
if (i > 6) {
16
let j = i - 2,
17
k = i - 1;
18
let previouscircle = document.getElementById('c' + j).getBoundingClientRect();
19
let lastcircle = document.getElementById('c' + k).getBoundingClientRect();
20
var x0 = previouscircle.left;
21
var y0 = previouscircle.top;
22
var x1 = lastcircle.left;
23
var y1 = lastcircle.top;
24
25
console.log("previous (" + j + ") " + x0 + " " + y0);
26
console.log("last (" + k + ") " + x1 + " " + y1);
27
28
function intersection(x0, y0, r0, x1, y1, r1) {
29
var a, dx, dy, d, h, rx, ry;
30
var x2, y2;
31
32
dx = x1 - x0;
33
dy = y1 - y0;
34
d = Math.sqrt((dy * dy) + (dx * dx));
35
a = ((r0 * r0) - (r1 * r1) + (d * d)) / (2.0 * d);
36
x2 = x0 + (dx * a / d);
37
y2 = y0 + (dy * a / d);
38
h = Math.sqrt((r0 * r0) - (a * a));
39
rx = -dy * (h / d);
40
ry = dx * (h / d);
41
42
var xi = x2 + rx;
43
var xi_prime = x2 - rx;
44
var yi = y2 + ry;
45
var yi_prime = y2 - ry;
46
47
return [xi, xi_prime, yi, yi_prime];
48
}
49
let result = intersection(x0, y0, distanceX, x1, y1, distanceY);
50
let resultX = result[1];
51
let resultY = result[3];
52
circle.style.left = resultX + "px";
53
circle.style.top = resultY + "px";
54
}
55
circle.style.transform = "translate(" + distanceX * Math.cos(Math.PI / 180 * angle * i) + "px," + distanceY * Math.sin(Math.PI / 180 * angle * i) + "px)";
56
}
57
58
let limits = circle.getBoundingClientRect();
59
console.log("c" + i + " " + limits.left + " " + limits.top);
60
}
61
});
JavaScript
1
21
21
1
body {
2
margin: 0;
3
background-color: black;
4
}
5
6
.container {
7
width: 100vw;
8
height: 100vh;
9
display: flex;
10
justify-content: center;
11
align-items: center;
12
}
13
14
.circle {
15
width: 150px;
16
height: 150px;
17
position: absolute;
18
border-radius: 100%;
19
background-color: rgba(255, 255, 255, 0.1);
20
box-shadow: 0 0 10px rgba(255, 255, 255, 1);
21
}
JavaScript
1
22
22
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<meta charset="utf-8" />
6
<title>Flower of life</title>
7
<!--
8
<link rel="stylesheet" type="text/css" href="fol.css" />
9
-->
10
</head>
11
12
<body>
13
<div class="background">
14
<div id="fol" class="container">
15
</div>
16
</div>
17
<!--
18
<script src="fol.js"></script>
19
-->
20
</body>
21
22
</html>
Result I try to achieve:
Advertisement
Answer
Code cleaned up 🙂
JavaScript
1
59
59
1
window.addEventListener('load', function () {
2
let distanceX = 75,
3
distanceY = 75,
4
angle = 60,
5
circles = 19;
6
for (var i = 0; i <= circles; i++) {
7
let div = document.createElement('div');
8
div.id = 'c' + i;
9
div.className = 'circle';
10
document.getElementById('fol').appendChild(div);
11
let circle = document.getElementById('c' + i);
12
if (i == 0) {
13
circle.style.transform = "translate(" + 0 + "px," + 0 + "px)";
14
} else {
15
circle.style.transform = "translate(" + distanceX * Math.cos(Math.PI / 180 * angle * i) + "px," + distanceY * Math.sin(Math.PI / 180 * angle * i) + "px)";
16
}
17
}
18
function intersection(x0, y0, r0, x1, y1, r1) {
19
var a, dx, dy, d, h, rx, ry;
20
var x2, y2;
21
22
dx = x1 - x0;
23
dy = y1 - y0;
24
d = Math.sqrt((dy * dy) + (dx * dx));
25
a = ((r0 * r0) - (r1 * r1) + (d * d)) / (2.0 * d);
26
x2 = x0 + (dx * a / d);
27
y2 = y0 + (dy * a / d);
28
h = Math.sqrt((r0 * r0) - (a * a));
29
rx = -dy * (h / d);
30
ry = dx * (h / d);
31
var xi = x2 + rx;
32
var yi = y2 + ry;
33
return [xi, yi];
34
}
35
for (var f = 7; f <= 18; f++) {
36
let j = f,
37
k = f + 1;
38
let previouscircle = document.getElementById('c' + j).getBoundingClientRect();
39
let lastcircle = document.getElementById('c' + k).getBoundingClientRect();
40
var x0 = previouscircle.left;
41
var y0 = previouscircle.top;
42
var x1 = lastcircle.left;
43
var y1 = lastcircle.top;
44
if (f < 13) {
45
distanceX = 75;
46
distanceY = 0.1;
47
} else {
48
distanceX = 0.1;
49
distanceY = 75;
50
}
51
let result = intersection(x0, y0, distanceX, x1, y1, distanceY);
52
let resultX = result[0];
53
let resultY = result[1];
54
let circle = document.getElementById('c' + f);
55
circle.style.left = resultX + "px";
56
circle.style.top = resultY + "px";
57
}
58
document.getElementById('c' + circles).remove();
59
});
JavaScript
1
21
21
1
body {
2
margin: 0;
3
background-color: black;
4
}
5
6
.container {
7
width: 100vw;
8
height: 100vh;
9
display: flex;
10
justify-content: center;
11
align-items: center;
12
}
13
14
.circle {
15
width: 150px;
16
height: 150px;
17
position: absolute;
18
border-radius: 100%;
19
background-color: rgba(255, 255, 255, 0.1);
20
box-shadow: 0 0 10px rgba(255, 255, 255, 1);
21
}
JavaScript
1
19
19
1
<!DOCTYPE html>
2
<html>
3
4
<head>
5
<meta charset="utf-8" />
6
<title>Flower of life</title>
7
<link rel="stylesheet" type="text/css" href="fol.css" />
8
</head>
9
10
<body>
11
<div class="background">
12
<div id="fol" class="container">
13
</div>
14
</div>
15
16
<script src="fol.js"></script>
17
</body>
18
19
</html>