ParanoidPanda

allthings.c

Jan 8th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. #include "allthings.h"
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. struct point *arrayofpoints;
  8. struct point dot;
  9.  
  10.  
  11. struct point *loadthis(const char*filename, int countofpoints, struct point *arrrrr){
  12. FILE *input = fopen(filename, "r");
  13. if (input == NULL){
  14. printf("Cannot open input file \n");
  15. exit(0);
  16. }
  17. else{
  18. int counter = 0;
  19. while (!feof(input)){
  20. if (fgetc(input) == '\n')
  21. counter++;
  22. }
  23. fseek(input, 0, SEEK_SET);
  24. countofpoints = counter + 1;
  25. arrayofpoints = (struct point *)malloc(countofpoints * sizeof(struct point));
  26. for (int i = 0; i < countofpoints; i++){
  27. fscanf(input, "%f %f", &arrayofpoints[i].x, &arrayofpoints[i].y);
  28. }
  29. printf("Our points : \n");
  30. for (int i = 0; i < countofpoints; i++){
  31. printf("struct point[%d] : %f %f\n", i, arrayofpoints[i].x, arrayofpoints[i].y);
  32. }
  33. }
  34. return arrayofpoints;
  35. }
  36.  
  37. int checkthis(struct point *arrrrr, int countofpoints, int x, int y){
  38. int result = 1;
  39. int j = countofpoints - 1;
  40. //check dot in figure
  41. for (int i = 0; i < countofpoints; i++) {
  42. if ((arrrrr[i].y < y && arrrrr[j].y >= y || arrrrr[j].y < y && arrrrr[i].y >= y) &&
  43. (arrrrr[i].x + (y - arrrrr[i].y) / (arrrrr[j].y - arrrrr[i].y) * (arrrrr[j].x - arrrrr[i].x) < y))
  44. result = 0;
  45. j = i;
  46. }
  47. //check dot in dot
  48. if (result == 1)
  49. for (int i = 0; i < countofpoints; i++){
  50. if (arrayofpoints[i].y == y && arrayofpoints[i].x == x)
  51. result = 0;
  52. }
  53. //check dot in line
  54. if (result == 1)
  55. for (int i = 0; i < countofpoints - 1; i++){
  56. if (((y - arrrrr[i].y) / (arrrrr[i + 1].y - arrrrr[i].y)) == ((x - arrrrr[i].x) / (arrrrr[i + 1].x - arrrrr[i].x)))
  57. result = 0;
  58. }
  59. if (result == 1)
  60. return 1;
  61. else return 0;
  62. }
  63.  
  64. void seconddir(){
  65. int target = 1;
  66. char numb1[20];
  67. char numb2[20];
  68. int counterofpoints = 0;
  69. loadthis("ourfigure.txt", counterofpoints, arrayofpoints);
  70.  
  71. while (target == 1){
  72. printf("x y: ");
  73. scanf("%s", &*numb1);
  74. if (!strcmp(numb1, "end")){
  75. target = 0;
  76. break;
  77. }
  78. scanf("%s", &*numb2);
  79. dot.x = atof(numb1);
  80. dot.y = atof(numb2);
  81. if (checkthis(arrayofpoints, counterofpoints, dot.x, dot.y) == 0)
  82. printf("YES!\n");
  83. else
  84. printf("NO\n");
  85. }
  86. free(arrayofpoints);
  87. }
  88.  
  89. void thirddir(){
  90. int target = 1;
  91. int targett = 1;
  92. char numb1[20];
  93. char numb2[20];
  94. char numb3[20];
  95. int counterofpoints = 0;
  96. while (target == 1){
  97. printf("x y z: ");
  98. scanf("%s", &*numb1);
  99. if (!strcmp(numb1, "end")){
  100. target = 0;
  101. break;
  102. }
  103. scanf("%s", &*numb2);
  104. scanf("%s", &*numb3);
  105. dot.x = atof(numb1);
  106. dot.y = atof(numb2);
  107. dot.z = atof(numb3);
  108. loadthis("top.txt", counterofpoints, arrayofpoints);
  109. if (checkthis(arrayofpoints, counterofpoints, dot.x, dot.z) == 1)
  110. targett = 1;
  111. free(arrayofpoints);
  112. loadthis("left.txt", counterofpoints, arrayofpoints);
  113. if (checkthis(arrayofpoints, counterofpoints, dot.z, dot.y) == 1)
  114. targett = 1;
  115. free(arrayofpoints);
  116. loadthis("front.txt", counterofpoints, arrayofpoints);
  117. if (checkthis(arrayofpoints, counterofpoints, dot.x, dot.y) == 1)
  118. targett = 1;
  119. free(arrayofpoints);
  120. if (targett == 0)
  121. printf("YES!\n");
  122. else
  123. printf("NO\n");
  124. targett = 0;
  125. }
  126. }
  127.  
  128. void menu(){
  129. printf("\n\n\n\t\MENU\n");
  130. printf("Work with 2D{1}\n");
  131. printf("Work with 3D{2}\n");
  132. int choose = 0;
  133. scanf("%d", &choose);
  134. switch (choose){
  135. case(1) : {
  136. seconddir();
  137. menu();
  138. break;
  139. }
  140. case(2) : {
  141. thirddir();
  142. menu();
  143. break;
  144. } break;
  145. default:{
  146. printf("Bad choose!\n");
  147. menu();
  148. }
  149. }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment