Advertisement
perjespersson

TDDI16 - Labb 4

Oct 13th, 2019
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. /*
  2. * brute < input.txt
  3. *
  4. * Compute and plot all line segments involving 4 points in input.txt using SDL
  5. */
  6.  
  7. #include <iostream>
  8. #include <algorithm>
  9. #include <vector>
  10. #include <chrono>
  11. #include <signal.h>
  12. #include "SDL/SDL.h"
  13. #include "point.h"
  14. #include <map>
  15.  
  16. using namespace std;
  17.  
  18. void render_points(SDL_Surface *screen, const vector<Point> &points) {
  19. SDL_LockSurface(screen);
  20.  
  21. for(const auto& point : points) {
  22. point.draw(screen);
  23. }
  24.  
  25. SDL_FreeSurface(screen);
  26. SDL_Flip(screen); // display screen
  27. }
  28.  
  29. void render_line(SDL_Surface *screen, const Point &p1, const Point &p2) {
  30. SDL_LockSurface(screen);
  31.  
  32. p1.lineTo(screen, p2);
  33.  
  34. SDL_FreeSurface(screen);
  35. SDL_Flip(screen); // display screen
  36. }
  37.  
  38. int main(int argc, char *argv[]) {
  39. if (argc != 1) {
  40. cout << "Usage:" << endl << argv[0] << " < input.txt" << endl;
  41.  
  42. return 0;
  43. }
  44.  
  45. // we only need SDL video
  46. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  47. fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
  48. exit(1);
  49. }
  50.  
  51. // Allow terminating using Ctrl+C.
  52. signal(SIGINT, SIG_DFL);
  53.  
  54. // register SDL_Quit to be called at exit
  55. atexit(SDL_Quit);
  56.  
  57. // set window title
  58. SDL_WM_SetCaption("Pointplot", 0);
  59.  
  60. // Set the screen up
  61. SDL_Surface* screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
  62.  
  63. if (screen == nullptr) {
  64. fprintf(stderr, "Unable to set 512x512 video: %s\n", SDL_GetError());
  65. exit(1);
  66. }
  67.  
  68. // clear the screen with white
  69. SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
  70.  
  71. // the array of points
  72. vector<Point> points;
  73.  
  74. // read points from cin
  75. int N;
  76. unsigned int x;
  77. unsigned int y;
  78.  
  79. cin >> N;
  80.  
  81. for (int i{0}; i < N; ++i) {
  82. cin >> x >> y;
  83. points.push_back(Point(x, y));
  84. }
  85.  
  86. // draw points to screen all at once
  87. render_points(screen, points);
  88.  
  89. // sort points by natural order
  90. // makes finding endpoints of line segments easy
  91. sort(points.begin(), points.end());
  92.  
  93. /////////////////////////////////////////////////////////////////////////////
  94. // Draw any lines that you find in 'points' using the function 'render_line'.
  95. /////////////////////////////////////////////////////////////////////////////
  96. // vector<Point> points2{points};
  97.  
  98. while(points.size() > 2) // För att bilda en linje måste vi hitta minst 3 punkter i rad och kan därför hoppa ut när det bara finns 2 kvar att söka igenom.
  99. {
  100. map<double, vector<Point>> result;
  101. Point first = points[0];
  102. for(int i = 0; i < points.size(); i++) // n-1? Size minskar med ett hela tiden
  103. {
  104. result[first.slopeTo(points[i])].push_back(points[i]);
  105. }
  106.  
  107. for(auto v : result) //n-1 igen kankse då size frf minskar med ett?
  108. {
  109. if(v.second.size()> 2)
  110. render_line(screen, first, v.second.at(v.second.size()-1));
  111. }
  112.  
  113. points.erase(points.begin()); // Eftersom vi börjar längst till vänster och alltid kollar "framåt" behöver vi bara undersöka punken en gång
  114.  
  115.  
  116.  
  117. /* OLD ONE
  118. Point first = points[0];
  119. std::sort(points2.begin(), points2.end(), [&first](Point a, Point b) {
  120. return first.slopeTo(a) > first.slopeTo(b);
  121. });
  122.  
  123. /*render_line(screen, first, to);
  124. points.erase(points.begin());
  125.  
  126. //=============================================//
  127. //Hittar vi tre i rad ska vi skriva ut - ska vara samma lutning mot first
  128.  
  129.  
  130. int count{0};
  131. Point to{0,0};
  132.  
  133. for(int i=0; i < points2.size(); i++)
  134. {
  135. count++;
  136. if(first.slopeTo(points2[i]) == first.slopeTo(points2[i+1]))
  137. {
  138.  
  139.  
  140. }
  141. else
  142. {
  143. to = points2[i];
  144. if(count > 2) //ÄNDRA SNART;
  145. {
  146. render_line(screen, first, to);
  147. }
  148. count=0;
  149. }
  150. }
  151. */
  152. }
  153.  
  154.  
  155.  
  156.  
  157.  
  158. auto end = chrono::high_resolution_clock::now();
  159. cout << "Computing line segments took "
  160. << std::chrono::duration_cast<chrono::milliseconds>(end - begin).count()
  161. << " milliseconds." << endl;
  162.  
  163. // wait for user to terminate program
  164. while (true) {
  165. SDL_Event event;
  166. while (SDL_PollEvent(&event)) {
  167. switch (event.type) {
  168. case SDL_KEYDOWN:
  169. break;
  170. case SDL_KEYUP:
  171. if (event.key.keysym.sym == SDLK_ESCAPE)
  172. return 0;
  173. break;
  174. case SDL_QUIT:
  175. return(0);
  176. }
  177. }
  178. }
  179.  
  180. return 0;
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement