Haifisch7734

Przycinanie

Oct 20th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <allegro.h>
  4. #include <time.h>
  5. #include <math.h>
  6.  
  7. #define COL_BLACK 0x000000
  8. #define COL_WHITE 0xFFFFFF
  9. #define COL_RED 0xFF0000
  10. #define COL_GREEN 0x00FF00
  11. #define DIM 400
  12. #define TR_C 15
  13.  
  14. typedef struct{
  15.     int Xmin, Xmax, Ymin, Ymax;
  16. } rectangle_t;
  17.  
  18. typedef struct{
  19.     int x,y;
  20. } point_t;
  21.  
  22. typedef struct{
  23.     point_t pt[3];
  24. } triangle_t;
  25.  
  26. void drawLine(int x0, int y0, int x1, int y1, int col, float m) {
  27.     int x, y;
  28.     float dx = x1 - x0;
  29.     float dy = y1 - y0;
  30.  
  31.     if (abs(dx) >= abs(dy)) {
  32.         y = y0;
  33.         if (dx > 0){
  34.             for (x = x0; x <= x1; x++)
  35.                 putpixel(screen, x, round(m * (x - x0) + y), col);
  36.         }
  37.         else {
  38.             for (x = x0; x >= x1; x--)
  39.                 putpixel(screen, x, round(m * (x - x0) + y), col);
  40.         }
  41.     }
  42.     else {
  43.         x = x0;
  44.         if (dy > 0){
  45.             for (y = y0; y <= y1; y++)
  46.                 putpixel(screen, round((y - y0) / m + x0), y, col);
  47.         }
  48.         else {
  49.             for (y = y0; y >= y1; y--)
  50.                 putpixel(screen, round((y - y0) / m + x0), y, col);
  51.         }
  52.  
  53.     }
  54. }
  55.  
  56. void swapPoints(point_t* p1, point_t* p2){
  57.     point_t tmp = (*p1);
  58.     (*p1) = (*p2);
  59.     (*p2) = tmp;
  60.     return;
  61. }
  62.  
  63. char getXCoord(int x, rectangle_t rec){
  64.     if (x < rec.Xmin)
  65.         return 1;
  66.     if (x >= rec.Xmin && x <= rec.Xmax)
  67.         return 0;
  68.     if (x > rec.Xmax)
  69.         return 2;
  70. }
  71.  
  72. char getYCoord(int y, rectangle_t rec){
  73.     if (y < rec.Ymin)
  74.         return 8;
  75.     if (y >= rec.Ymin && y <= rec.Ymax)
  76.         return 0;
  77.     if (y > rec.Ymax)
  78.         return 4;
  79. }
  80.  
  81. int getX(int y, float a, float b){
  82.     return round((y - b) / a);
  83. }
  84.  
  85. int getY(int x, float a, float b){
  86.     return round(a * x + b);
  87. }
  88.  
  89. void checkLine(point_t pt1, point_t pt2, rectangle_t rec){
  90.     point_t pp = pt1;
  91.     point_t pk = pt2;
  92.  
  93.     float dx = pt2.x - pt1.x;
  94.     float dy = pt2.y - pt1.y;
  95.     float a = dy / dx;
  96.     float b = pt1.y - a * pt1.x;
  97.  
  98.     while(1){
  99.         char cxp = getXCoord(pp.x, rec);
  100.         char cxk = getXCoord(pk.x, rec);
  101.         char cyp = getYCoord(pp.y, rec);
  102.         char cyk = getYCoord(pk.y, rec);
  103.         char cpp = cxp | cyp;
  104.         char cpk = cxk | cyk;
  105.  
  106.         if ((cpp | cpk) == 0){
  107.             drawLine(pp.x, pp.y, pk.x, pk.y, COL_RED, a);
  108.             break;
  109.         }
  110.         if ((cpp & cpk) != 0){
  111.             drawLine(pp.x, pp.y, pk.x, pk.y, COL_GREEN, a);
  112.             break;
  113.         }
  114.         if (cpp == 0){
  115.             swapPoints(&pp, &pk);
  116.             continue;
  117.         }
  118.         if ((cpp & 4) == 4) {
  119.             int xk = getX(rec.Ymax, a, b);
  120.             drawLine(pp.x, pp.y, xk, rec.Ymax, COL_GREEN, a);
  121.             pp.x = xk;
  122.             pp.y = rec.Ymax;
  123.             swapPoints(&pp, &pk);
  124.             continue;
  125.         }
  126.         if ((cpp & 8) == 8) {
  127.             int xk = getX(rec.Ymin, a, b);
  128.             drawLine(pp.x, pp.y, xk, rec.Ymin, COL_GREEN, a);
  129.             pp.x = xk;
  130.             pp.y = rec.Ymin;
  131.             swapPoints(&pp, &pk);
  132.             continue;
  133.         }
  134.         if ((cpp & 2) == 2) {
  135.             int yk = getY(rec.Xmax, a, b);
  136.             drawLine(pp.x, pp.y, rec.Xmax, yk, COL_GREEN, a);
  137.             pp.x = rec.Xmax;
  138.             pp.y = yk;
  139.             swapPoints(&pp, &pk);
  140.             continue;
  141.         }
  142.         if ((cpp & 1) == 1) {
  143.             int yk = getY(rec.Xmin, a, b);
  144.             drawLine(pp.x, pp.y, rec.Xmin, yk, COL_GREEN, a);
  145.             pp.x = rec.Xmin;
  146.             pp.y = yk;
  147.             swapPoints(&pp, &pk);
  148.             continue;
  149.         }
  150.     }
  151. }
  152.  
  153. void drawTriangle(triangle_t trg, rectangle_t rec){
  154.     checkLine(trg.pt[0], trg.pt[1], rec);
  155.     checkLine(trg.pt[1], trg.pt[2], rec);
  156.     checkLine(trg.pt[0], trg.pt[2], rec);
  157. }
  158.  
  159. int main() {
  160.     srand(time(NULL));
  161.     rectangle_t rec;
  162.     rec.Xmin = 50 + rand() % 50;
  163.     rec.Ymin = 50 + rand() % 50;
  164.     rec.Xmax = (DIM - 200) + rand() % 100;
  165.     rec.Ymax = (DIM - 200) + rand() % 100;
  166.     triangle_t trg[TR_C];
  167.     int i,j;
  168.     for (i = 0; i < TR_C; i++){
  169.         for (j = 0; j < 3; j++){
  170.             trg[i].pt[j].x = rand() % DIM;
  171.             trg[i].pt[j].y = rand() % DIM;
  172.         }
  173.     }
  174.  
  175.     allegro_init();
  176.     install_keyboard();
  177.     set_color_depth(32);
  178.     set_gfx_mode(GFX_AUTODETECT_WINDOWED, DIM, DIM, 0, 0);
  179.     set_palette(default_palette);
  180.     clear_to_color(screen, COL_BLACK);
  181.     rect(screen, rec.Xmin, rec.Ymin, rec.Xmax, rec.Ymax, COL_WHITE);
  182.     for (i = 0; i < TR_C; i++)
  183.         drawTriangle(trg[i], rec);
  184.  
  185.     readkey();
  186.     remove_keyboard();
  187.     allegro_exit();
  188.     return 0;
  189. }
  190. END_OF_MAIN();
Advertisement
Add Comment
Please, Sign In to add comment