commit 197fda4c7f673a64fef9d957a280c7b9b9c97e14 Author: Steffen Vogel Date: Tue Aug 17 01:22:08 2010 +0200 initial import diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f16baa8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.settings +.cproject +.project +.classpath + diff --git a/lacube.cpp b/lacube.cpp new file mode 100644 index 0000000..70f82ee --- /dev/null +++ b/lacube.cpp @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "lacube.h" + +int main(int argc, char *argv[]) { + double alpha = 0; + int deg = 0; + double rot[2][2]; + + SDL_Event event; + + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) < 0) { + std::cerr << "SDL konnte nicht initialisiert werden: " + << SDL_GetError() << std::endl; + exit(1); + } + atexit(SDL_Quit); + + SDL_WM_SetCaption("Basic matrix operations", "Basic matrix operations"); + + SDL_Surface *screen; + + screen = SDL_SetVideoMode(screen_width, screen_height, 16, SDL_SWSURFACE); + if (screen == NULL) { + std::cerr << "Ich konnte kein Fenster öffnen: " << SDL_GetError() + << std::endl; + exit(1); + } + + kos kos(screen_width, 10); + + kos.add_point(10, 10); + kos.add_point(-10, 10); + kos.add_point(10, -10); + kos.add_point(-10, -10); + + deg = 1; + alpha = M_PI / 180 * deg; // rotate with 12 deg + + while (true) { + alpha = M_PI / 180 * deg; // rotate with deg degrees + + rot[0][0] = cos(alpha); + rot[0][1] = -sin(alpha); + rot[1][0] = sin(alpha); + rot[1][1] = cos(alpha); + + kos.execute(rot); + kos.draw(screen); + + while (SDL_PollEvent(&event)) { /* Loop until there are no events left on the queue */ + switch (event.type) { /* Process the appropiate event type */ + case SDL_QUIT: + std::cout << "Good bye!" << std::endl; + return 0; + break; + case SDL_MOUSEBUTTONUP: + if (event.button.button == SDL_BUTTON_LEFT) { + float x = (event.button.x - kos.x0) / kos.scale; + float y = (event.button.y - kos.y0) / kos.scale; + std::cout << "you clicked -> new point will be added: " << x << " " << y << std::endl; + kos.add_point(x, y); + } + break; + case SDL_KEYUP: + switch (event.key.keysym.sym) { + case SDLK_UP: + deg++; + break; + case SDLK_DOWN: + deg--; + break; + case SDLK_q: + return 0; + break; + case SDLK_c: + kos.points.clear(); + break; + default: + std::cout << "unhandled key: " << event.key.keysym.sym << std::endl; + } + break; + } + } + + SDL_Delay(50); +} + +return 0; +} + +void kos::add_point(double x, double y) { + std::vector point(2); + point[0] = x; + point[1] = y; + + points.push_back(point); +} + +void kos::draw(SDL_Surface *screen) { + + // clear screen + SDL_FillRect(screen, NULL, 0xffffffff); + + // axes + draw_axes(screen); + + // lines + + // points + for (std::vector >::iterator i = points.begin(); i + != points.end(); i++) { + draw_point(*i, screen); + } + + SDL_Flip(screen); // refresh screen +} + +kos::kos(int _max, int _scale) { + max = _max; + scale = _scale; + + x0 = screen_width / 2; + y0 = screen_height / 2; +} + +void kos::draw_point(std::vector point, SDL_Surface *screen) { + filledEllipseColor(screen, x0 + (point[0] * scale), + y0 + (point[1] * scale), 4, 4, point_color); +} + +void kos::draw_axes(SDL_Surface *screen) { + lineColor(screen, screen_padding, y0, screen_width - screen_padding, y0, + axes_color); // x-Achse + lineColor(screen, x0, screen_padding, x0 - 5, screen_padding + 5, + axes_color); // Pfeile + lineColor(screen, x0, screen_padding, x0 + 5, screen_padding + 5, + axes_color); + + for (int i = 0; i < ((screen_width / 2) - screen_padding) / scale; i++) { + lineColor(screen, x0 + i * scale, y0 - 2, x0 + i * scale, y0 + 2, + axes_color); // x-Skalierung + lineColor(screen, x0 - i * scale, y0 - 2, x0 - i * scale, y0 + 2, + axes_color); + + lineColor(screen, x0 - 2, y0 + i * scale, x0 + 2, y0 + i * scale, + axes_color); // y-Skalierung + lineColor(screen, x0 - 2, y0 - i * scale, x0 + 2, y0 - i * scale, + axes_color); + } + + lineColor(screen, x0, screen_padding, x0, screen_height - screen_padding, + axes_color); // y-Achse + lineColor(screen, screen_width - screen_padding, x0, screen_width + - screen_padding - 5, y0 - 5, axes_color); // Pfeile + lineColor(screen, screen_width - screen_padding, y0, screen_width + - screen_padding - 5, y0 + 5, axes_color); + + //lineColor(screen, 0, screen_height, screen_width, 0, axes_color); // z-Achse + +} + +void kos::execute(double mat[2][2]) { + for (std::vector >::iterator i = points.begin(); i + != points.end(); i++) { + std::vector npoint(2, 0); + + for (int p = 0; p < 2; p++) { + for (int q = 0; q < 2; q++) { + npoint[p] += i->at(q) * mat[p][q]; + } + } + + *i = npoint; + } +} diff --git a/lacube.h b/lacube.h new file mode 100644 index 0000000..01054ff --- /dev/null +++ b/lacube.h @@ -0,0 +1,34 @@ +#ifndef LACUBE_H_ +#define LACUBE_H_ + +#include + +int main(int argc, char *argv[]); + +const int screen_height = 500; +const int screen_width = screen_height; +const int screen_padding = 10; + +class kos { + + static const int point_color = 0x000000ff; + static const int axes_color = 0x000000ff; + +public: + kos(int max, int scale); + void add_point(double x, double y); + void draw(SDL_Surface *screen); + void execute(double mat[2][2]); + double scale; + int max; + int x0; + int y0; + std::vector > points; + +private: + void draw_axes(SDL_Surface *screen); + void draw_point(std::vector point, SDL_Surface *screen); + void draw_line(SDL_Surface *screen); +}; + +#endif /*LACUBE_H_*/