graphtest.pwn 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include <a_samp>
  2. /*
  3. The goal is to get x many days shown on a graph
  4. of the total wealth of a family
  5. */
  6. #define MAX_DAY_COUNT 10
  7. static Yaxis[] = {
  8. 100000,245000,300000,270000,135000, 160000, 569000, 821000, 749004, 309000
  9. };
  10. static GetMaxYValue(Y[], size_t = sizeof Y) {
  11. new maximum = 0;
  12. for(new i = 0; i < size_t; i++) {
  13. if(Y[i] > maximum) {
  14. maximum = Y[i];
  15. }
  16. }
  17. return maximum;
  18. }
  19. static GetMinYValue(Y[], size_t = sizeof Y) {
  20. new minimum = Y[0];
  21. if(size_t < 1) return minimum;
  22. for(new i = 1; i < size_t; i++) {
  23. if(Y[i] < minimum) {
  24. minimum = Y[i];
  25. }
  26. }
  27. return minimum;
  28. }
  29. //credits RyDeR' (I was lazy...I adjusted it to maintain order)
  30. stock quickSort(array[], order[], left, right)
  31. {
  32. new
  33. tempLeft = left,
  34. tempRight = right,
  35. pivot = array[(left + right) / 2],
  36. tempVar,
  37. tempOrder
  38. ;
  39. while(tempLeft <= tempRight)
  40. {
  41. while(array[tempLeft] < pivot) tempLeft++;
  42. while(array[tempRight] > pivot) tempRight--;
  43. if(tempLeft <= tempRight)
  44. {
  45. tempVar = array[tempLeft];
  46. tempOrder = order[tempLeft];
  47. array[tempLeft] = array[tempRight];
  48. order[tempLeft] = order[tempRight];
  49. array[tempRight] = tempVar;
  50. order[tempRight] = tempOrder;
  51. tempLeft++, tempRight--;
  52. }
  53. }
  54. if(left < tempRight) quickSort(array, order, left, tempRight);
  55. if(tempLeft < right) quickSort(array, order, tempLeft, right);
  56. }
  57. /*
  58. todo: output to textdraw, all data points finished (start at 10 days ago), can add padding around graph of same colour
  59. */
  60. public OnFilterScriptInit() {
  61. const screenwidth = 100; //300px = height of graph
  62. new halfwidth = floatround(screenwidth/2);
  63. new maximum = GetMaxYValue(Yaxis);
  64. new minimum = GetMinYValue(Yaxis);
  65. printf("min: %d, max: %d", minimum, maximum);
  66. new diff = maximum - minimum;
  67. new scale = floatround(maximum/screenwidth);
  68. printf("scale: %d", scale);
  69. new order[] = {0,1,2,3,4,5,6,7,8,9}; //maintains the order
  70. quickSort(Yaxis, order, 0, sizeof(Yaxis) - 1);
  71. //Note since they're ordered -- just place them on Y-axis, we'll move them along X-axis later...
  72. new cm = minimum;
  73. new finalYs[MAX_DAY_COUNT+1]; //same array size as Yaxis. finalYs = pixel on screen
  74. new cursor = 0;
  75. for(new i; i < sizeof(Yaxis); i++) {
  76. if(i == 0) {
  77. printf("(%d,%d)", minimum, Yaxis[i]);
  78. printf("y: %d, cursor: %d", i, cursor);
  79. finalYs[i] = cursor;
  80. continue;
  81. }
  82. if(i == sizeof(Yaxis)-1) {
  83. printf("(%d,%d)", maximum, Yaxis[i]);
  84. printf("y: %d, cursor: %d", i, 100);
  85. finalYs[i] = 100;
  86. break;
  87. }
  88. while(1) {
  89. if(cm > maximum) {
  90. print("fault: less than or gtr than cm");
  91. return 1;
  92. }
  93. //found in between two data points?
  94. new cmtmp = cm + scale;
  95. //printf("cmtmp: %d, cm: %d, Y: %d", cmtmp, cm, Yaxis[i]);
  96. if(cm < Yaxis[i] < cmtmp) {
  97. printf("(%d, %d)", cm, Yaxis[i]);
  98. printf("y: %d, cursor: %d", i, cursor);
  99. finalYs[i] = cursor;
  100. break;
  101. }
  102. else {
  103. cm += scale;
  104. cursor++;
  105. }
  106. }
  107. }
  108. print("---------------------");
  109. for(new i; i < MAX_DAY_COUNT; i++) {
  110. printf("X: %d, Y: %d (old Y: %d)", order[i], finalYs[i], Yaxis[i]);
  111. }
  112. return 1;
  113. }
  114. public OnFilterScriptExit() {}
  115. /*
  116. OUTPUT -- maps Y value to nearest data point in graph
  117. */
  118. /*
  119. [15:20:19] min: 100000, max: 821000
  120. [15:20:19] scale: 8210
  121. [15:20:19] (100000,100000)
  122. [15:20:19] y: 0, cursor: 0
  123. [15:20:19] (132840, 135000)
  124. [15:20:19] y: 1, cursor: 4
  125. [15:20:19] (157470, 160000)
  126. [15:20:19] y: 2, cursor: 7
  127. [15:20:19] (239570, 245000)
  128. [15:20:19] y: 3, cursor: 17
  129. [15:20:19] (264200, 270000)
  130. [15:20:19] y: 4, cursor: 20
  131. [15:20:19] (297040, 300000)
  132. [15:20:19] y: 5, cursor: 24
  133. [15:20:19] (305250, 309000)
  134. [15:20:19] y: 6, cursor: 25
  135. [15:20:19] (567970, 569000)
  136. [15:20:19] y: 7, cursor: 57
  137. [15:20:19] (748590, 749004)
  138. [15:20:19] y: 8, cursor: 79
  139. [15:20:19] (821000,821000)
  140. [15:20:19] y: 9, cursor: 100
  141. [15:20:19] --------------------- ===== // quicksort back: quicksort(x, y) will reverse
  142. //now all I need to do is add textdraw 'points' at positions (X,Y)
  143. //Oh wait, no need to quicksort back... just textdraw :) order no matter
  144. [15:20:19] X: 0, Y: 0 (old Y: 100000)
  145. [15:20:19] X: 4, Y: 4 (old Y: 135000)
  146. [15:20:19] X: 5, Y: 7 (old Y: 160000)
  147. [15:20:19] X: 1, Y: 17 (old Y: 245000)
  148. [15:20:19] X: 3, Y: 20 (old Y: 270000)
  149. [15:20:19] X: 2, Y: 24 (old Y: 300000)
  150. [15:20:19] X: 9, Y: 25 (old Y: 309000)
  151. [15:20:19] X: 6, Y: 57 (old Y: 569000)
  152. [15:20:19] X: 8, Y: 79 (old Y: 749004)
  153. [15:20:19] X: 7, Y: 100 (old Y: 821000)
  154. */