Advertisement
Tevronis

Transform.cpp

Oct 1st, 2015
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.82 KB | None | 0 0
  1. #pragma once
  2. #include "stdafx.h"
  3. #include "Transform.h"
  4. #include <math.h>
  5.  
  6. mat T;
  7.  
  8. void times(mat a, mat b, mat c)
  9. {
  10.     for (int i = 0; i < M; i++)
  11.     {
  12.         for (int j = 0; j < M; j++)
  13.         {
  14.             float skalaar = 0;
  15.             for (int k = 0; k < M; k++)
  16.                 skalaar += a[i][k] * b[k][j];
  17.             c[i][j] = skalaar;         
  18.         }
  19.     }
  20. }
  21.  
  22. void timesMatVec(mat a, vec b, vec c)
  23. {
  24.     for (int i = 0; i < M; i++)
  25.     {
  26.         float skalaar = 0;
  27.         for (int j = 0; j < M; j++)
  28.             skalaar += a[i][j] * b[j];
  29.         c[i] = skalaar;
  30.     }
  31. }
  32.  
  33. void set(mat a, mat b)
  34. {
  35.     for (int i = 0; i < M; i++)
  36.         for (int j = 0; j < M; j++)
  37.         b[i][j] = a[i][j];
  38. }
  39. void point2vec(point a, vec b)
  40. {
  41.     b[0] = a.x; b[1] = a.y; b[2] = 1;
  42. }
  43.  
  44. void vec2point(vec a, point &b)
  45. {
  46.     b.x = ((float)a[0]) / a[2];
  47.     b.y = ((float)a[1]) / a[2];
  48. }
  49. void makeHomogenVec(float x, float y, vec c)
  50. {
  51.     c[0] = x; c[1] = y; c[2] = 1;
  52. }
  53.  
  54. void unit(mat a)
  55. {
  56.     for (int i = 0; i < M; i++)
  57.     {
  58.         for (int j = 0; j < M; j++)
  59.         {
  60.             if (i == j) a[i][j] = 1;
  61.             else a[i][j] = 0;
  62.         }
  63.     }
  64. }
  65.  
  66. void move(float Tx, float Ty, mat c)
  67. {
  68.     unit(c);
  69.     c[0][M - 1] = Tx;
  70.     c[1][M - 1] = Ty;
  71. }
  72. void scale(float S, mat c)
  73. {
  74.     unit(c);
  75.     c[0][0] = S; c[1][1] = S;
  76. }
  77. void rotate(float phi, mat c)
  78. {
  79.     unit(c);
  80.     c[0][0] = cos(phi); c[0][1] = -sin(phi);
  81.     c[1][0] = sin(phi); c[1][1] = cos(phi);
  82. }
  83. void rotate_2(float w, float h, float phi, mat c)
  84. {
  85.     unit(c);
  86.     c[0][0] = cos(phi); c[0][1] = -sin(phi); c[0][2] = -w/2*(cos(phi)-1) + h/2*sin(phi);
  87.     c[1][0] = sin(phi); c[1][1] = cos(phi) ; c[1][2] = -h/2*(cos(phi)-1) - w/2*sin(phi);
  88.     c[2][0] = 0;        c[2][1] = 0;         c[2][2] = 1;
  89. }
  90. void mirror1(float w, mat c)
  91. {
  92.     unit(c);
  93.     c[0][0] = -1; c[1][1] = 1; c[0][2] = w;
  94. }
  95. void mirror2(float h, mat c)
  96. {
  97.     unit(c);
  98.     c[0][0] = 1; c[1][1] = -1; c[1][2] = h;
  99. }
  100. void scale_2(float S, float n, float m, mat c)
  101. {
  102.     unit(c);
  103.     c[0][0] = S; c[0][1] = 0; c[0][2] = (1 - S)*m;
  104.     c[1][0] = 0; c[1][1] = S; c[1][2] = (1 - S)*n;
  105.     c[2][0] = 0; c[2][1] = 0; c[2][2] = 1;
  106. }
  107. void sdvig_g(float S,float w, mat c)
  108. {
  109.  unit (c);
  110.  c[0][0] = S; c[0][1] =0 ; c[0][2]=(1 - S)*w;
  111.  c[1][0] =0 ; c[1][1] =S ; c[1][2]=1;
  112.  c[2][0] =0 ; c[2][1] =0 ; c[2][2]=1;
  113. }
  114. void sdvig_v(float S,float h, mat c)
  115. {
  116.  unit (c);
  117.  c[0][0] = S; c[0][1] =0 ; c[0][2]=1;
  118.  c[1][0] =0 ; c[1][1] =S ; c[1][2]=(1 - S)*h;
  119.  c[2][0] =0 ; c[2][1] =0 ; c[2][2]=1;
  120. }
  121. void scale_3(float x, float y, float Sx, float Sy, mat c)
  122. {
  123.     unit(c);
  124.  
  125.     c[0][0] = Sx;
  126.     c[0][2] = -Sx*x + x;
  127.     c[1][1] = Sy;
  128.     c[1][2] = -Sy*y + y;
  129. }
  130. void frame(float Vx, float Vy, float Vcx, float Vcy, float Wx, float Wy, float Wcx, float Wcy, mat c)
  131. {
  132.     unit(c);
  133.     mat R, T1;
  134.     unit(R);
  135.     move(-Vcx, -Vcy, c);
  136.     scale_3(0, 0, Wx / Vx, Wy / Vy, R);
  137.     times(R, c, T1); set(T1, c);
  138.     unit(R); R[1][1] = -1;
  139.     times(R, c, T1); set(T1, c);
  140.     move(Wcx, Wcy, R);
  141.     times(R, c, T1); set(T1, c);
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement