Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.25 KB | None | 0 0
  1.  
  2. #include <stdafx.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <math.h>
  6. #include <time.h>
  7. #include <windows.h>
  8. #define THREADS 10
  9. #define PI 3.14159
  10. #define SIN(a) sin(a*PI/180.0)
  11. #define COS(a) cos(a*PI/180.0)
  12. #define MAX 1000
  13. #define MINSPEED 300.0 // Km/h
  14. #define MAXSPEED 900.0 // Km/h
  15. #define MAXALT 13000.0 // Metros
  16. #define MINDIST 4000.0 // Distancia mínima en metros entre aviones si estos están
  17.  
  18. struct AVION
  19. {
  20. double pos_x;
  21. double pos_y;
  22. double pos_z;
  23. double currspeed;
  24. double dir;
  25. double incl;
  26. int warnings;
  27. };
  28.  
  29. struct AVION avion[MAX];
  30. int totwarnings = 0;
  31. CRITICAL_SECTION sc;
  32.  
  33. double dist(struct AVION a, struct AVION b)
  34. {
  35. double dist_x, dist_y, dist_z, dist;
  36. dist_x = a.pos_x - b.pos_x;
  37. dist_y = a.pos_y - b.pos_y;
  38. dist_z = a.pos_z - b.pos_z;
  39. dist = sqrt(dist_x*dist_x + dist_y*dist_y + dist_z*dist_z);
  40. return(dist);
  41. }
  42.  
  43. double distmin(struct AVION a, struct AVION b)
  44. {
  45. double speed;
  46. double distmin;
  47. if (a.currspeed > b.currspeed)
  48. speed = a.currspeed / 3.6;
  49. else
  50. speed = b.currspeed / 3.6;
  51. distmin = MINDIST + speed*speed*1.6;
  52. return(distmin);
  53. }
  54. void inicializa_aviones(struct AVION *x)
  55. {
  56. int i;
  57. for (i = 0; i < MAX; i++)
  58. {
  59. x[i].pos_x = 1000 * (5000.0 - (double)(rand() % 10000));
  60. x[i].pos_y = 1000 * (5000.0 - (double)(rand() % 10000));
  61. x[i].pos_z = (double)(rand() % 13000);
  62. x[i].currspeed = MINSPEED + x[i].pos_z / 21;
  63. x[i].dir = (double)(rand() % 360);
  64. }
  65. return;
  66. }
  67. // Se actualiza cada 1/1000 segundos
  68. void actualiza_avion(struct AVION *a)
  69. {
  70. if (a->pos_z < 13000.0)
  71. a->pos_z += 0.001;
  72. a->currspeed = MINSPEED + a->pos_z / 21;
  73. a->pos_x += 0.001*(a->currspeed / 3.6)*SIN(a->dir);
  74. a->pos_y += 0.001*(a->currspeed / 3.6)*COS(a->dir);
  75. return;
  76. }
  77.  
  78. void NTAPI hilo(PTP_CALLBACK_INSTANCE instance, void * context, PTP_WORK work)
  79. {
  80. int hnilo = *((int *)context);
  81. // printf("Hilo=%d, inicio=%d, fin=%d\n",hnilo,rinic,rfin);
  82. for (int i = hnilo; i < MAX; i += THREADS)
  83. {
  84. for (int j = i; j < MAX; j++)
  85. if (i != j)
  86. if (dist(avion[i], avion[j]) < distmin(avion[i], avion[j]))
  87. {
  88. EnterCriticalSection(&sc);
  89. avion[i].warnings++;
  90. avion[j].warnings++;
  91. totwarnings++;
  92. LeaveCriticalSection(&sc);
  93. }
  94. }
  95. }
  96.  
  97. int main()
  98. {
  99. int n;
  100. int i, j;
  101. int t;
  102. int par[THREADS];
  103. PTP_POOL pool = CreateThreadpool(NULL);
  104. TP_CALLBACK_ENVIRON CallBackEnviron;
  105. clock_t t_inicial, t_final;
  106. InitializeThreadpoolEnvironment(&CallBackEnviron);
  107. SetThreadpoolThreadMaximum(pool, THREADS);
  108. SetThreadpoolThreadMinimum(pool, 1);
  109. SetThreadpoolCallbackPool(&CallBackEnviron, pool);
  110. inicializa_aviones(avion);
  111. InitializeCriticalSection(&sc);
  112. TP_WORK * work;
  113. t_inicial = clock();
  114. for (n = 0; n < 200; n++)
  115. {
  116. for (int x = 0; x < THREADS; x++){
  117. par[x] = x;
  118. work = CreateThreadpoolWork(hilo, &par[x], &CallBackEnviron);
  119. SubmitThreadpoolWork(work);
  120. }
  121. WaitForThreadpoolWorkCallbacks(work, FALSE);
  122. for (i = 0; i < MAX; i++)
  123. actualiza_avion(&avion[i]);
  124. if (n % 10 == 0)
  125. printf("Total Warnings = %d\n", totwarnings);
  126. }
  127. CloseThreadpoolWork(work);
  128. CloseThreadpool(pool);
  129. t_final = clock();
  130. printf("En %3.6f segundos\n", ((float)t_final - (float)t_inicial) / CLOCKS_PER_SEC);
  131. getchar();
  132. DeleteCriticalSection(&sc);
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement