Section 5 - Stars On Your Console

3B.5.1 A Return to Sparse Matrices
While not absolutely necessary, it will be nice to use sparse matrices now, because our goal will be something like an 80 x 80 grid of characters to print on the console, each of which will either be a blank (if there is no star there) or an '*' (if there is). Since there are 6400 grid locations and only 100 stars, this is a relatively sparse matrix. It will be entertaining to use this data structure and further confirm we haven't made any errors in our code from last week.
3B.5.2 Plotting the Stars
Our job now is to take the x-y-z coordinates and project them onto some plane in space -- we can take the x-y plane for simplicity. By doing so, our projection is very easy: we toss out the z coordinate and just use x and y. In a more powerful program, we would allow the client to specify an orientation plane and project onto that.
Now, we have 100 (x,y) pairs, one for each star. We'd like to turn that into some *s on the screen. We do it in stages:
- Find the minimum and maximum x-coordinate so we know the range of x-coordinates. Same for y-coordinates. These will be xMin, yMin, xMax, yMax. (We could use z, instead of either x or y).
- Form the range, or span, of the x-coordinate, namely xMax - xMin (and same for the y_coordinates).
- Pick constants for the 80 x 80, namely, NUM_ROWS and NUM_COLS. This way we can produce any size screen shot we wish. In fact, it is best to make NUM_ROWS half what NUM_COLS is because of the aspect ratio of text on a screen. I have found that a 35 x 70 size looks very good.
- Form a matrix (or sparse matrix) called starMap. This object will be NUM_ROWS x NUM_COLS and have the default character ' ' (blank).
- In a loop, get the x-y coordinates and scale them using the mins, ranges and NUM_ROW/COLs calculated above so that each x value falls into the 0 - NUM_COLS and each y value falls into the 0 - NUM_ROWS range. These scaled values can be cast to int and used as coordinates for the starMap matrix. Place an '*' into any location generated by this means. (Also, you can place 'S' at location (0,0) -- before conversion, of course. Additionally, the stars are ranked using getRank() which produces a number from 1 (closest) to 100 (farthest). You can replace the '*' of those nearby stars with with ranks 1 - 9, and these should appear closer to the Sun than most of the '*'s. We can't do it for 10 and above unless you use letters, since this is an array of chars, not strings.
- Once things are working, you can plot the x-z and y-z maps. You can also switch the roles so that, for example, y is plotted in the columns and x in the rows.
This is not a required assignment - you can do it for extra credit and submit it as your bonus assignment. If you want to see a view of your galactic neighborhood that you will not find anywhere else, then you should spend a few hours on this one.
Here is an outline of the source, which can guide you in your efforts:
// Summary of Solution #include <iostream> using namespace std; #include "StarNearEarth.h" #include "FHsparseMat.h" #include <cmath> class SNE_Analyzer: public StarNearEarth { private: double x, y, z; public: void calcCartCoords(); double getX() { return x; } double getY() { return y; } double getZ() { return z; } string coordToString(); SNE_Analyzer & operator=( const StarNearEarth &sne ); }; void SNE_Analyzer::calcCartCoords() { // not shown } string SNE_Analyzer::coordToString() { // not shown } SNE_Analyzer & SNE_Analyzer::operator=( const StarNearEarth &sne ) { // done in modules } // --------------- main --------------- int main() { string outString, longBlankString = " " " "; int k, arraySize, row, col; double maxX, minX, maxY, minY, maxZ, minZ, xRange, yRange, zRange, xConversion, yConversion, zConversion; StarNearEarthReader starInput("nearest_stars.txt"); const int NUM_COLS = 70; const int NUM_ROWS = 35; if (starInput.readError()) { cout << "couldn't open " << starInput.getFileName() << " for input.\n"; exit(1); } cout << starInput.getFileName() << endl; cout << starInput.getNumStars() << endl; // create an array of objects for our own use: arraySize = starInput.getNumStars(); SNE_Analyzer *starArray = new SNE_Analyzer[arraySize]; for (k = 0; k < arraySize; k++) starArray[k] = starInput[k]; // display cartesian coords for (int k = 0; k < arraySize; k++) cout << starArray[k].getNameCommon() << " " << starArray[k].coordToString() << endl; // get max and min coords for scaling maxX = minX = maxY = minY = maxZ = minZ = 0; for (int k = 0; k < arraySize; k++) { // not shown } xRange = maxX - minX; yRange = maxY - minY; zRange = maxZ - minZ; xConversion = // not shown yConversion = // not shown SparseMat<char> starMap(NUM_ROWS, NUM_COLS, ' '); for (k = 0; k < arraySize; k++) { row = // not shown col = // not shown if ( /* not shown */ ) starMap.set(row, col) = // not shown -- nearby starts labeled '1' - '9' else starMap.set(row, col) = '*'; } row = // not shown col = // not shown starMap.set( row, col) = 'S'; for (row = 0; row < NUM_ROWS; row++) { outString = longBlankString.substr(0, NUM_COLS); // inner loop that builds outString not shown cout << outString << endl; } delete[] starArray; return 0; }
I will not give you the slightest hint of the run, because, despite the clunky character of console output, the feeling of seeing factual visual astronomical data from code you wrote appear on your console is an experience I don't want to dilute with a preview.
3B.5.3 Three-D Interaction
If you are facile with a graphics library for your OS, like .Net and Forms applications for Windows, you can implement this with graphs. And if you have a 3D library like OpenGL ... you have a real project for the next few weeks.
Wow.
Well, we have completed what is considered a three week review and prep for the course. After completing the third assignment, your brain will be completely tuned-up and ready to receive the knowledge and skills that comprise the bulk of the course and form a foundation for advanced computer science. We will begin next week with what might be the most interesting and useful of all the data structures, the tree.