Compare commits

..

No commits in common. "14bee6cf6ebe8ed77e17eb525bb91110c60d4734" and "ca10f8a0381e526afcda3ef6f6cc868f3c18994a" have entirely different histories.

3 changed files with 30 additions and 51 deletions

View File

@ -156,14 +156,13 @@ function poptimal_svg(params) {
params.patterns.map((p) => { params.patterns.map((p) => {
const dots = dm.dots(1 / p.m, p.n, false); const dots = dm.dots(1 / p.m, p.n, false);
const rfunc = dm.radius(p.f);
const dots_g = svg.append("g") const dots_g = svg.append("g")
.attr("id", `dots${p.i}`); .attr("id", `dots${p.i}`);
dots_g.selectAll("circle") dots_g.selectAll("circle")
.data(dots) .data(dots)
.join("circle") .join("circle")
.attr("r", (d) => rfunc(d, p.r)) .attr("r", (d) => dm.radius(d, p.f, p.r))
.attr("fill", p.colour) .attr("fill", p.colour)
.attr("cx", (d) => d.x) .attr("cx", (d) => d.x)
.attr("cy", (d) => d.y); .attr("cy", (d) => d.y);

View File

@ -11,11 +11,8 @@ const RADIUS_OPTS = [
"right-down", "right-down",
"left-up", "left-up",
"left-down", "left-down",
"circle-in", "in",
"circle-out", "out",
"hyper-in",
"hyper-out",
"grid",
"noise", "noise",
]; ];
@ -29,19 +26,15 @@ const RADIUS_DESC = {
"right-down": "getting larger towards the lower right", "right-down": "getting larger towards the lower right",
"left-up": "getting larger towards the upper left", "left-up": "getting larger towards the upper left",
"left-down": "getting larger towards the lower left", "left-down": "getting larger towards the lower left",
"circle-in": "forming a circle at the centre", "in": "getting larger in the centre",
"circle-out": "leaving a circular gap at the centre", "out": "getting larger at the edges",
"hyper-in": "forming a starlike pattern at the centre",
"hyper-out": "leaving a starlike gap in the centre",
"noise": "of random sizes", "noise": "of random sizes",
"grid": "forming a grid pattern",
} }
function distance(dx, dy) { function distance(dx, dy) {
return Math.sqrt(dx ** 2 + dy ** 2); return Math.sqrt(dx ** 2 + dy ** 2);
} }
function int_range(v1, v2) { function int_range(v1, v2) {
const vs = [v1, v2]; const vs = [v1, v2];
vs.sort((a, b) => a - b); vs.sort((a, b) => a - b);
@ -50,10 +43,6 @@ function int_range(v1, v2) {
return [...Array(high - low + 1).keys()].map((i) => i + low); return [...Array(high - low + 1).keys()].map((i) => i + low);
} }
function randint(min, max) {
return min + Math.floor(Math.random() * (max + 1));
}
class DotMaker { class DotMaker {
constructor(width, height) { constructor(width, height) {
this.width = width; this.width = width;
@ -82,42 +71,40 @@ class DotMaker {
return ps; return ps;
} }
radius(func) { radius(d, func, maxr) {
switch (func) { switch (func) {
case "const": case "const":
return (d, r) => r; return maxr;
case "right": case "right":
return (d, r) => r * d.x / this.width; return maxr * d.x / this.width;
case "left": case "left":
return (d, r) => r * (this.width - d.x) / this.width; return maxr * (this.width - d.x) / this.width;
case "down": case "down":
return (d, r) => r * d.y / this.height; return maxr * d.y / this.height;
case "up": case "up":
return (d, r) => r * (this.height - d.y) / this.height; return maxr * (this.height - d.y) / this.height;
case "right-up": case "right-up":
return (d, r) => 0.5 * r * (d.x + this.height - d.y) / this.wh; return 0.5 * maxr * (d.x + this.height - d.y) / this.wh;
case "left-up": case "left-up":
return (d, r) => 0.5 * r * (this.width - d.x + this.height - d.y) / this.wh; return 0.5 * maxr * (this.width - d.x + this.height - d.y) / this.wh;
case "right-down": case "right-down":
return (d, r) => 0.5 * r * (d.x + d.y) / this.wh; return 0.5 * maxr * (d.x + d.y) / this.wh;
case "left-down": case "left-down":
return (d, r) => 0.5 * r * (this.width - d.x + d.y) / this.wh; return 0.5 * maxr * (this.width - d.x + d.y) / this.wh;
case "circle-out": case "out":
return (d, r) => 2 * r * distance((d.x - this.cx), (d.y - this.cy)) / this.wh; return 2 * maxr * distance((d.x - this.cx), (d.y - this.cy)) / this.wh;
case "circle-in": case "in":
return (d, r) => 2 * r * (0.5 * this.wh - distance((d.x - this.cx), (d.y - this.cy))) / this.wh; return 2 * maxr * (0.5 * this.wh - distance((d.x - this.cx), (d.y - this.cy))) / this.wh;
case "hyper-in":
return (d, r) => r * (1 - Math.abs((d.x - this.cx) * (d.y - this.cy)) / this.wh); // case "hyper-out":
case "hyper-out": // return 2 * maxr * Math.abs(d.x - this.cx) (d.y - this.cy)) / this.width;
return (d, r) => r * Math.abs((d.x - this.cx) * (d.y - this.cy)) / this.wh; // case "hyper-in":
// return 2 * maxr * (0.5 * this.width - distance((d.x - this.cx), (d.y - this.cy))) / this.width;
case "noise": case "noise":
return (d, r) => r * Math.random(); return maxr * Math.random();
case "grid":
const xk = Math.PI * (2 * randint(1, 10) - 1) / this.width;
const yk = Math.PI * (2 * randint(1, 10) - 1) / this.height;
return (d, r) => r * (0.5 + 0.5 * (Math.sin(xk * d.x) + Math.sin(yk * d.y)));
default: default:
return (d, r) => r; return maxr;
} }
} }
} }

View File

@ -8,7 +8,7 @@ toc: false
colourful generative patterns using [d3](https://d3js.org/) and [Observable Framework](https://observablehq.com/framework/) colourful generative patterns using [d3](https://d3js.org/) and [Observable Framework](https://observablehq.com/framework/)
<p>v1.2.1 | by <a href="https://mikelynch.org">mike lynch</a> | <a href="https://aus.social/@mikelynch">@mikelynch@aus.social</a> | <a href="https://git.tilde.town/bombinans/poptimal">source</a></p> <p>v1.2.0 | by <a href="https://mikelynch.org">mike lynch</a> | <a href="https://aus.social/@mikelynch">@mikelynch@aus.social</a> | <a href="https://git.tilde.town/bombinans/poptimal">source</a></p>
<div class="grid grid-cols-3"> <div class="grid grid-cols-3">
@ -137,13 +137,6 @@ const dots1 = dm.dots(1 / m1, n1, false);
const dots2 = dm.dots(1 / m2, n2, false); const dots2 = dm.dots(1 / m2, n2, false);
```
```js
const rfunc1 = dm.radius(f1);
const rfunc2 = dm.radius(f2);
``` ```
```js ```js
@ -217,8 +210,8 @@ display(svg.node());
```js ```js
// separate code block for when I understand transitions better // separate code block for when I understand transitions better
dots_g1.selectAll("circle").attr("r", (d) => rfunc1(d, r1)); dots_g1.selectAll("circle").attr("r", (d) => dm.radius(d, f1, r1));
dots_g2.selectAll("circle").attr("r", (d) => rfunc2(d, r2)); dots_g2.selectAll("circle").attr("r", (d) => dm.radius(d, f2, r2));
``` ```