Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * brute < input.txt
- *
- * Compute and plot all line segments involving 4 points in input.txt using SDL
- */
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <chrono>
- #include <signal.h>
- #include "SDL/SDL.h"
- #include "point.h"
- #include <map>
- using namespace std;
- void render_points(SDL_Surface *screen, const vector<Point> &points) {
- SDL_LockSurface(screen);
- for(const auto& point : points) {
- point.draw(screen);
- }
- SDL_FreeSurface(screen);
- SDL_Flip(screen); // display screen
- }
- void render_line(SDL_Surface *screen, const Point &p1, const Point &p2) {
- SDL_LockSurface(screen);
- p1.lineTo(screen, p2);
- SDL_FreeSurface(screen);
- SDL_Flip(screen); // display screen
- }
- int main(int argc, char *argv[]) {
- if (argc != 1) {
- cout << "Usage:" << endl << argv[0] << " < input.txt" << endl;
- return 0;
- }
- // we only need SDL video
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
- exit(1);
- }
- // Allow terminating using Ctrl+C.
- signal(SIGINT, SIG_DFL);
- // register SDL_Quit to be called at exit
- atexit(SDL_Quit);
- // set window title
- SDL_WM_SetCaption("Pointplot", 0);
- // Set the screen up
- SDL_Surface* screen = SDL_SetVideoMode(512, 512, 32, SDL_SWSURFACE);
- if (screen == nullptr) {
- fprintf(stderr, "Unable to set 512x512 video: %s\n", SDL_GetError());
- exit(1);
- }
- // clear the screen with white
- SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
- // the array of points
- vector<Point> points;
- // read points from cin
- int N;
- unsigned int x;
- unsigned int y;
- cin >> N;
- for (int i{0}; i < N; ++i) {
- cin >> x >> y;
- points.push_back(Point(x, y));
- }
- // draw points to screen all at once
- render_points(screen, points);
- // sort points by natural order
- // makes finding endpoints of line segments easy
- sort(points.begin(), points.end());
- /////////////////////////////////////////////////////////////////////////////
- // Draw any lines that you find in 'points' using the function 'render_line'.
- /////////////////////////////////////////////////////////////////////////////
- // vector<Point> points2{points};
- 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.
- {
- map<double, vector<Point>> result;
- Point first = points[0];
- for(int i = 0; i < points.size(); i++) // n-1? Size minskar med ett hela tiden
- {
- result[first.slopeTo(points[i])].push_back(points[i]);
- }
- for(auto v : result) //n-1 igen kankse då size frf minskar med ett?
- {
- if(v.second.size()> 2)
- render_line(screen, first, v.second.at(v.second.size()-1));
- }
- 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
- /* OLD ONE
- Point first = points[0];
- std::sort(points2.begin(), points2.end(), [&first](Point a, Point b) {
- return first.slopeTo(a) > first.slopeTo(b);
- });
- /*render_line(screen, first, to);
- points.erase(points.begin());
- //=============================================//
- //Hittar vi tre i rad ska vi skriva ut - ska vara samma lutning mot first
- int count{0};
- Point to{0,0};
- for(int i=0; i < points2.size(); i++)
- {
- count++;
- if(first.slopeTo(points2[i]) == first.slopeTo(points2[i+1]))
- {
- }
- else
- {
- to = points2[i];
- if(count > 2) //ÃNDRA SNART;
- {
- render_line(screen, first, to);
- }
- count=0;
- }
- }
- */
- }
- auto end = chrono::high_resolution_clock::now();
- cout << "Computing line segments took "
- << std::chrono::duration_cast<chrono::milliseconds>(end - begin).count()
- << " milliseconds." << endl;
- // wait for user to terminate program
- while (true) {
- SDL_Event event;
- while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_KEYDOWN:
- break;
- case SDL_KEYUP:
- if (event.key.keysym.sym == SDLK_ESCAPE)
- return 0;
- break;
- case SDL_QUIT:
- return(0);
- }
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement