steve@hite386.UUCP (Steve Hite) (02/13/90)
I am trying to put together some 2-D graphics routines in C++ and am
having a few problems. I got the matrix multiplication routines to work
(thanks to examples in Bruce Eckel and Tony Hansen's books!) but am having
trouble with the specifications for scaling, rotating and translating a
figure. I would like to be able to define a figure with a point array and
an edge array, and perform the aforementioned functions on the figure.
Here's the type of usage I had in mind:
double p_values[] = { ...values for point array (matrix)... };
double e_values[] = { ...values for edge array (matrix)... };
matrix *p_matrix = new matrix(3, 3, p_values);
matrix *e_matrix = new matrix(3, 2, e_values);
figure fig(p_matrix, e_matrix);
fig.rotate(45.0);
fig.translate(25.0, 55.0);
fig.scale(0.5, 2.0);
fig.draw();
Here's a portion of the code that I'm using. I hope that I've included
enough to make my intentions clear. I made draw, rotate, translate, and
scale virtual because I'm going to have circles, squares, curves, etc. which
will require the same functions but perform the operations in a different
way (I know you know that :-)).
I am using Zortech C++ v2.06 and get the following error message re-
lating to the last function in this sample portion (listed at the end
of this article) :
matrix& figure::rotate(double value)
^
"shape.cpp", line 194 Syntax error: not found or ambiguous reference to
function 'shape'
Is it incorrect of me to try and return a reference type to matrix
when I am defining a member function in the class figure? Is the line:
return (*points * *transform);
incorrect?
I am new to OOP in C++ and appreciate all the help I can get.
-------------------------- begin C++ source example ------------------------
class matrix {
private:
struct _mat {
double **m; // pointer to matrix
int r; // matrix row
int c; // matrix column
int ref; // reference count
} *mat;
public:
// Constructors
matrix(int rows = 3, int cols = 3, double value = 0.0);
matrix(int rows, int cols, double *values);
matrix(matrix& m); // copy constructor
// Operators
friend matrix& operator*(matrix& m1, matrix& m2);
matrix& operator=(matrix& m);
// Destructor
~matrix();
// Miscellaneous
nrows() { return mat->r; } // return number of rows in matrix
ncols() { return mat->c; } // return number of columns in matrix
double& val(int i, int j); // return reference to an element in
// the matrix
};
class shape {
public:
shape();
virtual void draw() = 0;
virtual matrix& scale(double, double) = 0;
virtual matrix& translate(double, double) = 0;
virtual matrix& rotate(double) = 0;
};
class figure : public shape {
matrix *points; // point matrix
matrix *edges; // edge matrix
public:
figure(matrix *p, matrix *e);
void draw();
int enclosed(point *pt);
matrix& scale(double, double);
matrix& translate(double, double);
matrix& rotate(double);
};
figure::figure(matrix *p, matrix *e)
{
points = p;
edges = e;
}
matrix& figure::rotate(double value)
{
matrix *transform;
transform = new matrix(3, 3, 0.0);
transform->val(0, 0) = cos(value);
transform->val(0, 1) = sin(value);
transform->val(1, 0) = -sin(value);
transform->val(1, 1) = cos(value);
transform->val(2, 2) = 1;
return (*points * *transform);
}
---------------------------- end C++ source example ------------------------
--------------------------------------
Steve Hite
...gatech!uflorida!unf7!hite386!steve