"Класс дял работы с графикой"


Страницы: 1
Пользователь: isak
Сообщений: 1
Статус: Пользователь
Зарегистрирован:
5 декабря 2008, 0:16
Был:10 декабря 2008, 23:10
isak
smsup
Дата: 5 декабря 2008, 0:24 Сообщение № 1
Помогите пожалуйста написать клас для работы с графикой. Класс должен описывать такие фигуры как - линия, квадрат, треугольник и функции для перемещения и поворота на заданный угол фигур. Язык программирования С++. Спаисбо!
Пользователь: nash
Сообщений: 1
Статус: Пользователь
Зарегистрирован:
6 декабря 2008, 14:04
Был:18 декабря 2008, 16:56
nash
smsup
Дата: 6 декабря 2008, 14:13 Сообщение № 2
Код на C++
  1. //////////////////////////////////////////////////////////
  2. // basic vector library //
  3. // all major operators overloaded to support vectors //
  4. // Author: Alex V. Boreskoff //
  5. // Last revision: 2/12/94 //
  6. //////////////////////////////////////////////////////////
  7. #ifndef __VECTOR__
  8. #define __VECTOR__
  9. #include <math.h>
  10. class Vector
  11. {
  12. public:
  13. double x, y, z;
  14. Vector () {};
  15. Vector ( double v ) { x = y = z = v; };
  16. Vector ( const Vector& v ) { x = v.x; y = v.y; z = v.z; };
  17. Vector ( double vx, double vy, double vz ) { x = vx; y = vy; z = vz; };
  18. Vector& operator = ( const Vector& v ) { x = v.x; y = v.y; z = v.z; return *this; };
  19. Vector& operator = ( double f ) { x = y = z = f; return *this; };
  20. Vector operator - () const;
  21. Vector& operator += ( const Vector& );
  22. Vector& operator -= ( const Vector& );
  23. Vector& operator *= ( const Vector& );
  24. Vector& operator *= ( double );
  25. Vector& operator /= ( double );
  26. friend Vector operator + ( const Vector&, const Vector& );
  27. friend Vector operator - ( const Vector&, const Vector& );
  28. friend Vector operator * ( const Vector&, const Vector& );
  29. friend Vector operator * ( double, const Vector& );
  30. friend Vector operator * ( const Vector&, double );
  31. friend Vector operator / ( const Vector&, double );
  32. friend Vector operator / ( const Vector&, const Vector& );
  33. friend double operator & ( const Vector& u, const Vector& v ) { return u.x*v.x + u.y*v.y + u.z*v.z; };
  34. friend Vector operator ^ ( const Vector&, const Vector& );
  35. double operator ! () { return (double) sqrt ( x*x + y*y + z*z ); };
  36. double mod(){return (double) sqrt ( x*x + y*y + z*z ); }
  37. double& operator [] ( int n ) { return * ( &x + n ); };
  38. int operator < ( double v ) { return x < v && y < v && z < v; };
  39. int operator > ( double v ) { return x > v && y > v && z > v; };
  40. };
  41. class Ray
  42. {
  43. public:
  44. Vector Org;
  45. Vector Dir; // direction must be normalyzed
  46. Ray () {};
  47. Ray ( Vector& o, Vector& d ) { Org = o; Dir = d; };
  48. Vector Point ( double t ) { return Org + Dir*t; };
  49. };
  50. //////////////////// implementation /////////////////////////
  51. inline Vector Vector :: operator - () const
  52. {
  53. return Vector ( -x, -y, -z );
  54. }
  55. inline Vector operator + ( const Vector& u, const Vector& v )
  56. {
  57. return Vector ( u.x + v.x, u.y + v.y, u.z + v.z );
  58. }
  59. inline Vector operator - ( const Vector& u, const Vector& v )
  60. {
  61. return Vector ( u.x - v.x, u.y - v.y, u.z - v.z );
  62. }
  63. inline Vector operator * ( const Vector& u, const Vector& v )
  64. {
  65. return Vector ( u.x * v.x, u.y * v.y, u.z * v.z );
  66. }
  67. inline Vector operator * ( const Vector& u, double f )
  68. {
  69. return Vector ( u.x * f, u.y * f, u.z * f );
  70. }
  71. inline Vector operator * ( double f, const Vector& v )
  72. {
  73. return Vector ( f * v.x, f * v.y, f * v.z );
  74. }
  75. inline Vector operator / ( const Vector& v, double f )
  76. {
  77. return Vector ( v.x / f, v.y / f, v.z / f );
  78. }
  79. inline Vector operator / ( const Vector& u, const Vector& v )
  80. {
  81. return Vector ( u.x / v.x, u.y / v.y, u.z / v.z );
  82. }
  83. inline Vector& Vector :: operator += ( const Vector& v )
  84. {
  85. x += v.x;
  86. y += v.y;
  87. z += v.z;
  88. return *this;
  89. }
  90. inline Vector& Vector :: operator -= ( const Vector& v )
  91. {
  92. x -= v.x;
  93. y -= v.y;
  94. z -= v.z;
  95. return *this;
  96. }
  97. inline Vector& Vector :: operator *= ( double v )
  98. {
  99. x *= v;
  100. y *= v;
  101. z *= v;
  102. return *this;
  103. }
  104. inline Vector& Vector :: operator *= ( const Vector& v )
  105. {
  106. x *= v.x;
  107. y *= v.y;
  108. z *= v.z;
  109. return *this;
  110. }
  111. inline Vector& Vector :: operator /= ( double v )
  112. {
  113. x /= v;
  114. y /= v;
  115. z /= v;
  116. return *this;
  117. }
  118. /////////////////////////// Functions /////////////////////////////////
  119. inline Vector Normalize ( Vector& v ) { return v / !v; };
  120. Vector RndVector ();
  121. Vector Invert_vector(Vector&);
  122. Vector& Clip ( Vector& );
  123. #endif
  124. #include <math.h>
  125. #include <stdlib.h>
  126. #include "..\include\Vector.hpp"
  127. //
  128. Vector operator ^ ( const Vector& u, const Vector& v )
  129. {
  130. return Vector ( u.y * v.z - u.z * v.y,
  131. u.z * v.x - u.x * v.z,
  132. u.x * v.y - u.y * v.x );
  133. }
  134. Vector RndVector ()
  135. {
  136. Vector v ( rand () - 0.5*RAND_MAX, rand () - 0.5*RAND_MAX, rand () - 0.5*RAND_MAX );
  137. return Normalize ( v );
  138. }
  139. Vector& Clip ( Vector& v )
  140. {
  141. if ( v.x < 0.0 )
  142. v.x = 0.0;
  143. else
  144. if ( v.x > 1.0 )
  145. v.x = 1.0;
  146. if ( v.y < 0.0 )
  147. v.y = 0.0;
  148. else
  149. if ( v.y > 1.0 )
  150. v.y = 1.0;
  151. if ( v.z < 0.0 )
  152. v.z = 0.0;
  153. else
  154. if ( v.z > 1.0 )
  155. v.z = 1.0;
  156. return v;
  157. }
  158. Vector Invert_vector( Vector& v)
  159. {
  160. return Vector (-v.x,-v.y,-v.z);
  161. };
  162. #ifndef __MATRIX__
  163. #define __MATRIX__
  164. #include "..\INCLUDE\Vector.HPP"
  165. class Matrix
  166. {
  167. public:
  168. double x [4][4];
  169. Matrix () {};
  170. Matrix ( double );
  171. Matrix& operator += ( const Matrix& );
  172. Matrix& operator -= ( const Matrix& );
  173. Matrix& operator *= ( const Matrix& );
  174. Matrix& operator *= ( double );
  175. Matrix& operator /= ( double );
  176. void Invert ();
  177. void Transpose ();
  178. friend Matrix operator + ( const Matrix&, const Matrix& );
  179. friend Matrix operator - ( const Matrix&, const Matrix& );
  180. friend Matrix operator * ( const Matrix&, double );
  181. friend Matrix operator * ( const Matrix&, const Matrix& );
  182. friend Vector operator * ( const Matrix&, const Vector& );
  183. };
  184. Matrix Translate ( const Vector& );
  185. Matrix Scale ( const Vector& );
  186. Matrix RotateX ( double );
  187. Matrix RotateY ( double );
  188. Matrix RotateZ ( double );
  189. Matrix Rotate ( const Vector& v, double );
  190. Matrix MirrorX ();
  191. Matrix MirrorY ();
  192. Matrix MirrorZ ();
  193. Matrix Proectir(float,float);
  194. #endif
  195. #include <math.h>
  196. #include "..\include\Matrix.hpp"
  197. Matrix :: Matrix ( double v )
  198. {
  199. for ( int i = 0; i < 4; i++)
  200. for ( int j = 0; j < 4; j++)
  201. x [i][j] = (i == j) ? v : 0.0;
  202. x [3][3] = 1;
  203. }
  204. void Matrix :: Invert ()
  205. {
  206. Matrix Out ( 1 );
  207. for ( int i = 0; i < 4; i++ )
  208. {
  209. double d = x [i][i];
  210. if ( d != 1.0)
  211. {
  212. for ( int j = 0; j < 4; j++ )
  213. {
  214. Out.x [i][j] /= d;
  215. x [i][j] /= d;
  216. }
  217. }
  218. for ( int j = 0; j < 4; j++ )
  219. {
  220. if ( j != i )
  221. {
  222. if ( x [j][i] != 0.0)
  223. {
  224. double mulby = x[j][i];
  225. for ( int k = 0; k < 4; k++ )
  226. {
  227. x [j][k] -= mulby * x [i][k];
  228. Out.x [j][k] -= mulby * Out.x [i][k];
  229. }
  230. }
  231. }
  232. }
  233. }
  234. *this = Out;
  235. }
  236. void Matrix :: Transpose ()
  237. {
  238. double t;
  239. for ( int i = 0; i < 4; i++ )
  240. for ( int j = i; j < 4; j++ )
  241. if ( i != j )
  242. {
  243. t = x [i][j];
  244. x [i][j] = x [j][i];
  245. x [j][i] = t;
  246. }
  247. }
  248. Matrix& Matrix :: operator += ( const Matrix& A )
  249. {
  250. for ( int i = 0; i < 4; i++ )
  251. for ( int j = 0; j < 4; j++ )
  252. x [i][j] += A.x [i][j];
  253. return *this;
  254. }
  255. Matrix& Matrix :: operator -= ( const Matrix& A )
  256. {
  257. for ( int i = 0; i < 4; i++ )
  258. for ( int j = 0; j < 4; j++ )
  259. x [i][j] -= A.x [i][j];
  260. return *this;
  261. }
  262. Matrix& Matrix :: operator *= ( double v )
  263. {
  264. for ( int i = 0; i < 4; i++ )
  265. for ( int j = 0; j < 4; j++ )
  266. x [i][j] *= v;
  267. return *this;
  268. }
  269. Matrix& Matrix :: operator *= ( const Matrix& A )
  270. {
  271. Matrix res = *this;
  272. for ( int i = 0; i < 4; i++ )
  273. for ( int j = 0; j < 4; j++ )
  274. {
  275. double sum = 0;
  276. for ( int k = 0; k < 4; k++ )
  277. sum += res.x [i][k] * A.x [k][j];
  278. x [i][j] = sum;
  279. }
  280. return *this;
  281. }
  282. Matrix operator + ( const Matrix& A, const Matrix& B )
  283. {
  284. Matrix res;
  285. for ( int i = 0; i < 4; i++ )
  286. for ( int j = 0; j < 4; j++ )
  287. res.x [i][j] = A.x [i][j] + B.x [i][j];
  288. return res;
  289. }
  290. Matrix operator - ( const Matrix& A, const Matrix& B )
  291. {
  292. Matrix res;
  293. for ( int i = 0; i < 4; i++ )
  294. for ( int j = 0; j < 4; j++ )
  295. res.x [i][j] = A.x [i][j] - B.x [i][j];
  296. return res;
  297. }
  298. Matrix operator * ( const Matrix& A, const Matrix& B )
  299. {
  300. Matrix res;
  301. for ( int i = 0; i < 4; i++ )
  302. for ( int j = 0; j < 4; j++ )
  303. {
  304. double sum = 0;
  305. for ( int k = 0; k < 4; k++ )
  306. sum += A.x [i][k] * B.x [k][j];
  307. res.x [i][j] = sum;
  308. }
  309. return res;
  310. }
  311. Matrix operator * ( const Matrix& A, double v )
  312. {
  313. Matrix res;
  314. for ( int i = 0; i < 4; i++ )
  315. for ( int j = 0; j < 4; j++ )
  316. res.x [i][j] = A.x [i][j] * v;
  317. return res;
  318. }
  319. Vector operator * ( const Matrix& M, const Vector& v )
  320. {
  321. Vector res;
  322. res.x = v.x * M.x [0][0] + v.y * M.x [1][0] + v.z * M.x [2][0] + M.x [3][0];
  323. res.y = v.x * M.x [0][1] + v.y * M.x [1][1] + v.z * M.x [2][1] + M.x [3][1];
  324. res.z = v.x * M.x [0][2] + v.y * M.x [1][2] + v.z * M.x [2][2] + M.x [3][2];
  325. double denom = v.x * M.x [0][3] + v.y * M.x [1][3] + v.z * M.x [2][3] + M.x [3][3];
  326. if ( denom != 1.0 )
  327. res /= denom;
  328. return res;
  329. }
  330. //////////////////////// Derived classes /////////////////////////////
  331. Matrix Translate ( const Vector& Loc )
  332. {
  333. Matrix res ( 1 );
  334. res.x [3][0] = Loc.x;
  335. res.x [3][1] = Loc.y;
  336. res.x [3][2] = Loc.z;
  337. return res;
  338. }
  339. Matrix Scale ( const Vector& v )
  340. {
  341. Matrix res ( 1 );
  342. res.x [0][0] = v.x;
  343. res.x [1][1] = v.y;
  344. res.x [2][2] = v.z;
  345. return res;
  346. }
  347. Matrix RotateX ( double Angle )
  348. {
  349. Matrix res ( 1 );
  350. double Cosine = cos ( Angle );
  351. double Sine = sin ( Angle );
  352. res.x [1][1] = Cosine;
  353. res.x [2][1] = -Sine;
  354. res.x [1][2] = Sine;
  355. res.x [2][2] = Cosine;
  356. return res;
  357. }
  358. Matrix RotateY ( double Angle )
  359. {
  360. Matrix res ( 1 );
  361. double Cosine = cos ( Angle );
  362. double Sine = sin ( Angle );
  363. res.x [0][0] = Cosine;
  364. res.x [2][0] = -Sine;
  365. res.x [0][2] = Sine;
  366. res.x [2][2] = Cosine;
  367. return res;
  368. }
  369. Matrix RotateZ ( double Angle )
  370. {
  371. Matrix res ( 1 );
  372. double Cosine = cos ( Angle );
  373. double Sine = sin ( Angle );
  374. res.x [0][0] = Cosine;
  375. res.x [1][0] = -Sine;
  376. res.x [0][1] = Sine;
  377. res.x [1][1] = Cosine;
  378. return res;
  379. }
  380. Matrix Rotate ( const Vector& axis, double angle )
  381. {
  382. Matrix res ( 1 );
  383. double Cosine = cos ( angle );
  384. double Sine = sin ( angle );
  385. res.x [0][0] = axis.x * axis.x + ( 1 - axis.x * axis.x ) * Cosine;
  386. res.x [0][1] = axis.x * axis.y * ( 1 - Cosine ) + axis.z * Sine;
  387. res.x [0][2] = axis.x * axis.z * ( 1 - Cosine ) - axis.y * Sine;
  388. res.x [0][3] = 0;
  389. res.x [1][0] = axis.x * axis.y * ( 1 - Cosine ) - axis.z * Sine;
  390. res.x [1][1] = axis.y * axis.y + ( 1 - axis.y * axis.y ) * Cosine;
  391. res.x [1][2] = axis.y * axis.z * ( 1 - Cosine ) + axis.x * Sine;
  392. res.x [1][3] = 0;
  393. res.x [2][0] = axis.x * axis.z * ( 1 - Cosine ) + axis.y * Sine;
  394. res.x [2][1] = axis.y * axis.z * ( 1 - Cosine ) - axis.x * Sine;
  395. res.x [2][2] = axis.z * axis.z + ( 1 - axis.z * axis.z ) * Cosine;
  396. res.x [2][3] = 0;
  397. res.x [3][0] = 0;
  398. res.x [3][1] = 0;
  399. res.x [3][2] = 0;
  400. res.x [3][3] = 1;
  401. return res;
  402. }
  403. Matrix MirrorX ()
  404. {
  405. Matrix res ( 1 );
  406. res.x [0][0] = -1;
  407. return res;
  408. }
  409. Matrix MirrorY ()
  410. {
  411. Matrix res ( 1 );
  412. res.x [1][1] = -1;
  413. return res;
  414. }
  415. Matrix MirrorZ ()
  416. {
  417. Matrix res ( 1 );
  418. res.x [2][2] = -1;
  419. return res;
  420. }
  421. Matrix Proectir(float a,float b){
  422. Matrix res(1);
  423. res.x[0][0]=cos(a);
  424. res.x[1][0]=0; res.x[2][0]=sin(a); res.x[3][0]=0;
  425. res.x[0][1]=sin(a)*sin(b); res.x[1][1]=cos(a); res.x[2][1]=-sin(a)*cos(a); res.x[3][1]=0;
  426. res.x[0][2]=0;res.x[1][2]=0;res.x[2][2]=0;res.x[3][2]=0;
  427. res.x[0][3]=0;res.x[1][2]=0;res.x[2][2]=0;res.x[3][2]=1;
  428. return res;
  429. }
  430.  
При использовании обязательна ссылка на http://DMTSoft.ru


требуется просто задать массив векторов, содержащий координаты вершин(для многоугольников), либо (концов отрезка)
А все функции поворота, переноса, уже есть.

Страницы: 1