| 1 | import {linearish} from "./linear.js";
|
|---|
| 2 | import {copy, identity, transformer} from "./continuous.js";
|
|---|
| 3 | import {initRange} from "./init.js";
|
|---|
| 4 |
|
|---|
| 5 | function transformPow(exponent) {
|
|---|
| 6 | return function(x) {
|
|---|
| 7 | return x < 0 ? -Math.pow(-x, exponent) : Math.pow(x, exponent);
|
|---|
| 8 | };
|
|---|
| 9 | }
|
|---|
| 10 |
|
|---|
| 11 | function transformSqrt(x) {
|
|---|
| 12 | return x < 0 ? -Math.sqrt(-x) : Math.sqrt(x);
|
|---|
| 13 | }
|
|---|
| 14 |
|
|---|
| 15 | function transformSquare(x) {
|
|---|
| 16 | return x < 0 ? -x * x : x * x;
|
|---|
| 17 | }
|
|---|
| 18 |
|
|---|
| 19 | export function powish(transform) {
|
|---|
| 20 | var scale = transform(identity, identity),
|
|---|
| 21 | exponent = 1;
|
|---|
| 22 |
|
|---|
| 23 | function rescale() {
|
|---|
| 24 | return exponent === 1 ? transform(identity, identity)
|
|---|
| 25 | : exponent === 0.5 ? transform(transformSqrt, transformSquare)
|
|---|
| 26 | : transform(transformPow(exponent), transformPow(1 / exponent));
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | scale.exponent = function(_) {
|
|---|
| 30 | return arguments.length ? (exponent = +_, rescale()) : exponent;
|
|---|
| 31 | };
|
|---|
| 32 |
|
|---|
| 33 | return linearish(scale);
|
|---|
| 34 | }
|
|---|
| 35 |
|
|---|
| 36 | export default function pow() {
|
|---|
| 37 | var scale = powish(transformer());
|
|---|
| 38 |
|
|---|
| 39 | scale.copy = function() {
|
|---|
| 40 | return copy(scale, pow()).exponent(scale.exponent());
|
|---|
| 41 | };
|
|---|
| 42 |
|
|---|
| 43 | initRange.apply(scale, arguments);
|
|---|
| 44 |
|
|---|
| 45 | return scale;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | export function sqrt() {
|
|---|
| 49 | return pow.apply(null, arguments).exponent(0.5);
|
|---|
| 50 | }
|
|---|