Advertisement
Guest User

Untitled

a guest
Oct 25th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.99 KB | None | 0 0
  1. //Fedor Zsombor
  2. //OFAPYD
  3. //2014.10.23.
  4. //A forráskódhoz segítségül az InfoC SDL grafika oldalon lévő rajzolós programot használtam.
  5. /////////////////////////////////////////////////////////////////////////////////////////////
  6. #include <SDL/SDL.h>
  7. //#include <SDL_image/SDL_image.h>
  8. #include <SDL_gfx/SDL_gfxPrimitives.h>
  9. #include <stdio.h>
  10. #include <math.h>
  11.  
  12. typedef struct
  13. {
  14.     int x;
  15.     int y;
  16. }Pont;
  17.  
  18. double Pont_tav(Pont p1, Pont p2){
  19.     return  sqrt((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
  20. }
  21.  
  22. void ellipszis(Pont p1, Pont p2, int tav, SDL_Surface *screen)
  23. {
  24.     int i, j;
  25.     int eps=4;
  26.    
  27.     for (i=1; i<=800; i++)
  28.     {
  29.         for (j=1; j<=600; j++)
  30.         {
  31.             Pont pp = {i,j};
  32.             int a=Pont_tav(pp, p1) + Pont_tav(pp, p2);
  33.             if (a < tav+eps && a > tav-eps)
  34.             {
  35.                 pixelRGBA(screen, i,j, 70,130,180,255);
  36.             }
  37.         }
  38.     }
  39. }
  40.  
  41. void Pont_rajzolas(Pont p1, SDL_Surface *screen)
  42. {
  43.     filledCircleRGBA(screen, p1.x, p1.y, 10, 0, 255, 0, 255);
  44. }
  45.  
  46. void szoveg(Pont p1, Pont p2, int tav, SDL_Surface *screen)
  47. {
  48.     char tav_s[10];
  49.     char p1x_s[10];
  50.     char p1y_s[10];
  51.     char p2x_s[10];
  52.     char p2y_s[10];
  53.     sprintf(tav_s, "%d", tav);
  54.     sprintf(p1x_s, "%d", p1.x);
  55.     sprintf(p1y_s, "%d", p1.y);
  56.     sprintf(p2x_s, "%d", p2.x);
  57.     sprintf(p2y_s, "%d", p2.y);
  58.    
  59.     stringRGBA(screen, 770, 0,  tav_s,       0 , 0, 0, 255);
  60.     stringRGBA(screen, 740, 10, p1x_s,       0, 0, 0, 255);
  61.     stringRGBA(screen, 770, 10, p1y_s,       0, 0, 0, 255);
  62.     stringRGBA(screen, 740, 20, p2x_s,       0, 0, 0, 255);
  63.     stringRGBA(screen, 770, 20, p2y_s,       0, 0, 0, 255);
  64.     stringRGBA(screen, 702, 0,  "Tav:",      0, 0, 0, 255);
  65.     stringRGBA(screen, 670, 10, "1. Pont:",  0, 0, 0, 255);
  66.     stringRGBA(screen, 670, 20, "2. Pont:",  0, 0, 0, 255);
  67.     stringRGBA(screen, 692, 30, "Fedor Zsombor", 0, 0, 0, 255);
  68. }
  69.  
  70. int main(int argc, char *argv[])
  71. {
  72.     SDL_Event event;
  73.     SDL_Surface *screen;
  74.     int tav=600;
  75.     Pont p1, p2;
  76.     p1.x=200;
  77.     p1.y=300;
  78.     p2.x=600;
  79.     p2.y=300;
  80.     int click, quit;
  81.    
  82.     // SDL inicializálása és ablak megnyitása
  83.     SDL_Init(SDL_INIT_VIDEO);
  84.     screen=SDL_SetVideoMode(800, 600, 0, SDL_ANYFORMAT);
  85.     if (!screen) {
  86.         fprintf(stderr, "Nem sikerult megnyitni az ablakot!\n");
  87.         exit(1);
  88.     }
  89.     SDL_WM_SetCaption("Ellipszis", "Ellipszis");
  90.    
  91.     SDL_FillRect(screen, NULL, 0xDCDCDCFF);
  92.    
  93.     //A fókuszpontok kirajzolása
  94.     filledCircleRGBA(screen, p1.x, p1.y, 10, 0,206,209,255);
  95.     filledCircleRGBA(screen, p2.x, p2.y, 10, 64,224,208,255);
  96.    
  97.     //Az ellipszis kirajzolása
  98.     ellipszis(p1, p2, tav, screen);
  99.    
  100.     //Szöveg kiirítása
  101.     szoveg(p1, p2, tav, screen);
  102.    
  103.     //Az eddigi dolgok kirajzolása
  104.     SDL_Flip(screen);
  105.    
  106.     quit = 0;
  107.     click = 0;
  108.     while (!quit) {
  109.         SDL_WaitEvent(&event);
  110.         switch (event.type) {
  111.                 //Egér kattintás
  112.             case SDL_MOUSEBUTTONDOWN:
  113.                 if (event.button.button == SDL_BUTTON_LEFT)
  114.                 {
  115.                     click = 1;
  116.                 }
  117.                 break;
  118.                 //Egérgomb elengedése
  119.             case SDL_MOUSEBUTTONUP:
  120.                 if (event.button.button == SDL_BUTTON_LEFT)
  121.                 {
  122.                     click = 0;
  123.                 }
  124.                 break;
  125.                 //Egér mozdulat
  126.             case SDL_MOUSEMOTION:
  127.                 if(click)
  128.                 {
  129.                     Pont p_eger={event.button.x, event.button.y};
  130.                    
  131.                     if (event.button.x<p1.x+10 && event.button.x>p1.x-10)
  132.                     {
  133.                         if (event.button.y<p1.y+10 && event.button.y>p1.y-10)
  134.                         {
  135.                             p1.x=event.button.x;
  136.                             p1.y=event.button.y;
  137.                            
  138.                             SDL_FillRect(screen, NULL, 0xDCDCDCFF);
  139.                             filledCircleRGBA(screen, p1.x, p1.y, 10, 0,206,209,255);
  140.                             filledCircleRGBA(screen, p2.x, p2.y, 10, 64,224,208,255);
  141.                             ellipszis(p1, p2, tav, screen);
  142.                             szoveg(p1, p2, tav, screen);
  143.                             SDL_Flip(screen);
  144.                         }
  145.                     } else if (event.button.x<p2.x+10 && event.button.x>p2.x-10)
  146.                     {
  147.                         if (event.button.y<p2.y+10 && event.button.y>p2.y-10)
  148.                         {
  149.                             p2.x=event.button.x;
  150.                             p2.y=event.button.y;
  151.                            
  152.                             SDL_FillRect(screen, NULL, 0xDCDCDCFF);
  153.                             filledCircleRGBA(screen, p1.x, p1.y, 10, 0,206,209,255);
  154.                             filledCircleRGBA(screen, p2.x, p2.y, 10, 64,224,208,255);
  155.                             ellipszis(p1, p2, tav, screen);
  156.                             szoveg(p1, p2, tav, screen);
  157.                            
  158.                             SDL_Flip(screen);
  159.                         }
  160.                     } else if(Pont_tav(p_eger, p1)+Pont_tav(p_eger, p2)<tav+10 && Pont_tav(p_eger, p1)+Pont_tav(p_eger, p2)>tav-10)
  161.                     {
  162.                         tav = Pont_tav(p_eger, p1)+Pont_tav(p_eger, p2);
  163.                         SDL_FillRect(screen, NULL, 0xDCDCDCFF);
  164.                         filledCircleRGBA(screen, p1.x, p1.y, 10, 0,206,209,255);
  165.                         filledCircleRGBA(screen, p2.x, p2.y, 10, 64,224,208,255);
  166.                         ellipszis(p1, p2, tav, screen);
  167.                         szoveg(p1, p2, tav, screen);
  168.                        
  169.                         SDL_Flip(screen);
  170.                     }
  171.                 }
  172.                 break;
  173.                 //Bezárás
  174.             case SDL_QUIT:
  175.                 quit=1;
  176.                 break;
  177.         }
  178.     }
  179.     return 0;
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement