ParanoidPanda

Untitled

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