Metody Informatyczne w Fizyce i Technice
Uczymy się jezyka C++. Programy kompilujemy używając GNU g++ w nastepujący sposób
g++ zrodlo.cpp -o nazwa_programu
Zalecana literatura:
- Symfonia Grębosza (klasyk)
- Stephen Prata, Język C++. Szkoła programowania, wyd. Helion, kiedyś Robomatic.
- stackoverflow.com
Klasy
(ostatnia aktualizacja śro 30 mar 19:23:18 2016 CEST)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | #include <iostream>
#include <string> //nowość, żeby nie męczyć się ze zmiennymi typu char*
#include <math.h> //nowość, potrzbne funkcje matematyczne zaimplenetowane są tutaj
//c++ nie wie co to jest sin...
using namespace std;
class Point
{
private: //zmienne prywatne klasy, niedostępne "z zewnątrz"
double x;
double y;
string opis;
public: //metody publiczne, dostępne "dla każdego"
//wariant 1 poczatek
//Point(const double& _x, const double& _y); //konstruktor 1
//wariant 1 koniec
// UWAGA, NALEŻY WYBRAĆ MIĘDZY 1 A 2!!!
//wariant 2 poczatek
Point(const double& _x, const double& _y) : x(_x), y(_y) {};
//wariant 2 koniec
//ten wariant to tzw. lista inicjalizacyjna konstruktora
Point(const double& _x, const double& _y, string opis); //konstruktor 2
Point(void); //konstruktor 3
double retX();
double retY();
void showCoords(); //funkcja zwracająca współrzędne punktu
void przesun(Point wektor); //przesuwanie punktu o wektor (typu Point)
void przesun(double wektor[2]); // przesuwanie punktu o wektor (jawny)
void obroc(double kat); //obrót o "kat"
void obroc(double kat, Point& srodek); //obrót o "kat" wokol innego punktu
void ustaw_opis(string opis); //ustawienie opisu punktu, niepotrzebne, ale ciekawe
Point operator*(double scalar); //dodawanie 2 punktów (jaki jest tego sens?)
Point operator+(Point& pA); //dodawanie 2 punktów (jaki jest tego sens?)
Point operator-(Point& pA); //dodawanie 2 punktów (jaki jest tego sens?)
//jak to sie ma do przesun(...) ?
friend std::ostream& operator<< (std::ostream& stream, const Point& pP) {
return stream << '<' << pP.x << ',' << pP.y << '>';
} //powiedzmy operatorowi << jak obsługiwać się z obiektem typu Point
//tutaj deklaracja występuje wraz z definicją, ale jak widać wyżej, tak być nie musi
};
double Point::retX(void) {
return this->x;
}
double Point::retY(void) {
return this->y;
}
Point::Point(const double& _x, const double & _y, string opis){
this->x = _x;
this->y = _y;
this->opis = "";
}
//Point::Point(const double& _x, const double& _y){
// this->x = _x;
// this->y = _y;
//}
Point::Point(void){
this->x = 0;
this->y = 0;
}
void Point::showCoords(){
cout << x << y << endl;
}
void Point::ustaw_opis(string _opis){
this->opis = _opis;
}
void Point::obroc(double kat) {
this->x = this->x * cos(kat) - this->y * sin(kat);
this->y = this->x * sin(kat) + this->y * cos(kat);
}
void Point::obroc(double kat, Point& _p2) {
// 1. przesun o -odpowiedni wektor
Point w = (*this-_p2);
// 2. obroc przesuniety punkt o "kat"
w.obroc(M_PI);
// 3. przesun o odpowiedni wektor
*this = w+_p2;
}
Point Point::operator*(double scalar){
return Point(this->x * scalar, this->y * scalar);
}
Point Point::operator+(Point& pA){
return Point(this->x + pA.x, this->y + pA.y);
}
Point Point::operator-(Point& pA){
return Point(this->x - pA.x, this->y - pA.y);
}
void Point::przesun(Point wektor) {
//return(Point(*this + wektor));
}
void Point::przesun(double wektor[2]) {
//return(Point(*this + Point(wektor[0], wektor[1])));
this->x += wektor[0];
this->y += wektor[1];
}
int main(int argc, char **argv){
Point p1(0.,1.);
Point p2(0.,0.);
p1.obroc(M_PI, p2);
cout << p1;
return 0;
}
|
SDL – szkielet programu
SDL to biblioteka powalające na łatwe programowanie multimediów, w tym grafiki.
Przede wszystkim: jak kompilowac?
g++ zrodlo.cpp -lSDL -lpthread -lSDL_gfx -I/usr/include/SDL
Kod źródłowy minimalnego przykładu:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | #ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL/SDL.h>
int main ( int argc, char** argv )
{
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
SDL_Surface* bmp = SDL_LoadBMP("JAKAS_BITMAPA.bmp");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
// centre the bitmap on screen
SDL_Rect dstrect;
dstrect.x = (screen->w - bmp->w) / 2;
dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
|
Inny przykład z tej strony
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include "SDL/SDL.h"
#include "SDL/SDL_gfxPrimitives.h"
const int WINDOW_WIDTH = 640;
const int WINDOW_HEIGHT = 480;
const char* WINDOW_TITLE = "SDL Start";
int main(int argc, char **argv)
{
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
SDL_HWSURFACE | SDL_DOUBLEBUF );
SDL_WM_SetCaption( WINDOW_TITLE, 0 );
SDL_Event event;
bool gameRunning = true;
while (gameRunning)
{
if (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
{
gameRunning = false;
}
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
pixelRGBA(screen,
10, 15,
255, 255, 255, 255);
lineRGBA(screen,
20, 10,
70, 90,
255, 0, 0, 255);
trigonRGBA(screen,
500, 50,
550, 200,
600, 150,
0, 255, 255, 255);
filledTrigonRGBA(screen,
200, 200,
300, 50,
400, 200,
0, 0, 255, 255);
rectangleRGBA(screen,
-10, 300,
100, 380,
0, 255, 0, 255);
boxRGBA(screen,
210, 76,
325, 300,
255, 0, 0, 150);
ellipseRGBA(screen,
600, 400,
50, 90,
255, 255, 0, 200);
filledEllipseRGBA(screen,
600, 400,
25, 150,
0, 255, 0, 255);
short x[6] = { 350, 275, 300, 325, 350, 400 };
short y[6] = { 325, 325, 390, 390, 375, 300 };
polygonRGBA(screen,
x, y,
6,
255, 255, 255, 155);
short s[5] = { 400, 450, 450, 425, 300 };
short t[5] = { 400, 410, 450, 425, 500};
filledPolygonRGBA(screen,
s, t,
5,
255, 0, 255, 155);
SDL_Flip(screen);
}
SDL_Quit();
return 0;
}
|
Przykład 06/04/2016 – rozwinięty
Rozwinięty przykład z ćwiczeń z dnia 06/04/2016:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | #include <cstdlib>
#include <SDL.h>
#include "SDL_gfxPrimitives.h"
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Point
{
protected: //zmienne prywatne klasy, niedostępne "z zewnątrz"
double x;
double y;
string opis;
public: //metody publiczne, dostępne "dla każdego"
//wariant 1 poczatek
//Point(const double& _x, const double& _y); //konstruktor 1
//wariant 1 koniec
// UWAGA, NALEŻY WYBRAĆ MIĘDZY 1 A 2!!!
//wariant 2 poczatek
Point(const double& _x, const double& _y) : x(_x), y(_y) {};
//wariant 2 koniec
//ten wariant to tzw. lista inicjalizacyjna konstruktora
Point(const double& _x, const double& _y, string opis); //konstruktor 2
Point(void); //konstruktor 3
double retX();
double retY();
void showCoords(); //funkcja zwracająca współrzędne punktu
void przesun(Point wektor); //przesuwanie punktu o wektor (typu Point)
void przesun(double wektor[2]); // przesuwanie punktu o wektor (jawny)
void obroc(double kat); //obrót o "kat"
void obroc(double kat, Point& srodek); //obrót o "kat" wokol innego punktu
void ustaw_opis(string opis); //ustawienie opisu punktu, niepotrzebne, ale ciekawe
Point operator*(double scalar); //dodawanie 2 punktów (jaki jest tego sens?)
Point operator+(Point& pA); //dodawanie 2 punktów (jaki jest tego sens?)
Point operator-(Point& pA); //dodawanie 2 punktów (jaki jest tego sens?)
//jak to sie ma do przesun(...) ?
friend std::ostream& operator<< (std::ostream& stream, const Point& pP) {
return stream << '<' << pP.x << ',' << pP.y << '>';
} //powiedzmy operatorowi << jak obsługiwać się z obiektem typu Point
//tutaj deklaracja występuje wraz z definicją, ale jak widać wyżej, tak być nie musi
};
class cialoNiebieskie : public Point {
public:
double rozmiar;
cialoNiebieskie(double x, double y, double _rozmiar) : Point(x, y), rozmiar(_rozmiar) {};
void rysuj(SDL_Surface* _screen);
};
void cialoNiebieskie::rysuj(SDL_Surface* _screen) {
filledEllipseRGBA(_screen,
this->x, this->y,
this->rozmiar, this->rozmiar,
0, 0, 255, 255);
}
double Point::retX(void) {
return this->x;
}
double Point::retY(void) {
return this->y;
}
Point::Point(const double& _x, const double & _y, string opis){
this->x = _x;
this->y = _y;
this->opis = "";
}
//Point::Point(const double& _x, const double& _y){
// this->x = _x;
// this->y = _y;
//}
Point::Point(void){
this->x = 0;
this->y = 0;
}
void Point::showCoords(){
cout << x << y << endl;
}
void Point::ustaw_opis(string _opis){
this->opis = _opis;
}
void Point::obroc(double kat) {
double tempx, tempy;
tempx = this->x;
tempy = this->y;
this->x = tempx * cos(kat) - tempy * sin(kat);
this->y = tempx * sin(kat) + tempy * cos(kat);
}
void Point::obroc(double kat, Point& _p2) {
// 1. przesun o -odpowiedni wektor
Point w = (*this-_p2);
// 2. obroc przesuniety punkt o "kat"
w.obroc(kat);
// 3. przesun o odpowiedni wektor
*this = w+_p2;
}
Point Point::operator*(double scalar){
return Point(this->x * scalar, this->y * scalar);
}
Point Point::operator+(Point& pA){
return Point(this->x + pA.x, this->y + pA.y);
}
Point Point::operator-(Point& pA){
return Point(this->x - pA.x, this->y - pA.y);
}
void Point::przesun(Point wektor) {
//return(Point(*this + wektor));
}
void Point::przesun(double wektor[2]) {
//return(Point(*this + Point(wektor[0], wektor[1])));
this->x += wektor[0];
this->y += wektor[1];
}
void drawLine(SDL_Surface* _screen, Point p1, Point p2);
vector< std::pair<double, double> > moonPoints;
int main ( int argc, char** argv )
{
//Point ziemia(400,300);
cialoNiebieskie ziemia(360, 0, 15);
Point srodek(320,240);
cialoNiebieskie ksiezyc(460, 0 ,10);
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(720, 680, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
//SDL_Surface* bmp = SDL_LoadBMP("cb.bmp");
//if (!bmp)
//{
// printf("Unable to load bitmap: %s\n", SDL_GetError());
// return 1;
//}
//// centre the bitmap on screen
//SDL_Rect dstrect;
//dstrect.x = (screen->w - bmp->w) / 2;
//dstrect.y = (screen->h - bmp->h) / 2;
// program main loop
bool done = false;
int iter = 0;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
//if(iter % 1000000 == 0){
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
//}
// draw bitmap
//SDL_BlitSurface(bmp, 0, screen, &dstrect);
ziemia.rysuj(screen);
ksiezyc.rysuj(screen);
double stara_ziemiaX = ziemia.retX();
double stara_ziemiaY = ziemia.retY();
pair<double, double> coordsMoon(ksiezyc.retX(), ksiezyc.retY());
moonPoints.push_back(coordsMoon);
//filledEllipseRGBA(screen,
// 320, 240,
// 2, 2,
// 255, 255, 0, 255);
//filledEllipseRGBA(screen,
// ziemia.retX(), ziemia.retY(),
// 10, 10,
// 0, 0, 255, 255);
//filledEllipseRGBA(screen,
// ksiezyc.retX(), ksiezyc.retY(),
// 5, 5,
// 0, 255, 255, 255);
//drawLine(screen, ziemia, ksiezyc);
ziemia.obroc(0.025, srodek);
ksiezyc.obroc(0.25, ziemia);
double nowa_ziemiaX = ziemia.retX();
double nowa_ziemiaY = ziemia.retY();
double podroz_ziemi[2] = {nowa_ziemiaX-stara_ziemiaX, nowa_ziemiaY-stara_ziemiaY};
ksiezyc.przesun(podroz_ziemi);
vector<pair<double, double> >::iterator i;
for(i = moonPoints.begin(); i != moonPoints.end(); i++) {
cout << (*i).first << endl;
pixelRGBA(screen, (*i).first, (*i).second, 255,255,255,255);
}
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
//SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
void drawLine(SDL_Surface* _screen, Point p1, Point p2) {
lineRGBA(_screen, p1.retX(), p1.retY(), p2.retX(), p2.retY(), 255, 0, 255, 255);
}
|
Program z 13/04/2016 – krzywe Lissajous
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 | #ifdef __cplusplus
#include <cstdlib>
#else
#include <stdlib.h>
#endif
#include <SDL.h>
#include "SDL_gfxPrimitives.h"
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Point
{
protected: //zmienne prywatne klasy, niedostępne "z zewnątrz"
double x;
double y;
string opis;
public: //metody publiczne, dostępne "dla każdego"
//wariant 1 poczatek
//Point(const double& _x, const double& _y); //konstruktor 1
//wariant 1 koniec
// UWAGA, NALEŻY WYBRAĆ MIĘDZY 1 A 2!!!
//wariant 2 poczatek
Point(const double& _x, const double& _y) : x(_x), y(_y) {};
//wariant 2 koniec
//ten wariant to tzw. lista inicjalizacyjna konstruktora
Point(const double& _x, const double& _y, string opis); //konstruktor 2
Point(void); //konstruktor 3
double retX();
double retY();
void showCoords(); //funkcja zwracająca współrzędne punktu
void przesun(Point wektor); //przesuwanie punktu o wektor (typu Point)
void przesun(double wektor[2]); // przesuwanie punktu o wektor (jawny)
void obroc(double kat); //obrót o "kat"
void obroc(double kat, Point& srodek); //obrót o "kat" wokol innego punktu
void ustaw_opis(string opis); //ustawienie opisu punktu, niepotrzebne, ale ciekawe
Point operator*(double scalar); //dodawanie 2 punktów (jaki jest tego sens?)
Point operator+(Point& pA); //dodawanie 2 punktów (jaki jest tego sens?)
Point operator-(Point& pA); //dodawanie 2 punktów (jaki jest tego sens?)
//jak to sie ma do przesun(...) ?
void rysuj(SDL_Surface* _screen);
friend std::ostream& operator<< (std::ostream& stream, const Point& pP) {
return stream << '<' << pP.x << ',' << pP.y << '>';
} //powiedzmy operatorowi << jak obsługiwać się z obiektem typu Point
//tutaj deklaracja występuje wraz z definicją, ale jak widać wyżej, tak być nie musi
};
class cialoNiebieskie : public Point {
public:
double rozmiar;
cialoNiebieskie(double x, double y, double _rozmiar) : Point(x, y), rozmiar(_rozmiar) {};
void rysuj(SDL_Surface* _screen);
};
void cialoNiebieskie::rysuj(SDL_Surface* _screen) {
filledEllipseRGBA(_screen,
this->x, this->y,
this->rozmiar, this->rozmiar,
0, 0, 255, 255);
}
void Point::rysuj(SDL_Surface* _screen) {
filledEllipseRGBA(_screen,
this->x+320, this->y+240,
5,5,
0, 0, 255, 255);
}
double Point::retX(void) {
return this->x;
}
double Point::retY(void) {
return this->y;
}
Point::Point(const double& _x, const double & _y, string opis){
this->x = _x;
this->y = _y;
this->opis = "";
}
//Point::Point(const double& _x, const double& _y){
// this->x = _x;
// this->y = _y;
//}
Point::Point(void){
this->x = 0;
this->y = 0;
}
void Point::showCoords(){
cout << x << y << endl;
}
void Point::ustaw_opis(string _opis){
this->opis = _opis;
}
void Point::obroc(double kat) {
double tempx, tempy;
tempx = this->x;
tempy = this->y;
this->x = tempx * cos(kat) - tempy * sin(kat);
this->y = tempx * sin(kat) + tempy * cos(kat);
}
void Point::obroc(double kat, Point& _p2) {
// 1. przesun o -odpowiedni wektor
Point w = (*this-_p2);
// 2. obroc przesuniety punkt o "kat"
w.obroc(kat);
// 3. przesun o odpowiedni wektor
*this = w+_p2;
}
Point Point::operator*(double scalar){
return Point(this->x * scalar, this->y * scalar);
}
Point Point::operator+(Point& pA){
return Point(this->x + pA.x, this->y + pA.y);
}
Point Point::operator-(Point& pA){
return Point(this->x - pA.x, this->y - pA.y);
}
void Point::przesun(Point wektor) {
//return(Point(*this + wektor));
}
void Point::przesun(double wektor[2]) {
//return(Point(*this + Point(wektor[0], wektor[1])));
this->x += wektor[0];
this->y += wektor[1];
}
void drawLine(SDL_Surface* _screen, Point p1, Point p2);
vector< std::pair<double, double> > points;
int main ( int argc, char** argv )
{
double ampl1 = atof(argv[1]);
double ampl2 = atof(argv[2]);
double omega1 = atof(argv[3]);
double omega2 = atof(argv[4]);
double faza = atof(argv[5]);
double t = 0;
//Point ziemia(400,300);
Point srodek(ampl1*cos(omega1*t+faza),ampl2*cos(omega2*t));
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
SDL_Surface* screen = SDL_SetVideoMode(720, 680, 16,
SDL_HWSURFACE|SDL_DOUBLEBUF);
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// program main loop
bool done = false;
int iter = 0;
while (!done)
{
t += 0.01;
Point nowy(ampl1*cos(omega1*t+faza),ampl2*cos(omega2*t));
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
pair<double, double> coords(nowy.retX(), nowy.retY());
points.push_back(coords);
vector<pair<double, double> >::iterator i;
nowy.rysuj(screen);
for(i = points.begin(); i != points.end(); i++) {
if (i == points.begin()) continue; //nie rysuj kreski w pierwszej iteracji!
//cout << (*i).first << endl;
pixelRGBA(screen, (*i).first+320, (*i).second+240, 255,255,255,255);
lineRGBA(screen, (*(i-1)).first+320, (*(i-1)).second+240, (*i).first+320, (*i).second+240, 255, 255 ,255 ,255);
}
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
//SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
void drawLine(SDL_Surface* _screen, Point p1, Point p2) {
lineRGBA(_screen, p1.retX(), p1.retY(), p2.retX(), p2.retY(), 255, 0, 255, 255);
}
|