/****************/ /* 3d.cpp */ /****************/ // Ohjelma, jolla piirretään sin x:n kuvaaja // /vl-96 #ifndef M3D_H #define M3D_H #include //------------------------------------------------------------------------------ typedef double (*tFunc)(double x); // R->R funktiotyyppi typedef double (*tFunc2)(double x, double y); // R2->R funktiotyyppi //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ class cDPoint { // Reaalilukupiste //------------------------------------------------------------------------------ double x,y; public: cDPoint (double ix=0, double iy=0) : x(ix),y(iy) {} double px() const { return x; } double py() const { return y; } }; //------------------------------------------------------------------------------ class cDRect { // Reaaliluku suorakaide; //------------------------------------------------------------------------------ cDPoint p1,p2; // Vasen alanurkka ja oikea ylänurkka public: cDRect(double x1=0,double x2=1,double y1=0,double y2=1) : p1(x1,y1),p2(x2,y2) {} cDRect(tFunc f,double x1, double x2,double dx=0) { scan(f,x1,x2,dx); } void scan(tFunc f,double x1, double x2,double dx=0); double Width() const { return p2.px() - p1.px(); } double Height() const { return p2.py() - p1.py(); } double MidX() const { return ( p1.px() + p2.px() ) / 2.0; } double MidY() const { return ( p1.py() + p2.py() ) / 2.0; } const cDPoint &rp1() const { return p1; } const cDPoint &rp2() const { return p2; } }; //------------------------------------------------------------------------------ const int VSIZE=4; // VSIZE voi olla joko 3 (2D kuvat) tai 4 (3D kuvat) int vsize(int &i); //------------------------------------------------------------------------------ class cVector { //------------------------------------------------------------------------------ protected: double a[VSIZE]; public: cVector(double a0=0.0, double a1=0.0, double a2=0.0); cVector(int i, double ai=1.0); double operator*(const cVector &b) const; // Sisätulo double operator[](int i) const { return a[vsize(i)]; } // v[2] double &operator[](int i) { return a[vsize(i)]; } }; //------------------------------------------------------------------------------ class cMatrix { //------------------------------------------------------------------------------ protected: cVector r[VSIZE]; public: cMatrix() { ident(); } cVector &operator[](int j) { return r[vsize(j)]; } void ident(); const cVector &operator[](int j) const { return r[vsize(j)]; } cVector column(int i) const; friend cMatrix operator*(const cMatrix &A, const cMatrix &B); // C = A*B cMatrix &operator*=(const cMatrix &B); }; //------------------------------------------------------------------------------ class cTMatrix : public cMatrix { // Transformaatiomatriisi //------------------------------------------------------------------------------ public: cTMatrix() : cMatrix() {}; // Yksikkömatriisi cTMatrix(const cMatrix &M) : cMatrix(M) {} cTMatrix(int axis,double deg); // Kierto akselin ympäri void scale(const cDRect &dRect, const TRect &iRect,bool equal=true); TPoint operator()(double x,double y, double z) const; TPoint operator()(double x,double y) const { return operator()(x,y,0); } }; //------------------------------------------------------------------------------ class cRotMatrix : public cMatrix { // Rotaatiomatriisi //------------------------------------------------------------------------------ public: cRotMatrix(int axis,double deg); }; //------------------------------------------------------------------------------ class cCoordinate { // Piirtää koordinaatiston R2:ssa //------------------------------------------------------------------------------ cDPoint origo; cDRect dRect; cDPoint ds; // small = pikkutikkujen välit cDPoint db; // big = isojen tikkujen välit const char *xformat; const char *yformat; void XTicks(TDC &dc, const cTMatrix &A,double dx, double dty) const; void YTicks(TDC &dc, const cTMatrix &A,double dy, double dtx) const; void XLabels(TDC &dc, const cTMatrix &A, double dx, double dty) const; void YLabels(TDC &dc, const cTMatrix &A, double dy, double dtx) const; public: cCoordinate() : origo(0,0), dRect(), db(1.0,1.0), ds(0.1,0.1) { xformat = "%3.1lf"; yformat = "%3.1lf"; } void SetOrigo(const cDPoint &pt) { origo = pt; } void SetRect(const cDRect rc) { dRect = rc; } void SetSmallTicks(const cDPoint &pt) { ds = pt; } void SetBigTicks(const cDPoint &pt) { db = pt; } void SetXFormat(const char *format) { xformat = format; } void SetYFormat(const char *format) { yformat = format; } void Draw(TDC &dc, const cTMatrix &A) const; }; //------------------------------------------------------------------------------ void Draw(TDC &dc, const cTMatrix &A, tFunc f, double x1, double x2, double dx, COLORREF col=RGB(0,0,0)); //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ //============================================================== #endif