sort.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. function shapeBox(shape) {
  2. let minX=shape[0][0], minY=shape[0][1], maxX=minX, maxY=minY
  3. shape.forEach(curve=> {
  4. let x1 = curve[0],
  5. x2 = curve[2],
  6. x3 = curve[4],
  7. x4 = curve[6],
  8. y1 = curve[1],
  9. y2 = curve[3],
  10. y3 = curve[5],
  11. y4 = curve[7]
  12. minX = Math.min(minX, x1, x2, x3, x4)
  13. minY = Math.min(minY, y1, y2, y3, y4)
  14. maxX = Math.max(maxX, x1, x2, x3, x4)
  15. maxY = Math.max(maxY, y1, y2, y3, y4)
  16. })
  17. return [minX, minY, maxX, maxY]
  18. }
  19. function boxDistance(boxA, boxB){
  20. return Math.sqrt(Math.pow( boxA[0] - boxB[0],2)+Math.pow(boxA[1]-boxB[1],2))+ Math.sqrt(Math.pow( boxA[2] - boxB[2],2)+Math.pow(boxA[3]-boxB[3],2))
  21. }
  22. function curveDistance(curveA,curveB) {
  23. let x1 = curveA[0],
  24. x2 = curveA[2],
  25. x3 = curveA[4],
  26. x4 = curveA[6],
  27. y1 = curveA[1],
  28. y2 = curveA[3],
  29. y3 = curveA[5],
  30. y4 = curveA[7],
  31. xb1 = curveB[0],
  32. xb2 = curveB[2],
  33. xb3 = curveB[4],
  34. xb4 = curveB[6],
  35. yb1 = curveB[1],
  36. yb2 = curveB[3],
  37. yb3 = curveB[5],
  38. yb4 = curveB[7]
  39. return Math.sqrt(Math.pow(xb1 - x1, 2) + Math.pow(yb1 - y1, 2)) +
  40. Math.sqrt(Math.pow(xb2 - x2, 2) + Math.pow(yb2 - y2, 2)) +
  41. Math.sqrt(Math.pow(xb3 - x3, 2) + Math.pow(yb3 - y3, 2)) +
  42. Math.sqrt(Math.pow(xb4 - x4, 2) + Math.pow(yb4 - y4, 2))
  43. }
  44. function sortCurves(curvesA, curvesB){
  45. let arrList = permuteCurveNum(curvesA.length)
  46. let list = []
  47. arrList.forEach(arr => {
  48. let distance = 0
  49. let i = 0
  50. arr.forEach(index=> {
  51. distance += curveDistance(curvesA[index], curvesB[i++])
  52. })
  53. list.push({index: arr, distance: distance})
  54. })
  55. list.sort(function(a,b){
  56. return a.distance - b.distance
  57. })
  58. let result = []
  59. list[0].index.forEach(index=>{
  60. result.push(curvesA[index])
  61. })
  62. return result
  63. }
  64. function sort(pathA, pathB){
  65. let arrList = permuteNum(pathA.length)
  66. let list = []
  67. arrList.forEach(arr => {
  68. let distance = 0
  69. arr.forEach(index=> {
  70. distance += boxDistance(shapeBox(pathA[index]), shapeBox(pathB[index]))
  71. })
  72. list.push({index: arr, distance: distance})
  73. })
  74. list.sort(function(a,b){
  75. return a.distance - b.distance
  76. })
  77. let result = []
  78. list[0].index.forEach(index=>{
  79. result.push(pathA[index])
  80. })
  81. return result
  82. }
  83. function permuteCurveNum(num) {
  84. let arr = []
  85. for (let i = 0; i < num; i++) {
  86. let indexArr = []
  87. for (let j = 0; j < num; j++) {
  88. let index = j + i
  89. if (index > num - 1)index -= num
  90. indexArr[index] = j
  91. }
  92. arr.push(indexArr)
  93. }
  94. return arr
  95. }
  96. function permuteNum(num) {
  97. let arr = []
  98. for (let i = 0; i < num; i++) {
  99. arr.push(i)
  100. }
  101. return permute(arr)
  102. }
  103. function permute(input) {
  104. var permArr = [],
  105. usedChars = []
  106. function main(input){
  107. var i, ch
  108. for (i = 0; i < input.length; i++) {
  109. ch = input.splice(i, 1)[0]
  110. usedChars.push(ch)
  111. if (input.length == 0) {
  112. permArr.push(usedChars.slice())
  113. }
  114. main(input)
  115. input.splice(i, 0, ch)
  116. usedChars.pop()
  117. }
  118. return permArr
  119. }
  120. return main(input)
  121. }
  122. export {
  123. sort,
  124. sortCurves
  125. }