Section 5 - Stars On Your Console

sun pic

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:

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.