3D Rotational Matrix
struct Point { double x, y, z; };
or
double Point[3];
The function prototype would then have this form:
Point Rotate(Point &P, double alpha, double beta, double gamma);
if you would like not to modify the original point and obtain another, by rotation, or
void Rotate(Point &P, double alpha, double beta, double gamma);
which would rotate the point and affect its coordinates, without creating a second point. In both versions I have considered alpha, beta and gamma to be the angles about the X, Y and Z axis. In what implementation of this function is concerned, it is easy to construct the 3D rotation matrix by first calculating sin(alpha), cos(alpha), sin(beta), cos(beta), sin(gamma), cos(gamma), storing them in separate variables, and then calculate each element of the matrix. To apply the rotation on the given point you would consider that the point forms a matrix of 3 rows and 1 column, with its elements coresponding to the 3 coordinates. Then you would do matrix multiplication between this row-matrix and the rotation matrix, which I assume would not be very difficult to do. In the end, return the new point if you have considered the first version of the prototype.
I hope this helps.class Point { int x,y,z; void rotate(alpha_x, alpha_y, alpha_z); void scale(scale_x, scale_y, scale_z); }where rotate and scale are the first of a collection of functions you'll probably want to manipulate each point. rotate for example, would translate any point around a central point (e.g. x=y=z=0), using the classical matrix transformation, i.e to rotate about x-axis you want: To rotate about the y_axis you want: Then for the z-axis: At this point you can go two ways: a) Define a single array of Point, which contains the nodes of your entire mesh for the sphere (the option I'd do). Then have a new class relating the interconnectivity between nodes - Pro: Faster; Con: Not tightly encapsulated. b) Define a class that handles each polygon separately - Con: a little memory inefficient. Thus
class TriNodes { int m_points[3]; // you put in here the index values for each Point }
You then have something like:
class Sphere { std::vector<Point> m_points; std::vector<TriNodes> m_nodes; // with functions void addNode(int x, y, z); void addTriMesh(int a, int b, int c); // where a,b,c reference m_points[a], m_points[b] etc. void scale(..); void rotate(..); void display(...); };
Seems like something CodeCogs should have, but can't find anything. Why don't you post it to CodeCogs. Keep it 'Private' initially make me an editor and I'll helping you out..
JohnPoint P[100];
I know that it would only store the data, while there wouldn't be any copies of the functions inside the class, but is it really necessary to have these transformation functions linked to each point, or could they be declared outside the class ? This way they could be made into separate components on the website and may be downloaded separately. They could live in a category called "transformations" or something similar. Please let me know your opinion on this.
static virtual class PolyMethods { protected: // rotates the given array of Points about (0,0,0) // by changing each Point's bearing and elevation by the amounts given void rotate(Point * points, double bearing_change, double elevation_change) { /* do all that trigonometry stuff here */ } // translate, scale, etc... } virtual class Polyhedron: public PolyMethods { protected: Point points[]; // not sure about this... // constructor Polyhedron(Point* initial_data) { self.points = initial_data; } // rotate... this method gets the Points data, then passes it to the method it overrides void rotate(double bearing_change, double elevation_change) { super.rotate( self.points, bearing_change, elevation_change) } // translate, scale, etc... }; class Sphere: public Polyhedron { // In here you do something about knowing how many points there are // perhaps you override the constructor or something... I don't know };
Or, I suppose, you might just make Polyhedron non-virtual and use it directly... But I'm not sure how you would deal with the variable number of points then. In fact I'm not sure anyway...
Feel free to correct me on any of this, my C++ is a bit rusty!
Login