Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. #include<windows.h>
  2. #include<GL/Glut.h> //includes the opengl, glu, and glut header files
  3. #include<stdlib.h> //includes the standard library header file
  4. #include<iostream>
  5. #include<fstream>
  6. #include<vector>
  7.  
  8. using namespace std;
  9.  
  10. char path[] = "D:\\Andrei\\Facultate\\AN 2\\Geometrie computationala\\Proiect\\Proiect\\date.in";
  11. ifstream fin(path);
  12.  
  13. struct Point{
  14. double x , y;
  15. };
  16.  
  17. vector <Point> v;
  18. Point newPoint; // the new point that will be added
  19.  
  20. int vSize; // size of the v vector
  21.  
  22. void read(vector <Point> &v , int &n , Point &a)
  23. {
  24. //read the number of vertex and the vertices
  25. if(!fin)
  26. {
  27. cout<<"Cannot open file\n";
  28. exit(1);
  29. }
  30. fin>>n;
  31. v.resize(n);
  32. for(int i = 0 ; i < n ; i++)
  33. fin>>v[i].x>>v[i].y;
  34.  
  35. }
  36.  
  37.  
  38. double distance(Point A , Point B)
  39. {
  40. //calculate the square of the distance
  41. return (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y);
  42. }
  43.  
  44.  
  45.  
  46. // To find orientation of ordered triplet (p, q, r).
  47. // The function returns following values
  48. // 0 --> p, q and r are colinear
  49. // 1 --> Clockwise
  50. // -1 --> Counterclockwise
  51. int orientation(Point p, Point q, Point r)
  52. {
  53. int val = (q.y - p.y) * (r.x - q.x) -
  54. (q.x - p.x) * (r.y - q.y);
  55.  
  56. if (val == 0) return 0; // colinear
  57. return (val > 0)? 1: -1; // clock or counterclock wise
  58. }
  59.  
  60. int findIndexOfMinDistance(vector <Point> v , int vSize , Point newPoint)
  61. {
  62. //search the ith vertex with distance(ith vertex , the added point) is minimum
  63. //return the index with that property
  64. double minDistance = distance(newPoint, v[0]);
  65. int indexMinDistance = 0;
  66.  
  67. for(int i = 1 ; i < vSize ; i++)
  68. {
  69. int newDistance = distance(newPoint, v[i]);
  70. if(newDistance < minDistance)
  71. {
  72. minDistance = newDistance;
  73. indexMinDistance = i;
  74. }
  75. }
  76. return indexMinDistance;
  77. }
  78.  
  79. // prints the points
  80. void printVector(vector <Point> v , int vSize)
  81. {
  82. for(int i = 0 ; i < vSize ; i++)
  83. cout<<v[i].x<<' '<<v[i].y<<" ";
  84. }
  85.  
  86. // Returns true if the point p lies inside the polygon[] with n vertices
  87. bool isInside(vector <Point> v , int vSize, Point newPoint)
  88. {
  89. // There must be at least 3 vertices in polygon[]
  90. if (vSize < 3) return false;
  91.  
  92. for(int i = 0 ; i < vSize ; i++)
  93. {
  94. if(newPoint.x == v[i].x && newPoint.y == v[i].y) //we consider that a vertex of a polygon is inside it
  95. return true;
  96. //we say that a point is inside a polygon
  97. //if p is always on the left side of an edge
  98. if(orientation(newPoint , v[i] , v[(i + 1) % vSize]) >= 0) //clockwise order. pi is not on the left side of edge vi v(i+1)%n
  99. return false; //so p is not inside the polygon
  100. }
  101. return true;
  102. }
  103.  
  104.  
  105. void addPointToConvexHull(vector <Point> &v , int &vSize , Point newPoint)
  106. {
  107. // function that adds a point (if it's not inside the convexHull) to the convex Hull, maintaining the
  108. // points in the counter-clockwise order
  109. if(isInside(v, vSize, newPoint)) {cout<<"INSIDE\n";return ;} // if the point is inside the convex Hull, nothing happens
  110.  
  111. int upper = 0; // the upper tangent point from newPoint to the convex hull
  112. int lower = 0; // the lower tangent point from newPoint to the convex hull
  113.  
  114. for(int i = 1 ; i < vSize ; i++)
  115. {
  116. int test1 = orientation(newPoint , v[i - 1] , v[i]);
  117. int test2 = orientation(newPoint , v[i] , v[(i + 1) % vSize]);
  118.  
  119. if(test1 >= 0 && test2 < 0)
  120. {
  121. upper = i;
  122. }
  123. if(test1 < 0 && test2 >= 0)
  124. {
  125. lower = i;
  126. }
  127.  
  128. }
  129.  
  130. int current = upper;
  131. vector <Point> aux;
  132. aux.push_back(v[current]);
  133.  
  134. while (current != lower) // adds the points to the convex hull
  135. {
  136. current = (current + 1) % vSize;
  137. aux.push_back(v[current]);
  138. }
  139.  
  140. // Modifies the original vector, keeping only the ones that are part of the convex hull
  141. aux.push_back(newPoint); // the point will be the last one in the new convex hull
  142. v.clear();
  143.  
  144. for (int i = 0 ; i < aux.size() ; i++)
  145. v.push_back(aux[i]);
  146. vSize = aux.size();
  147. }
  148.  
  149. void display()
  150. {
  151. glClear(GL_COLOR_BUFFER_BIT);
  152. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  153.  
  154. // prints the polygon
  155. glBegin(GL_POLYGON);
  156. for(int i = 0 ; i < vSize ;i++)
  157. glVertex2f(v[i].x , v[i].y);
  158. glEnd();
  159.  
  160. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  161. glFlush();
  162. }
  163.  
  164. void reshape(GLsizei width, GLsizei height) {
  165.  
  166. // Set the viewport to cover the new window
  167. glViewport(0, 0, width, height);
  168. glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
  169. glLoadIdentity(); // Reset the projection matrix
  170. gluOrtho2D(-width/ 2.0, width/ 2.0,-height / 2.0 ,height / 2.0); // compute the window's size to the those dimensions
  171.  
  172. }
  173.  
  174.  
  175. void mouseClick(int button, int state, int x, int y)
  176. {
  177. if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) //if the left button is pressed
  178. {
  179. double windowHeightSize = glutGet(GLUT_WINDOW_HEIGHT); // gets the window's dimensions
  180. double windowWidthSize = glutGet(GLUT_WINDOW_WIDTH);
  181.  
  182. // computes the coordinates of the point, converted for the window's size
  183. newPoint.x = x - (double)windowWidthSize /2.0;
  184. newPoint.y = (double)windowHeightSize /2.0- y;
  185. addPointToConvexHull(v, vSize, newPoint);
  186. display();
  187. }
  188. }
  189.  
  190. int main(int argc,char** argv)
  191. {
  192. read(v, vSize, newPoint); // reads the convex hull
  193. glutInit(&argc, argv);
  194. glutInitWindowSize(800 , 600); // sets the window with the default 800 x 600 dimensions
  195. glutInitWindowPosition(50 , 50);//sets the position of the window in pixels from top left corner
  196. glutCreateWindow("Adding a Point to a Convex Hull"); // creates the window
  197. glutDisplayFunc(display); // function that displays the window
  198. glutReshapeFunc(reshape); // function that reshapes the window
  199. glutMouseFunc(mouseClick); // function that tracks the click on the screen
  200. glutMainLoop(); //loops the current event
  201.  
  202. return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement