Advertisement
iggnaccy

figura.c

Mar 1st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #include "figura.h"
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. static const float PI = 3.1415926;
  6.  
  7. void przesun(Figura *f, float x, float y)
  8. {
  9. f->xA += x;
  10. f->xB += x;
  11. f->xC += x;
  12. f->xD += x;
  13. f->yA += y;
  14. f->yB += y;
  15. f->yC += y;
  16. f->yD += y;
  17. }
  18.  
  19. float pole(Figura *f)
  20. {
  21. switch(f->typfig)
  22. {
  23. case SQUARE:
  24. {
  25. return fabs((f->xB - f->xA) * (f->yC - f->yA));
  26. }
  27. case TRIANGLE:
  28. {
  29. float a = fabs(f->xA - f->xB);
  30. float h = fabs(f->yC - f->yA);
  31. return a*h/2;
  32. }
  33. case CIRCLE:
  34. {
  35. float x = fabs(f->xA - f->xB);
  36. float y = fabs(f->yA - f->yB);
  37. float rKw = (x*x + y*y);
  38. return PI * rKw;
  39. }
  40. }
  41. }
  42.  
  43. float sumapol(Figura** f, int size)
  44. {
  45. float r = 0.0f;
  46. for(int i = 0; i < size; i++)
  47. {
  48. r += pole(f[i]);
  49. }
  50. return r;
  51. }
  52.  
  53. static Figura* stworzFigure(enum FIG_TYPES t,float xA, float yA, float xB, float yB, float xC, float yC, float xD, float yD)
  54. {
  55. Figura* f = (Figura*)malloc(sizeof(Figura));
  56. f->typfig = t;
  57. f->xA = xA;
  58. f->xB = xB;
  59. f->xC = xC;
  60. f->xD = xD;
  61. f->yA = yA;
  62. f->yB = yB;
  63. f->yC = yC;
  64. f->yD = yD;
  65. return f;
  66. }
  67.  
  68. Figura* stworzKwadrat(float xA, float yA, float bok)
  69. {
  70. return stworzFigure(SQUARE, xA, yA, xA+bok, yA, xA+bok, yA+bok, xA, yA+bok);
  71. }
  72.  
  73. Figura* stworzKolo(float xA, float yA, float r)
  74. {
  75. return stworzFigure(CIRCLE, xA, yA, xA+r, yA, 0, 0, 0, 0);
  76. }
  77.  
  78. Figura* stworzTrojkat(float xA, float yA, float xB, float xC, float yC)
  79. {
  80. if(xB == xA || yC == yA) return NULL;
  81. return stworzFigure(TRIANGLE, xA, yA, xB, yA, xC, yC, 0, 0);
  82. }
  83.  
  84. void zniszczFigure(Figura* f)
  85. {
  86. free(f);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement