inline Vector2D operator * ( const Vector2D& u, const Vector2D& v ) return Vector2D ( u.x*v.x, u.y*v.y );
inline Vector2D operator * ( const Vector2D& v, float a ) return Vector2D ( v.x*a, v.y*a );
inline Vector2D operator * (float a, const Vector2D& v ) return Vector2D (v.x*a, v.y*a );
inline Vector2D operator / ( const Vector2D& u, const Vector2D& v ) return Vector2D ( u.x/v.x, u.y/v.y );
inline Vector2D operator / ( const Vector2D& v, float a ) return Vector2D ( v.x/a, v.y/a ),
inline float operator & ( const Vector2D& u, const Vector2D& v ) return u.x*v.x + u.y*v.y;
#endif
7. Преобразования на плоское
// File vector2d.cpp #include "vector2d.h"
////////////////////// member functions //////////////////
int Vector2D :: classify (const Vector2D& p,const Vector2D& q) const {
Vector2D a = q - p;
Vector2D b = *this - p;
float s = a.x * b.y - a.y * b.x;
if ( s > 0.0 )
return LEFT;
if ( s < 0.0 )
return RIGHT;
if ( a.x * b.x < 0.0 || a.y * b.y < 0.0 ) return BEHIND;
if (a.length () < b.length ()) return BEYOND;
if ( p == "this )
return ORIGIN;
if ( q == *this )
return DESTINATION;
return BETWEEN;
}
// File matrix2d.h
#ifndef _MATRIX2D__
#define ___MATRIX2D_
#include "vector2d.h"
class Matrix2D {
public:
float a [2][2];
Matrix2D () {}
Matrix2D (float);
Matrix2D ( const Matrix2D& );
Matrix2D& operator = ( const Matrix2D& ) Matrix2D& operator = (float); Matrix2D& operator += ( const Matrix2D& ); Matrix2D& operator -= ( const Matrix2D& ); Matrix2D& operator *= ( const Matrix2D& ); Matrix2D& operator *= (float); Matrix2D& operator /= (float);
float * operator 0 (int i) {
return &x [i][0];
}
Компьютерная графика. Полигональные модели
void invert (); void transpose ();
static Matn*x2D scale ( const Vector2D& ); static Matrix2D rotate (float); static Matrix2D mirrorX (); static Matrix2D mirrorY ();
friend Matrix2D operator + (const Matrix2D&,const Matrix2D&); friend Matrix2D operator - (const Matrix2D&,const Matrix2D&); friend Matrix2D operator * (const Matrix2D&,const Matrix2D&); friend Matrix2D operator * (const Matrix2D&,float); friend Matrix2D operator * (float, const Matrix2D&); friend Vector2D operator * (const Matrix2D&,const Vector2D&);