|
Last change
on this file was e4c61dd, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Prototype 1.1
|
-
Property mode
set to
100644
|
|
File size:
854 bytes
|
| Line | |
|---|
| 1 | import {Adder} from "d3-array";
|
|---|
| 2 | import {sqrt} from "../math.js";
|
|---|
| 3 | import noop from "../noop.js";
|
|---|
| 4 |
|
|---|
| 5 | var lengthSum = new Adder(),
|
|---|
| 6 | lengthRing,
|
|---|
| 7 | x00,
|
|---|
| 8 | y00,
|
|---|
| 9 | x0,
|
|---|
| 10 | y0;
|
|---|
| 11 |
|
|---|
| 12 | var lengthStream = {
|
|---|
| 13 | point: noop,
|
|---|
| 14 | lineStart: function() {
|
|---|
| 15 | lengthStream.point = lengthPointFirst;
|
|---|
| 16 | },
|
|---|
| 17 | lineEnd: function() {
|
|---|
| 18 | if (lengthRing) lengthPoint(x00, y00);
|
|---|
| 19 | lengthStream.point = noop;
|
|---|
| 20 | },
|
|---|
| 21 | polygonStart: function() {
|
|---|
| 22 | lengthRing = true;
|
|---|
| 23 | },
|
|---|
| 24 | polygonEnd: function() {
|
|---|
| 25 | lengthRing = null;
|
|---|
| 26 | },
|
|---|
| 27 | result: function() {
|
|---|
| 28 | var length = +lengthSum;
|
|---|
| 29 | lengthSum = new Adder();
|
|---|
| 30 | return length;
|
|---|
| 31 | }
|
|---|
| 32 | };
|
|---|
| 33 |
|
|---|
| 34 | function lengthPointFirst(x, y) {
|
|---|
| 35 | lengthStream.point = lengthPoint;
|
|---|
| 36 | x00 = x0 = x, y00 = y0 = y;
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | function lengthPoint(x, y) {
|
|---|
| 40 | x0 -= x, y0 -= y;
|
|---|
| 41 | lengthSum.add(sqrt(x0 * x0 + y0 * y0));
|
|---|
| 42 | x0 = x, y0 = y;
|
|---|
| 43 | }
|
|---|
| 44 |
|
|---|
| 45 | export default lengthStream;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.