Guest User

Untitled

a guest
Dec 14th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4.  
  5. typedef struct point2D {
  6. int x, y;
  7. }point2D;
  8.  
  9. typedef struct line2D {
  10. point2D *start;
  11. point2D *end;
  12. }line2D;
  13.  
  14. typedef struct polygon2D {
  15. int edge_count;
  16. int available;
  17. line2D** lines;
  18. }polygon2D;
  19.  
  20. point2D* createPoint(int x, int y) {
  21. point2D* point = (point2D*)malloc(sizeof(point2D));
  22. point->x = x;
  23. point->y = y;
  24. return point;
  25. }
  26.  
  27. line2D* createLine(point2D* p1, point2D* p2) {
  28. line2D * line = (line2D*)malloc(sizeof(line2D));
  29. line->start = p1;
  30. line->end = p2;
  31. return line;
  32. }
  33.  
  34. polygon2D* createEmptyPolygon(int edge_count) {
  35. polygon2D* polygon = (polygon2D*)malloc(sizeof(polygon2D));
  36. polygon->edge_count = edge_count;
  37. polygon->lines = (line2D**)malloc(polygon->edge_count * sizeof(line2D*));
  38. return polygon;
  39. }
  40.  
  41. int checkLinesCrossing(line2D * l1, line2D * l2) {
  42. //Line-1
  43. int x1 = l1->start->x;
  44. int y1 = l1->start->y;
  45. int x2 = l1->end->x;
  46. int y2 = l1->end->y;
  47. //Line-2
  48. int x3 = l2->start->x;
  49. int y3 = l2->start->y;
  50. int x4 = l2->end->x;
  51. int y4 = l2->end->y;
  52.  
  53. //Slopes
  54. int A1 = (y1 - y2) / (x1 - x2);
  55. int A2 = (y3 - y4) / (x3 - x4);
  56.  
  57. //intersection points
  58. int b1 = y1 - (A1 * x1);
  59. int b2 = y3 - (A2 * x3);
  60.  
  61. //intersection point
  62. int Xa = (b2 - b1) / (A1 - A2);
  63.  
  64. //basic non-intersection
  65. if (max(x1, x2) < min(x3, x4)) {
  66. return 0;
  67. }
  68. //lines parallel
  69. else if (A1 == A2) {
  70. return 0;
  71. }
  72. //intersection out of bounds
  73. else if((Xa < max(min(x1, x2), min(x3, x4))) || (Xa < min(max(x1,x2), max(x3,x4)))){
  74. return 0;
  75. }
  76.  
  77. return 1;
  78. }
  79.  
  80. void checkPolygonRules(polygon2D* polygon) {
  81.  
  82. if (polygon->lines[0]->start->x != polygon->lines[polygon->edge_count - 1]->end->x &&
  83. polygon->lines[0]->start->y != polygon->lines[polygon->edge_count - 1]->end->y) {
  84. printf("Start and End aren't same point !!! \n");
  85. }
  86. else {
  87. for (int i = 0; i < polygon->edge_count - 1, polygon->available; i++) {
  88. for (int j = i; j < polygon->edge_count; j++) {
  89. int isCrossingLines = checkLinesCrossing(polygon->lines[i], polygon->lines[j]);
  90. if (isCrossingLines) {
  91. printf("%d. and %d. lines are crossing eachother!!! \n", i, j);
  92. polygon->available = 0;
  93. break;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. if (polygon->available) {
  100. printf("This points means a closed polygon!!! \n");
  101. }
  102. }
  103.  
  104. int main(void) {
  105. int vertex_count;
  106.  
  107. point2D* target_point = (point2D*)malloc(sizeof(point2D));
  108. target_point->x = 2;
  109. target_point->y = 3;
  110.  
  111. printf("Total Vertex Count : ");
  112. scanf("%d", &vertex_count);
  113.  
  114. point2D** points = (point2D**)malloc(vertex_count*sizeof(point2D*));
  115. for (int i = 0; i < vertex_count; i++) {
  116. points[i] = (point2D*)malloc(sizeof(point2D));
  117. }
  118.  
  119. polygon2D * polygon = createEmptyPolygon(vertex_count - 1);
  120.  
  121. for (int i = 0; i < vertex_count; i++) {
  122. printf("x : ");
  123. scanf("%d", &points[i]->x);
  124. printf("y : ");
  125. scanf("%d", &points[i]->y);
  126. }
  127.  
  128. for (int i = 0; i < vertex_count; i++) {
  129. printf("%d - %d\n", i, ((i + 1) % vertex_count));
  130. line2D * line = createLine(points[i], points[(i+1) % vertex_count]);
  131. polygon->lines[i] = line;
  132. getchar();
  133. }
  134.  
  135. checkPolygonRules(polygon);
  136.  
  137. getchar();
  138. return 0;
  139. }
Add Comment
Please, Sign In to add comment