-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fredrik Jonsén
committed
Oct 21, 2015
1 parent
92d9ee8
commit f48ae54
Showing
5 changed files
with
178 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
/* | ||
* 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 "SDL/SDL.h" | ||
#include "Point.h" | ||
#include <map> | ||
|
||
using namespace std; | ||
using match_map = map<double, vector<Point>>; | ||
|
||
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); | ||
} | ||
|
||
// 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()); | ||
|
||
auto begin = chrono::high_resolution_clock::now(); | ||
|
||
|
||
|
||
for(int p = 0; p < N-3; ++p) { | ||
match_map matches; | ||
for(int j = p+1; j < N; ++j) { | ||
matches[points.at(p).slopeTo(points.at(j))].push_back(points.at(j)); | ||
} | ||
|
||
for (match_map::iterator it = matches.begin(); it != matches.end(); ++it) { | ||
if (it->second.size() >= 3) { | ||
it->second.push_back(points.at(p)); | ||
sort(it->second.begin(), it->second.end()); | ||
render_line(screen, it->second.front(), it->second.back()); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,40 @@ | ||
/********************************************************************** | ||
* M�nsterigenk�nning readme.txt | ||
* Mönsterigenkänning readme.txt | ||
**********************************************************************/ | ||
|
||
Ungef�rligt antal timmar spenderade p� labben (valfritt): | ||
Ungefärligt antal timmar spenderade på labben (valfritt): | ||
|
||
/********************************************************************** | ||
* Empirisk Fyll i tabellen nedan med riktiga k�rtider i sekunder | ||
* analys n�r det k�nns vettigt att v�nta p� hela ber�kningen. | ||
* Ge uppskattningar av k�rtiden i �vriga fall. | ||
* Empirisk Fyll i tabellen nedan med riktiga körtider i sekunder | ||
* analys när det känns vettigt att vänta på hela beräkningen. | ||
* Ge uppskattningar av körtiden i övriga fall. | ||
* | ||
**********************************************************************/ | ||
|
||
N brute sortering | ||
---------------------------------- | ||
150 | ||
200 | ||
300 | ||
400 | ||
800 | ||
1600 | ||
3200 | ||
6400 | ||
12800 | ||
150 <1 <1 | ||
200 <1 <1 | ||
300 <1 <1 | ||
400 1 <1 | ||
800 9 <1 | ||
1600 70 2 | ||
3200 565 10 | ||
6400 4552 41 | ||
12800 174 | ||
|
||
|
||
/********************************************************************** | ||
* Teoretisk Ge ordo-uttryck f�r v�rstafallstiden f�r programmen som | ||
* Teoretisk Ge ordo-uttryck för värstafallstiden för programmen som | ||
* analys en funktion av N. Ge en kort motivering. | ||
* | ||
**********************************************************************/ | ||
|
||
Brute: | ||
Brute: O(N^4). Algoritmen består av fyra loopar. Det fjärde loopen körs | ||
visserligen endast då if-satsen är sann, men eftersom det handlar om | ||
värstafallet, räknar vi med att if-satsen är sann varje gång (i.e. alla | ||
punkterna är på en lång rad) | ||
|
||
Sortering: | ||
Sortering: O(N^2). Algoritmen består av en yttre loop, med två inre loopar. Eftersom | ||
de två inre är seriella och inte rekursiva blir deras komplexitet | ||
2N och inte N^2. Funktionen blir därmed N(N + N) = 2*N^2, vilket blir O(N^2) |