Last active
June 18, 2017 21:24
-
-
Save stephanbogner/4150fb19b3addd96c6b89b1d6b41ba30 to your computer and use it in GitHub Desktop.
Gets the circumference of svg style coordinates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var coordinatesString = "223.177496408,3814.53101376 210.997794052,3807.13302253 195.852458671,3793.15810611 193.993838635,3791.43941224 180.933214269,3772.19079794 170.136151235,3747.26405193 165.198319283,3723.470457 163.682660844,3678.3761749 156.176550135,3555.30499258 151.509512401,3480.88344101 145.426837106,3400.82934564 143.794799706,3364.1990739 143.798834889,3326.0160711 150.571949561,3300.02762516 160.269931448,3276.84312163 172.710136954,3253.09881653 191.619853303,3230.83282556 216.520246839,3210.98593792 239.837973955,3200.03271458 260.488917349,3192.51986408 274.107463498,3190.53640455 288.34689427,3188.89624907 304.890225132,3189.71668734 328.216563439,3192.98221144 345.935882157,3196.87585617 358.827514541,3202.22143433 371.831727975,3208.85035743 388.203233763,3219.72972793 409.103972729,3237.94034309 425.300490135,3258.88452843 444.210350636,3303.2243898 447.967830543,3319.477736 448.705200294,3345.05481017 443.331961433,3372.210188 429.295830231,3405.64584587 412.766832771,3427.98420714 395.953864727,3439.61475951 383.774995462,3446.43131012 377.363627046,3452.30408312 376.036758894,3458.49627594 378.120972396,3462.87300892 383.500490992,3464.15680231 389.21670368,3471.96279845 400.91907328,3487.94094596 409.101275371,3499.12281393 420.024901381,3515.58346306 425.84577555,3527.26532721 429.31024144,3538.07551503 429.561455403,3547.31534996 424.893195437,3624.74549528 418.289719018,3632.07855337 412.647752352,3665.03394769 402.849961198,3702.02929806 391.40829249,3729.87462584 378.102329066,3755.71725053 364.033439553,3776.85731903 335.924878258,3803.28388913 306.191363037,3819.91767082 280.331037887,3825.76036174 248.570252241,3821.79918472 223.177496408,3814.53101376"; | |
var coordinates = prepareCoordinates(coordinatesString); | |
var distance = getEntireDistance(coordinates); | |
console.log(distance); | |
function getEntireDistance(coordinates){ | |
var distance = 0; | |
for (var i = 0; i < coordinates.length - 1; i++) { | |
var from = coordinates[i]; | |
var to = coordinates[i+1]; | |
distance += getDistance(from[0], from[1], to[0], to[1]); | |
} | |
return distance; | |
} | |
function prepareCoordinates(coordinatesString) { | |
var coordinatesArray = coordinatesString.split(' '); | |
for (var i = 0; i < coordinatesArray.length; i++) { | |
coordinatesArray[i] = coordinatesArray[i].split(','); | |
for (var j = 0; j < coordinatesArray[i].length; j++) { | |
coordinatesArray[i][j] = Number(coordinatesArray[i][j]); // Convert string to number | |
} | |
} | |
return coordinatesArray; | |
} | |
function getDistance(x1, y1, x2, y2) { | |
var dx = x1 - x2; | |
var dy = y1 - y2; | |
var d = Math.sqrt(Math.pow(dx, 2) + Math.pow(dy, 2)); // Pythagoras :) | |
return d; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment