Skip to content

Commit

Permalink
ALL IS WELL
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredrik Jonsén committed Oct 21, 2015
1 parent 92d9ee8 commit f48ae54
Show file tree
Hide file tree
Showing 5 changed files with 178 additions and 26 deletions.
5 changes: 4 additions & 1 deletion lab4/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
# Makefile för bruteforcelösning
#

CCC = g++ -std=c++11
CCC = g++ -std=c++11
LFLAGS = -L/usr/lib/x86_64-linux-gnu -lSDL -lm

all: brute.cc Point.o
$(CCC) -o brute brute.cc Point.o $(LFLAGS)

fast: fast.cc Point.o
$(CCC) -o fast fast.cc Point.o $(LFLAGS)

Point.o: Point.cc Point.h
$(CCC) -c Point.cc

Expand Down
Binary file removed lab4/brute
Binary file not shown.
14 changes: 7 additions & 7 deletions lab4/brute.cc
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ main(int argc, char* argv[])
for (int i{0} ; i < N-3 ; ++i) {
for (int j{i+1} ; j < N-2 ; ++j) {
for (int k{j+1} ; k < N-1 ; ++k) {
//only consider fourth point if first three are collinear
if (points.at(i).slopeTo(points.at(j)) == points.at(i).slopeTo(points.at(k))) {
for (int m{k+1} ; m < N ; ++m) {
if (points.at(i).slopeTo(points.at(j)) == points.at(i).slopeTo(points.at(m))) {
render_line(screen, points.at(i), points.at(m));
//only consider fourth point if first three are collinear
if (points.at(i).slopeTo(points.at(j)) == points.at(i).slopeTo(points.at(k))) {
for (int m{k+1} ; m < N ; ++m) {
if (points.at(i).slopeTo(points.at(j)) == points.at(i).slopeTo(points.at(m))) {
render_line(screen, points.at(i), points.at(m));
}
}
}
}
}
}
}
}
Expand Down
144 changes: 144 additions & 0 deletions lab4/fast.cc
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;
}
41 changes: 23 additions & 18 deletions lab4/readme.txt
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 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)

0 comments on commit f48ae54

Please sign in to comment.