Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <stack>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. class Point {
  10. public:
  11. int x = 0;
  12. int y = 0;
  13. Point() {}
  14. Point(int x, int y) {
  15. this->x = x;
  16. this->y = y;
  17. }
  18. };
  19.  
  20. int side(Point start, Point end, Point point) {
  21. return (end.x - start.x)*(point.y - start.y) - (end.y - start.y)*(point.x - start.x);
  22. }
  23.  
  24. vector<Point> order_points (vector<Point> points){
  25.  
  26. Point corner = points[0];
  27. Point p1 = points[1];
  28. Point p2 = points[2];
  29. Point p3 = points[3];
  30.  
  31. Point opposite, leftside, rightside;
  32. if (side(corner, p1, p2) * side(corner, p1, p3) < 0) {
  33. opposite = p1;
  34. if (side(corner, p1, p2) < 0) {
  35. leftside = p2;
  36. rightside = p3;
  37. }
  38. else {
  39. leftside = p3;
  40. rightside = p2;
  41. }
  42. }
  43. else if (side(corner, p2, p1) * side(corner, p2, p3) < 0) {
  44. opposite = p2;
  45. if (side(corner, p2, p1) < 0) {
  46. leftside = p1;
  47. rightside = p3;
  48. }
  49. else {
  50. leftside = p3;
  51. rightside = p1;
  52. }
  53. }
  54. else if (side(corner, p3, p1) * side(corner, p3, p2) < 0) {
  55. opposite = p3;
  56. if (side(corner, p3, p1) < 0) {
  57. leftside = p1;
  58. rightside = p2;
  59. }
  60. else {
  61. leftside = p2;
  62. rightside = p1;
  63. }
  64. }
  65. else {
  66. throw("Computation error: right points order not found");
  67. }
  68.  
  69. vector<Point> answer;
  70. answer.push_back(corner);
  71. answer.push_back(leftside);
  72. answer.push_back(opposite);
  73. answer.push_back(rightside);
  74. return answer;
  75. }
  76.  
  77.  
  78. void solve_matrix(int arank, double A[8][9]) {
  79. //Jordan-Gauss
  80.  
  81. const int nrows = arank;
  82. const int ncols = arank+1;
  83.  
  84. stack < pair < int,int> > permutations;
  85.  
  86. cout << "start (must be the same as an input):" << endl;
  87. for (int i = 0; i < nrows; i++) {
  88. for (int j = 0; j < ncols; j++) {
  89. cout << A[i][j] << " ";
  90. }
  91. cout << endl;
  92. }
  93. cout << "go" << endl;
  94. cout<< endl;
  95.  
  96. for (int lead = 0; lead < nrows; lead++) {
  97. double d, m;
  98.  
  99. int r;
  100. for (r = lead; r < nrows && A[lead][r] == 0; r++);
  101. if (r == nrows) continue;
  102. // The column is cleared out. There shouldn't be any non-zeroes above
  103. // in this case since the matrix is not singular.
  104.  
  105. if (r != lead) { // Make the right row a leading one
  106. permutations.push(make_pair(r, lead)); // Save the permutation made
  107. for (int i = 0; i < ncols; i++) {
  108. swap(A[i][lead], A[i][r]);
  109. }
  110. }
  111.  
  112. for (int r = 0; r < nrows; r++) {
  113. d = A[lead][lead];
  114.  
  115. if (A[r][lead] == 0) continue;
  116.  
  117. m = A[r][lead] / d;
  118.  
  119. for (int c = 0; c < ncols; c++) {
  120. if (r == lead) {
  121. A[r][c] /= d;
  122. } else {
  123. A[r][c] -= A[lead][c] * m;
  124. }
  125. }
  126. }
  127. for (int i = 0; i < nrows; i++) {
  128. for (int j = 0; j < ncols; j++) {
  129. cout << A[i][j] << " ";
  130. }
  131. cout << endl;
  132. }
  133. cout<< endl;
  134. }
  135.  
  136. // The answers must be in the last columns, but we need to restore the order
  137. while(!permutations.empty()) {
  138. pair <int,int> p = permutations.top();
  139. permutations.pop();
  140. swap(A[p.first][arank], A[p.second][arank]);
  141. }
  142. }
  143.  
  144. Point multiply(double matrix[3][3], int x, int y) {
  145. double x1 = matrix[0][0]*x + matrix[1][0]*y + matrix[2][0];
  146. double y1 = matrix[0][1]*x + matrix[1][1]*y + matrix[2][1];
  147. double z1 = matrix[0][2]*x + matrix[1][2]*y + matrix[2][2];
  148. return Point(round(x1/z1), round(y1/z1));
  149. }
  150.  
  151. bool in_bounds(int c, int r, int width, int height) {
  152. return c >= 0 && c < width && r >= 0 && r < height;
  153. }
  154.  
  155. int main()
  156. {
  157. const int max_width = 1000;
  158. ifstream in;
  159. ofstream out;
  160. in.open("input.txt");
  161. out.open("output.txt");
  162.  
  163. int in_width, in_height;
  164. in >> in_width >> in_height;
  165. char in_image[max_width][max_width];
  166.  
  167. vector <Point> source;
  168. for (int i = 0; i < 4; i++) {
  169. int x, y;
  170. in >> x >> y;
  171. source.push_back(Point(x, y));
  172. }
  173. source = order_points(source);
  174.  
  175. vector <Point> destination;
  176. int out_width, out_height;
  177. in >> out_width >> out_height; //rectangle sides
  178. destination.push_back(Point(0, 0));
  179. destination.push_back(Point(out_width, 0));
  180. destination.push_back(Point(out_width, out_height));
  181. destination.push_back(Point(0, out_height));
  182.  
  183. /*
  184. source.clear();
  185. destination.clear();
  186. source.emplace_back(0, 0);
  187. source.emplace_back(0, 1);
  188. source.emplace_back(1, 1);
  189. source.emplace_back(1, 0);
  190. destination.emplace_back(0, 0);
  191. destination.emplace_back(0, 1);
  192. destination.emplace_back(1, 1);
  193. destination.emplace_back(1, 0);
  194. */
  195.  
  196. for (int y = 0; y < in_height; y++) {
  197. for(int x = 0; x < in_width; x++) {
  198. in.get(in_image[y][x]);
  199. }
  200. in.get();
  201. }
  202.  
  203.  
  204. double transform_equations[8][9];
  205. for (int i = 0; i < 8; i++) {
  206. for (int j = 0; j < 9; j++) {
  207. transform_equations[i][j] = 0;
  208. }
  209. }
  210. for (unsigned i = 0; i < 4; i++) {
  211. transform_equations[2*i][0] = source[i].x;
  212. transform_equations[2*i][1] = source[i].y;
  213. transform_equations[2*i][2] = 1;
  214.  
  215. transform_equations[2*i+1][3] = source[i].x;
  216. transform_equations[2*i+1][4] = source[i].y;
  217. transform_equations[2*i+1][5] = 1;
  218.  
  219. transform_equations[2*i] [6] = - destination[i].x * source[i].x;
  220. transform_equations[2*i] [7] = - destination[i].x * source[i].y;
  221. transform_equations[2*i+1][6] = - destination[i].y * source[i].x;
  222. transform_equations[2*i+1][7] = - destination[i].y * source[i].y;
  223.  
  224. ////
  225. transform_equations[2*i] [8] = destination[i].x;
  226. transform_equations[2*i+1][8] = destination[i].y;
  227.  
  228. }
  229.  
  230. for (int i = 0; i < 8; i++) {
  231. for (int j = 0; j < 9; j++) {
  232. cout << transform_equations[i][j] << " ";
  233. }
  234. cout << endl;
  235. }
  236. cout<< endl;
  237.  
  238.  
  239. solve_matrix(8, transform_equations);
  240.  
  241. double transform_matrix[3][3];
  242. for (int i = 0; i < 8; i++) {
  243. transform_matrix[i/3][i%3] = transform_equations[i][8];
  244. }
  245. transform_matrix[2][2] = 1;
  246.  
  247. char out_image[max_width][max_width];
  248. for (int r = 0; r < out_height; r++) {
  249. for (int c = 0; c < out_width; c++) {
  250. out_image[r][c] = ' ';
  251. }
  252. }
  253.  
  254. for (int r = 0; r < in_height; r++) {
  255. for(int c = 0; c < in_width; c++) {
  256. Point dest = multiply(transform_matrix, c, r);
  257. if (in_bounds(dest.x, dest.y, out_width, out_height)) {
  258. out_image[dest.y][dest.x] = in_image[r][c];
  259. }
  260. }
  261. }
  262.  
  263. for (int r = 0; r < out_height; r++) {
  264. cout << ":";
  265. for (int c = 0; c < out_width; c++) {
  266. cout << out_image[r][c];
  267. out << out_image[r][c];
  268. }
  269. //out << endl;
  270. cout << ':' << endl;
  271. }
  272.  
  273. in.close();
  274. out.close();
  275.  
  276. return 0;
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement