Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifdef __cplusplus
- #include <cstdlib>
- #else
- #include <stdlib.h>
- #endif
- #include <SDL/SDL.h>
- #include <math.h>
- #define pi 3.1415926536
- #include <time.h>
- #include <iostream>
- using namespace std;
- SDL_Surface *screen;
- int width = 900;
- int height = 600;
- char const* tytul = "GKiM2019 - Lab 6 - Liszka Bartosz";
- struct linia
- {
- float xp;
- float yp;
- float xk;
- float yk;
- };
- linia wzorzec[] = { {100, 100, 100, 120},
- {100, 120, 110, 120},
- {110, 120, 110, 160},
- {110, 160, 130, 160},
- {130, 160, 130, 120},
- {130, 120, 140, 120},
- {140, 120, 120, 100},
- {120, 100, 100, 100} };
- linia mlotek[8];
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B);
- SDL_Color getPixel (int x, int y);
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B);
- void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B);
- void Okrag(int x, int y, int r, Uint8 R, Uint8 G, Uint8 B);
- void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B);
- void inicjalizacja()
- {
- for(int i=0; i<8; i++)
- {
- mlotek[i] = wzorzec[i];
- }
- }
- void rysowanie(Uint8 R, Uint8 G, Uint8 B)
- {
- for(int i=0; i<8; i++)
- {
- Linia(mlotek[i].xp, mlotek[i].yp,
- mlotek[i].xk, mlotek[i].yk, R, G, B);
- }
- }
- void obrotL() {
- float x1[8], x2[8], y1[8], y2[8], srX = 0, srY = 0;
- // Srodek ciezkosci
- for(int i = 0; i < 8; i++)
- {
- srX += mlotek[i].xp;
- srY += mlotek[i].yp;
- }
- srX /= 8;
- srY /= 8;
- for(int i = 0; i < 8; i++)
- {
- mlotek[i].xp -= srX;
- mlotek[i].yp -= srY;
- mlotek[i].xk -= srX;
- mlotek[i].yk -= srY;
- }
- for(int i = 0; i < 8; i++)
- {
- x1[i] = round(mlotek[i].xp * sqrt(3) * 0.5 + mlotek[i].yp * 0.5);
- y1[i] = round(mlotek[i].yp * sqrt(3) * 0.5 - mlotek[i].xp * 0.5);
- x2[i] = round(mlotek[i].xk * sqrt(3) * 0.5 + mlotek[i].yk * 0.5);
- y2[i] = round(mlotek[i].yk * sqrt(3) * 0.5 - mlotek[i].xk * 0.5);
- }
- for(int i = 0; i < 8; i++)
- {
- mlotek[i].xp = x1[i]+srX;
- mlotek[i].yp = y1[i]+srY;
- mlotek[i].xk = x2[i]+srX;
- mlotek[i].yk = y2[i]+srY;
- }
- }
- void obrotP() {
- float x1[8], x2[8], y1[8], y2[8], srX = 0, srY = 0;
- // Srodek ciezkosci
- for(int i = 0; i < 8; i++)
- {
- srX += mlotek[i].xp;
- srY += mlotek[i].yp;
- }
- srX /= 8;
- srY /= 8;
- for(int i = 0; i < 8; i++)
- {
- mlotek[i].xp -= srX;
- mlotek[i].yp -= srY;
- mlotek[i].xk -= srX;
- mlotek[i].yk -= srY;
- }
- for(int i = 0; i < 8; i++)
- {
- x1[i] = round(mlotek[i].xp * sqrt(3) * 0.5 - mlotek[i].yp * 0.5);
- y1[i] = round(mlotek[i].yp * sqrt(3) * 0.5 + mlotek[i].xp * 0.5);
- x2[i] = round(mlotek[i].xk * sqrt(3) * 0.5 - mlotek[i].yk * 0.5);
- y2[i] = round(mlotek[i].yk * sqrt(3) * 0.5 + mlotek[i].xk * 0.5);
- }
- for(int i = 0; i < 8; i++)
- {
- mlotek[i].xp = x1[i]+srX;
- mlotek[i].yp = y1[i]+srY;
- mlotek[i].xk = x2[i]+srX;
- mlotek[i].yk = y2[i]+srY;
- }
- }
- void Funkcja1();
- void Funkcja2();
- void Funkcja3();
- void Funkcja4();
- void Funkcja5();
- void Funkcja6();
- void Funkcja7();
- void Funkcja8();
- void Funkcja9();
- void algorytm1(float x, float y){
- float a=x/y;
- int xx, yy;
- if(a>=1){
- for(xx=0; xx<=x; xx++){
- yy=round(xx/a);
- setPixel(xx,yy,255,255,255);
- }
- }
- else{
- for(yy=0; yy<=y; yy++){
- xx=round(yy*a);
- setPixel(xx,yy,255,0,0);
- }
- }
- cout<<a;
- }
- void algorytm3(float x1, float y1,float x2, float y2){
- if(x1<=x2 && y1<=y2 ){
- float a=(x2-x1)/(y2-y1);
- int xx, yy;
- if(a>=1){
- for(xx=x1; xx<=x2; xx++){
- yy=round(xx/a);
- setPixel(xx,yy,255,255,255);
- }
- }
- else{
- for(yy=y1; yy<=y2; yy++){
- xx=round(yy*a);
- setPixel(xx,yy,255,0,0);
- }
- }
- cout<<a;
- }
- else if(x1>x2 && y1<y2){
- SDL_Flip(screen);
- }
- }
- }
- else if(x1<x2 && y1>y2){
- float a=(x2-x1)/(y1-y2);
- int xx, yy;
- if(a>=1){
- int yyy=y1;
- for(xx=x1; xx<=x2; xx++){
- yy=round(xx/a);
- setPixel(xx,yy,0,255,0);
- yyy--;
- SDL_Flip(screen);
- }
- }
- else{
- int yyy=y1;
- for(yy=y2; yy<=y1; yy++){
- xx=round(yy*a);
- setPixel(xx,yyy,0,0,255);
- yyy--;
- SDL_Flip(screen);
- }
- }
- }
- else{
- float a=(x1-x2)/(y1-y2);
- int xx, yy;
- if(a>=1){
- for(xx=x1; xx>=x2; xx--){
- yy=round(xx/a);
- setPixel(xx,yy,0,255,255);
- SDL_Flip(screen);
- }
- }
- else{
- for(yy=y1; yy>=y2; yy--){
- xx=round(yy*a);
- setPixel(xx,yy,200,0,0);
- SDL_Flip(screen);
- }
- }
- }
- SDL_Flip(screen);
- }
- void algorytm2(float x, float y){
- float a=x/y;
- int xx,yy;
- if(a>=1){
- for(xx=0;xx<=x;xx++){
- yy=round(xx/a + 1/a);
- setPixel(xx,yy,255,255,255);
- }
- }
- else{
- for(yy=0;yy<=y;yy++){
- xx=round(yy*a + 1);
- setPixel(xx,yy,255,0,0);
- }
- }
- }
- void algorytm2(float x1, float y1,float x2, float y2){
- if(x1<x2 && y1<y2){
- float a=(x2-x1)/(y2-y1);
- int xx,yy;
- if(a>=1){
- for(xx=0;xx<=x;xx++){
- yy=round(xx/a + 1/a);
- setPixel(xx,yy,255,255,255);
- }
- }
- else{
- for(yy=0;yy<=y;yy++){
- xx=round(yy*a + 1);
- setPixel(xx,yy,255,0,0);
- }
- }
- }
- else if(x1>x2 && y1<y2)
- float a=(x1-x2)/(y2-y1);
- int xx,yy;
- if(a>=1){
- for(xx=0;xx<=x;xx++){
- yy=round(xx/a + 1/a);
- setPixel(xx,yy,255,255,255);
- }
- }
- else{
- for(yy=0;yy<=y;yy++){
- xx=round(yy*a + 1);
- setPixel(xx,yy,255,0,0);
- }
- }
- }
- }
- void Funkcja1() {
- algorytm1(rand()%900, rand()%600);
- SDL_Flip(screen);
- }
- void Funkcja2() {
- algorytm2(rand()%900, rand()%600);
- SDL_Flip(screen);
- }
- void Funkcja3() {
- algorytm3(rand()%900, rand()%600,rand()%900, rand()%600);
- SDL_Flip(screen);
- }
- void Funkcja4() {
- int yy=height/20;
- int xx;
- for(int xx=0;xx<=width;xx+=width/20){
- Linia(xx,0,width,yy,255,255,255);
- yy+=height/20;
- }
- xx=width-width/20;
- for(yy=0;yy<=height;yy+=height/20){
- Linia(width,yy,xx,height,255,0,0);
- xx-=width/20;
- }
- yy=height-height/20;
- for(xx=width;xx>=0;xx-=width/20){
- Linia(xx,height,0,yy,255,255,0);
- yy-=height/20;
- }
- xx=width/20;
- for(yy=height;yy>=0;yy-=height/20){
- Linia(0,yy,xx,0,0,0,255);
- xx+=width/20;
- }
- SDL_Flip(screen);
- }
- void Funkcja5() {
- inicjalizacja();
- rysowanie(255,255,255);
- SDL_Flip(screen);
- }
- void Funkcja6() {
- rysowanie(0,0,0);
- obrotL();
- rysowanie(255,255,255);
- SDL_Flip(screen);
- }
- void Funkcja7() {
- rysowanie(0,0,0);
- obrotP();
- rysowanie(255,255,255);
- SDL_Flip(screen);
- }
- void Funkcja8() {
- //...
- SDL_Flip(screen);
- }
- void Funkcja9() {
- //...
- SDL_Flip(screen);
- }
- void Linia(int x1, int y1, int x2, int y2, Uint8 R, Uint8 G, Uint8 B) {
- setPixel(x1,y1,R,G,B);
- setPixel(x2,y2,R,G,B);
- // zmienne pomocnicze
- int d, dx, dy, ai, bi, xi, yi;
- int x = x1, y = y1;
- // ustalenie kierunku rysowania
- if (x1 < x2)
- {
- xi = 1;
- dx = x2 - x1;
- }
- else
- {
- xi = -1;
- dx = x1 - x2;
- }
- // ustalenie kierunku rysowania
- if (y1 < y2)
- {
- yi = 1;
- dy = y2 - y1;
- }
- else
- {
- yi = -1;
- dy = y1 - y2;
- }
- // pierwszy piksel
- setPixel(x, y, R, G, B);
- // oś wiodąca OX
- if (dx > dy)
- {
- ai = (dy - dx) * 2;
- bi = dy * 2;
- d = bi - dx;
- // pętla po kolejnych x
- while (x != x2)
- {
- // test współczynnika
- if (d >= 0)
- {
- x += xi;
- y += yi;
- d += ai;
- }
- else
- {
- d += bi;
- x += xi;
- }
- setPixel(x, y, R, G, B);
- }
- }
- // oś wiodąca OY
- else
- {
- ai = ( dx - dy ) * 2;
- bi = dx * 2;
- d = bi - dy;
- // pętla po kolejnych y
- while (y != y2)
- {
- // test współczynnika
- if (d >= 0)
- {
- x += xi;
- y += yi;
- d += ai;
- }
- else
- {
- d += bi;
- y += yi;
- }
- setPixel(x, y, R, G, B);
- }
- }
- SDL_Flip(screen);
- }
- void RysujOkrag(int x0, int y0, int x, int y, Uint8 R, Uint8 G, Uint8 B){
- setPixel(x+x0, y+y0, R, G, B);
- setPixel(y+x0, x+y0, R, G, B);
- setPixel(y+x0, -x+y0, R, G, B);
- setPixel(x+x0, -y+y0, R, G, B);
- setPixel(-x+x0, -y+y0, R, G, B);
- setPixel(-y+x0, -x+y0, R, G, B);
- setPixel(-y+x0, x+y0, R, G, B);
- setPixel(-x+x0, y+y0, R, G, B);;
- }
- void Okrag(int x0, int y0, int r, Uint8 R, Uint8 G, Uint8 B) {
- int x =0;
- int y=r;
- int d = 3-2*r;
- while (x <= y){
- if (d<0){
- d=d+4*x+6;
- RysujOkrag(x0, y0, x, y, R, G, B);
- } else{
- d=d+4*(x-y)+10;
- y--;
- RysujOkrag(x0, y0, x, y, R, G, B);
- }
- x++;
- }
- SDL_Flip(screen);
- }
- void Elipsa(int x, int y, int a, int b, Uint8 R, Uint8 G, Uint8 B) {
- int xc = 0,
- yc = b;
- int aa = a * a,
- aa2 = aa + aa,
- bb = b * b,
- bb2 = bb + bb;
- int d = bb - aa * b + (aa / 4),
- dx = 0,
- dy = aa2 * b;
- while (dx < dy) {
- setPixel (x - xc,y - yc, R, G, B);
- setPixel (x - xc,y + yc, R, G, B);
- setPixel (x + xc,y - yc, R, G, B);
- setPixel (x + xc,y + yc, R, G, B);
- if (d > 0){
- yc = yc-1;
- dy -= aa2;
- d -= dy;
- }
- xc =xc+1;
- dx += bb2;
- d += bb + dx;
- }
- d += (3 * ((aa - bb) / 2) - (dx + dy) / 2);
- while (yc >= 0) {
- setPixel (x - xc,y - yc, R, G, B);
- setPixel (x - xc,y + yc, R, G, B);
- setPixel (x + xc,y - yc, R, G, B);
- setPixel (x + xc,y + yc, R, G, B);
- if (d < 0) {
- xc =xc+1;
- dx += bb2;
- d += (bb + dx);
- }
- yc = yc-1;
- dy -= aa2;
- d += aa - dy;
- }
- SDL_Flip(screen);
- }
- void setPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
- {
- if ((x>=0) && (x<width) && (y>=0) && (y<height))
- {
- /* Zamieniamy poszczególne składowe koloru na format koloru piksela */
- Uint32 pixel = SDL_MapRGB(screen->format, R, G, B);
- /* Pobieramy informację ile bajtów zajmuje jeden piksel */
- int bpp = screen->format->BytesPerPixel;
- /* Obliczamy adres piksela */
- Uint8 *p = (Uint8 *)screen->pixels + y * screen->pitch + x * bpp;
- /* Ustawiamy wartość piksela, w zależności od formatu powierzchni*/
- switch(bpp)
- {
- case 1: //8-bit
- *p = pixel;
- break;
- case 2: //16-bit
- *(Uint16 *)p = pixel;
- break;
- case 3: //24-bit
- if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {
- p[0] = (pixel >> 16) & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = pixel & 0xff;
- } else {
- p[0] = pixel & 0xff;
- p[1] = (pixel >> 8) & 0xff;
- p[2] = (pixel >> 16) & 0xff;
- }
- break;
- case 4: //32-bit
- *(Uint32 *)p = pixel;
- break;
- }
- /* ewentualna aktualizacja obrazu (aka double buffering) */
- }
- }
- SDL_Color getPixel (int x, int y) {
- SDL_Color color ;
- Uint32 col = 0 ;
- if ((x>=0) && (x<width) && (y>=0) && (y<height)) {
- //określamy pozycję
- char* pPosition=(char*)screen->pixels ;
- //przesunięcie względem y
- pPosition+=(screen->pitch*y) ;
- //przesunięcie względem x
- pPosition+=(screen->format->BytesPerPixel*x);
- //kopiujemy dane piksela
- memcpy(&col, pPosition, screen->format->BytesPerPixel);
- //konwertujemy kolor
- SDL_GetRGB(col, screen->format, &color.r, &color.g, &color.b);
- }
- return ( color ) ;
- }
- void czyscEkran(Uint8 R, Uint8 G, Uint8 B)
- {
- SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, R, G, B));
- SDL_Flip(screen);
- }
- int main ( int argc, char** argv )
- {
- // console output
- freopen( "CON", "wt", stdout );
- freopen( "CON", "wt", stderr );
- // 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
- screen = SDL_SetVideoMode(width, height, 32,
- SDL_HWSURFACE|SDL_DOUBLEBUF);
- if ( !screen )
- {
- printf("Unable to set video: %s\n", SDL_GetError());
- return 1;
- }
- SDL_WM_SetCaption( tytul , NULL );
- // 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;
- if (event.key.keysym.sym == SDLK_1)
- Funkcja1();
- if (event.key.keysym.sym == SDLK_2)
- Funkcja2();
- if (event.key.keysym.sym == SDLK_3)
- Funkcja3();
- if (event.key.keysym.sym == SDLK_4)
- Funkcja4();
- if (event.key.keysym.sym == SDLK_5)
- Funkcja5();
- if (event.key.keysym.sym == SDLK_6)
- Funkcja6();
- if (event.key.keysym.sym == SDLK_7)
- Funkcja7();
- if (event.key.keysym.sym == SDLK_b)
- czyscEkran(0, 0, 10); break;
- }
- } // end switch
- } // end of message processing
- } // end main loop
- // all is well ;)
- printf("Exited cleanly\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement