SW 중심대학 OSS GIT 서버 박건태, 이승준, 고기완, 이준호 새로운 배포
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
11 KiB

4 years ago
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using ARLocation;
  6. public class dijkstraShortPathInfo : MonoBehaviour
  7. {
  8. public LocationPath locationPath;
  9. //public Location[] locations;
  10. // Start is called before the first frame update
  11. public void GetPath(int x, int y){
  12. dijkstra(adjacencyMatrix, x, y);
  13. Array.Resize(ref locationPath.Locations, savepath.Length);
  14. Invoke("Fcc", 1f);
  15. }
  16. void Fcc()
  17. {
  18. for (int i = 0; i < savepath.Length; i++)
  19. {
  20. locationPath.Locations[i].Latitude = xx[savepath[i]];
  21. locationPath.Locations[i].Longitude = yy[savepath[i]];
  22. }
  23. }
  24. private void Start()
  25. {
  26. Array.Resize(ref locationPath.Locations, 0);
  27. }
  28. public static double[] xx = {35.968340,
  29. 35.968344,
  30. 35.967567,
  31. 35.967233,
  32. 35.967175,
  33. 35.967116,
  34. 35.967376,
  35. 35.967466,
  36. 35.967796,
  37. 35.968186,
  38. 35.968294,
  39. 35.968169,
  40. 35.968122,
  41. 35.967498,
  42. 35.968656,
  43. 35.968681,
  44. 35.968701,
  45. 35.968712,
  46. 35.968747,
  47. 35.968811,
  48. 35.969017,
  49. 35.969254,
  50. 35.969649,
  51. 35.970050,
  52. 35.970448,
  53. 35.970412,
  54. 35.970105,
  55. 35.970123,
  56. 35.970074,
  57. 35.970277,
  58. 35.970773,
  59. 35.971093,
  60. 35.971691,
  61. 35.972213,
  62. 35.971945,
  63. 35.968735,
  64. 35.969062,
  65. 35.969360,
  66. 35.969972,
  67. 35.969965,
  68. 35.970113,
  69. 35.969409,
  70. 35.969063,
  71. 35.969110};
  72. public static double[] yy = {126.958104,
  73. 126.958931,
  74. 126.959010,
  75. 126.959057,
  76. 126.958213,
  77. 126.957411,
  78. 126.957384,
  79. 126.958192,
  80. 126.957333,
  81. 126.957295,
  82. 126.957481,
  83. 126.957026,
  84. 126.956660,
  85. 126.956364,
  86. 126.956969,
  87. 126.958065,
  88. 126.958890,
  89. 126.959303,
  90. 126.960234,
  91. 126.960767,
  92. 126.959285,
  93. 126.959269,
  94. 126.959206,
  95. 126.959107,
  96. 126.959044,
  97. 126.958577,
  98. 126.958505,
  99. 126.958834,
  100. 126.958083,
  101. 126.958161,
  102. 126.958137,
  103. 126.958119,
  104. 126.958135,
  105. 126.958130,
  106. 126.957231,
  107. 126.956853,
  108. 126.956823,
  109. 126.956825,
  110. 126.956810,
  111. 126.955791,
  112. 126.955241,
  113. 126.955928,
  114. 126.956008,
  115. 126.956427};
  116. private static readonly int NO_PARENT = -1;
  117. //경로 저장 배열
  118. public static int[] savepath;
  119. public static int[,] adjacencyMatrix = {{0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  120. {1,0,1,0,0,0,0,0,0,0,2,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  121. {0,1,0,1,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  122. {0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  123. {0,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  124. {0,0,0,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
  125. {0,0,0,0,0,1,0,1,1,0,0,0,0,0,0,0,0,0,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1,1,0,0},
  159. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,1},
  160. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,1,0},
  161. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,1},
  162. {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0}};
  163. public static void dijkstra(int[,] adjacencyMatrix, int startVertex, int destinationvertex)
  164. {
  165. int nVertices = adjacencyMatrix.GetLength(0);
  166. //shortestDistances[i]는 src(출발지)에서 i까지의 최단 거리를 유지합니다
  167. int[] shortestDistances = new int[nVertices];
  168. //added [i]는 버텍스(vertex정점,노드) i가 포함되어 있거나 최단 경로 트리에 있거나 src에서 i까지의 최단 거리가 완료되면 true입니다.
  169. bool[] added = new bool[nVertices];
  170. //모든 거리를 INFINITE로 초기화하고[]를 false로 추가
  171. for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++)
  172. {
  173. shortestDistances[vertexIndex] = int.MaxValue;
  174. added[vertexIndex] = false;
  175. }
  176. //자체 정점의 거리는 항상 0입니다
  177. shortestDistances[startVertex] = 0;
  178. //최단 경로 트리를 저장하는 부모 배열
  179. int[] parents = new int[nVertices];
  180. //시작 정점에는 부모가 없습니다
  181. parents[startVertex] = NO_PARENT;
  182. //모든 정점에 대한 최단 경로 찾기
  183. for (int i = 1; i < nVertices; i++)
  184. {
  185. //아직 처리되지 않은 정점 세트에서 최소 거리 정점을 지정합니다. 가장 가까운 정점은 첫 번째 반복에서 항상 startNode와 같습니다.
  186. int nearestVertex = -1;
  187. int shortestDistance = int.MaxValue;
  188. for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++)
  189. {
  190. if (!added[vertexIndex] && shortestDistances[vertexIndex] < shortestDistance)
  191. {
  192. nearestVertex = vertexIndex;
  193. shortestDistance = shortestDistances[vertexIndex];
  194. }
  195. }
  196. //선택된 정점을 처리 된 것으로 표시
  197. added[nearestVertex] = true;
  198. //선택된 정점의 인접한 정점의 거리 값을 업데이트합니다.
  199. for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++)
  200. {
  201. int edgeDistance = adjacencyMatrix[nearestVertex, vertexIndex];
  202. if (edgeDistance > 0 && ((shortestDistance + edgeDistance) < shortestDistances[vertexIndex]))
  203. {
  204. parents[vertexIndex] = nearestVertex;
  205. shortestDistances[vertexIndex] = shortestDistance + edgeDistance;
  206. }
  207. }
  208. }
  209. printSolution(startVertex, shortestDistances, parents, destinationvertex);
  210. }
  211. //구성된 거리 배열과 최단 경로를 인쇄하는 유틸리티 기능
  212. private static void printSolution(int startVertex, int[] distances, int[] parents, int destinationvertex)
  213. {
  214. int nVertices = distances.Length;
  215. Console.Write("Vertex\t Distance\tPath");
  216. //for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++)
  217. //{
  218. if (destinationvertex != startVertex)
  219. {
  220. Console.Write("\n" + startVertex + " ->");
  221. Console.Write(destinationvertex + "\t\t");
  222. Console.Write(distances[destinationvertex] + "\t\t");
  223. //거리만큼 savepath 배열 크기 재설정
  224. Array.Resize(ref savepath, distances[destinationvertex] + 1);
  225. printPath(destinationvertex, parents, distances[destinationvertex] + 1);
  226. }
  227. //}
  228. }
  229. //부모 배열을 사용하여 소스에서 currentVertex까지의 최단 경로를 인쇄하는 기능
  230. private static void printPath(int currentVertex, int[] parents, int chkpoint)
  231. {
  232. if (currentVertex == NO_PARENT)
  233. {
  234. return;
  235. }
  236. printPath(parents[currentVertex], parents, --chkpoint);
  237. //경로 배열에 저장
  238. savepath[chkpoint] = currentVertex;
  239. Console.Write(currentVertex + " ");
  240. }
  241. }