ParanoidPanda

somethings.c

Jan 8th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. #include "somethings.h"
  2. COLORREF whiteColor = RGB(255, 255, 255);
  3. int countOfDots = 0;
  4. struct sDots *dots;
  5. struct sDots *result;
  6.  
  7. //HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
  8.  
  9. void menu(){
  10. printf("\n\nChoose what u want \n");
  11. printf("Add dot [1] \n");
  12. printf("Delete dot [2] \n");
  13. printf("Show dots [3] \n");
  14. printf("Start algo [4] \n");
  15. printf("Load dots from file [5] \n");
  16. int choose = 0;
  17. scanf("%d", &choose);
  18. switch (choose){
  19. case(1) : {
  20. addDot();
  21. break;
  22. }
  23. case(2) : {
  24. deleteDot();
  25. menu();
  26. break;
  27. }
  28. case(3) : {
  29. showDots();
  30. menu();
  31. break;
  32.  
  33. }
  34. case(4) : {
  35. startAlgo();
  36. break;
  37. }
  38. case(5) : {
  39. loadThis();
  40. menu();
  41. break;
  42. }
  43. default:{
  44. printf("Bad choose! \n\n");
  45. menu();
  46. }
  47. }
  48. }
  49.  
  50. void addDot(){
  51. printf("x y \n");
  52. int tmpx = 0, tmpy = 0;
  53. int target = 1;
  54. scanf("%d %d", &tmpx, &tmpy);
  55. for (int i = 0; i < countOfDots; i++){
  56. if (tmpx == dots[i].x && tmpy == dots[i].y){
  57. target = 0;
  58. }
  59. }
  60. if (target == 0){
  61. printf("WRONG DOT!!\n\n");
  62. }
  63. else
  64. {
  65. dots = (struct sDots *)realloc((void *)dots, (countOfDots = countOfDots + 1)*sizeof(struct sDots));
  66. dots[countOfDots - 1].x = tmpx;
  67. dots[countOfDots - 1].y = tmpy;
  68. }
  69. menu();
  70. }
  71.  
  72. void showDots(){
  73. printf("\n\nDots: \n");
  74. for (int i = 0; i < countOfDots; i++){
  75. printf("dot[%d] : %d %d \n", i + 1, dots[i].x, dots[i].y);
  76. }
  77. printf("\n\n");
  78. }
  79.  
  80. void deleteDot(){
  81. showDots();
  82. printf("input number of dot: ");
  83. int numdot = 0;
  84. scanf("%d", &numdot);
  85. numdot -= 1;
  86. if (numdot < countOfDots){
  87. for (int i = numdot; i < countOfDots - 1; i++){
  88. dots[i].x = dots[i + 1].x;
  89. dots[i].y = dots[i + 1].y;
  90. }
  91. dots = (struct sDots *)realloc((void *)dots, (countOfDots = countOfDots - 1)*sizeof(struct sDots));
  92. }
  93. else{
  94. printf("bad input!!!!\n\n");
  95. }
  96. }
  97.  
  98. void startAlgo(){
  99. int counter = 0;
  100. result = (struct sDots *)realloc((void *)result, (counter)*sizeof(struct sDots));
  101. //free(result);
  102. if (countOfDots < 5){
  103. printf("wron number of dots!\n");
  104. menu();
  105. }
  106. else{
  107. int size = 0;
  108. int l = 0;
  109. for (int i = 1; i < countOfDots; i++){
  110. if (dots[i].x < dots[l].x)
  111. l = i;
  112. }
  113. int p = l, q = 0;
  114. do{
  115. q = (p + 1) % countOfDots;
  116. for (int i = 0; i < countOfDots; i++)
  117. if (orientation(dots[p], dots[i], dots[q]) == 2)
  118. q = i;
  119. result = (struct sDots *)realloc((void *)result, (size = size + 1)*sizeof(struct sDots));
  120. result[size - 1].x = dots[q].x;
  121. result[size - 1].y = dots[q].y;
  122. p = q;
  123. } while (p != l);
  124. for (int i = 0; i < size; i++){
  125. printf("%d %d \n", result[i].x, result[i].y);
  126. }
  127. FILE *output = fopen("output.txt", "w+");
  128. for (int i = 0; i < size; i++){
  129. fprintf(output, "%d %d \n", result[i].x, result[i].y);
  130. }
  131. fclose(output);
  132. drawThis(size);
  133. }
  134. }
  135.  
  136. int orientation(struct sDots a, struct sDots b, struct sDots c){
  137. int val = (b.y - a.y) * (c.x - b.x) - (b.x - a.x) * (c.y - b.y);
  138. if (val == 0)
  139. return 0;
  140. return (val > 0) ? 1 : 2;
  141. }
  142.  
  143. void loadThis(){
  144. FILE *input = fopen("dots.txt", "r+");
  145. if (input == NULL){
  146. printf("Wrong file!\n\n\n\n\n");
  147. menu();
  148. }
  149. else{
  150. int counter = 0;
  151. while (!feof(input)){
  152. if (fgetc(input) == '\n')
  153. counter++;
  154. }
  155. fseek(input, 0, SEEK_SET);
  156. for (int i = 0; i < counter + 1; i++){
  157. dots = (struct sDots *)realloc((void *)dots, (countOfDots = countOfDots + 1)*sizeof(struct sDots));
  158. fscanf(input, "%d %d", &dots[countOfDots - 1].x, &dots[countOfDots - 1].y);
  159. }
  160. showDots();
  161. }
  162. }
  163.  
  164. void drawThis(int c){
  165. HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
  166. system("cls");
  167. for (int i = 0; i < countOfDots; i++){
  168. drawDot((dots[i].x * 25) + 10, (dots[i].y * 25) + 10);
  169. }
  170. for (int i = 0; i < c - 1; i++){
  171. drawLine((result[i].x * 25) + 10, (result[i].y * 25) + 10, (result[i + 1].x * 25) + 10, (result[i + 1].y * 25) + 10);
  172. }
  173. drawLine((result[c - 1].x * 25) + 10, (result[c - 1].y * 25) + 10, (result[0].x * 25) + 10, (result[0].y * 25) + 10);
  174. }
  175.  
  176. void drawDot(int x, int y){
  177. HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
  178. SetPixel(hdc, x, y, whiteColor);
  179. }
  180.  
  181. void drawLine(int x1, int y1, int x2, int y2){
  182. HDC hdc = GetDC(FindWindowA("ConsoleWindowClass", NULL));
  183. HPEN pen = CreatePen(PS_SOLID, 1, whiteColor);
  184. HPEN oldpen = (HPEN)SelectObject(hdc, pen);
  185. MoveToEx(hdc, x1, y1, 0);
  186. LineTo(hdc, x2, y2);
  187. SelectObject(hdc, oldpen);
  188. DeleteObject(pen);
  189. }
Advertisement
Add Comment
Please, Sign In to add comment