Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!rutgers!bellcore!texbell!uhnix1!sugar!sms From: sms@sugar.hackercorp.com (Stanley M. Sutton) Newsgroups: comp.lang.c++ Subject: Class vect2 Keywords: C++ Class-example Message-ID: <3587@sugar.hackercorp.com> Date: 6 Mar 89 02:41:28 GMT Organization: Sugar Land Unix - Houston Lines: 100 Here is a class for 2 dimensional vectors I've written for a game I'm working on. There are some problems with compiling I've coded around, probably due to the fact I'm using version 1.5 of Zortech C++. Someday, when I have some funds, I'm planning to upgrade. I haven't implemented the dot product yet, as I haven't needed it. Suggestions for improvement would be welcomed, as I'm learning. -----------------------------------vect2.hpp------------------------------ #include class vect2 { double x,y; public: vect2(double ix = 0.0, double iy = 0.0) {x = ix; y = iy;}; vect2 operator= (vect2& v){ x = v.x; y = v.y; return (*this); }; vect2 operator+ (vect2& v){ double lx, ly;lx = x + v.x;ly = y + v.y; return vect2(lx,ly); }; vect2 operator- (vect2& v){ double lx, ly;lx = x - v.x;ly = y - v.y; return vect2(lx,ly); }; vect2 operator+= (vect2& v){x += v.x; y += v.y; return *this;}; vect2 operator-= (vect2& v){x -= v.x; y -= v.y; return *this;}; friend vect2 operator* (vect2 &v1, double &s); friend vect2 operator* (double &s, vect2 &v1); double distance (vect2 &v); vect2 unit (); // double dot (vect2 v1, vect2 v2); friend ostream& operator<< (ostream &s,vect2 &v); }; -----------------------------vect2.cpp------------------------------------- #ifndef _vect2.cpp_ #define _vect2.cpp_ #include #include "vect2.hpp" double vect2::distance(vect2 &v) { return (sqrt((x-v.x)*(x-v.x)+(y-v.y)*(y-v.y))); } vect2 vect2::unit() { double lx,ly; lx = x/sqrt( x * x + y * y); ly = y/sqrt( x * x + y * y); return vect2(lx,ly); } ostream& operator<< (ostream &s, vect2 &v) { return ( s << "(" << v.x << ", " << v.y << ")" ); } vect2 operator*(vect2& v, double& s) { return vect2(v.x * s, v.y * s); } vect2 operator*(double& s, vect2& v) { return vect2(v.x * s, v.y * s); } # ifndef NDEBUG main() { vect2 x(1,1),y(2,2),z(5,7),t(0,0); double tmp=5.0; t = x + y; cout << " t" << t << " = x" << x << " + y" << y << "\n" ; cout << " distance from x" << x << " to y" << y << " is "; cout << x.distance(y) << "\n"; t = vect2(4,0); cout << " unit vector for " << t << " is " << t.unit() << "\n"; cout << " z * 5 is " << (z * tmp) << "\n"; // cout << " z * 5 is " << (z * 5) << "\n"; // Produces wrong answer, // possibly 5 is not converted to double. // cout << " z * 5 is " << (z * 5.0) << "\n"; // Compiler bug: 11147 from ZTC2 tmp = x.distance(y); cout << " x.distance(y) * z is " << (tmp * z) << "\n"; // cout << " x.distance(y) * z is " << (x.distance(y) * z) << "\n"; // Compiler bug 11147 from ZTC2 cout << " x += z is " << (x += z) << "\n"; cout << " x -= z is " << (x -= z) << "\n"; } # endif #endif ---------------------------------------- compile/link---------------------- ZTCPP1 -mc -otemp.tmp vect2.cpp ZTC2 temp.tmp -ovect2 LINK vect2/noi; BLINK 2.14 Copyright (c) 1986-88 by Zortech, written by Bjorn N. Freeman-Benson BLINK complete. Time: 16.86 seconds -------------------------------------output--------------------------------- t(3, 3) = x(1, 1) + y(2, 2) distance from x(1, 1) to y(2, 2) is 1.414214 unit vector for (4, 0) is (1, 0) z * 5 is (25, 35) x.distance(y) * z is (7.071068, 9.899495) x += z is (6, 8) x -= z is (1, 1)