如何将x和y坐标的结果组织为ASCII X-Y图?

时间:2016-10-13 22:26:12

标签: c++ math bitmap trigonometry gedit

我正在用C ++做一个以前的作业,并试图弄清楚如何获得正确的ASCII X-Y图。

所以真正发生的是给用户一个菜单,允许他们选择一个特定的trig函数。所有函数的范围在[-4..6]之间,对于X和[-12 ... 5], Y.Next用户将被允许选择渐变量(或受限制的x和y范围之间的值),如果他们想要查看结果值或Bitmap。最终输出将在值/位图中。我已粘贴wolfram alpha链接对于评论中的功能。

我所做的是在2D输出中每列增加1 /(gradu-1)的乘积(即,如果刻度值为4,则为1/3)和列#。一旦该产品达到1,我就没有得到正确的output代表值。

#include <iostream>
#include <cmath>


using namespace std;


double minX=-4;
double maxX=6;
double minY=-12;
double maxY=5;



 void displayValues(double result[],int result_size,int graduationVal,double percentIncrease,int menuSelection){

     int displaySelection=0;
     cerr<<"(0) Bitmap or (1) Values?";
     cin>>displaySelection;
     int k=0;
     int i=0;
     int division=0;

     //Add Values into array until result-1 values
     while(i < result_size){
       //cout<<"Before j";
       //Calculate all the horizontal axis values
for(int j=0; j< graduationVal;j++){

        if(displaySelection==1){

            //cout<<"In if";
            if(menuSelection==1){
                result[i]=sin(minX+(percentIncrease*j))*cos(minY+(percentIncrease*k));
                cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
                cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";
                //cout<<sin(minX+(percentIncrease*j))<<"\n";
                //cout<<cos(minY+(percentIncrease*k))<<"\n";
                cout<<result[i]<<" ";
            }else if(menuSelection==2){
                result[i]=sin(minX+(percentIncrease*j))+pow(cos(minY+(percentIncrease*j)),2)-(minX+(percentIncrease*j))/(minY+(percentIncrease*k));
                cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
                cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";

                cout<<result[i]<<" ";

            }else if(menuSelection==3){

                result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(0.5 *cos(minY+(percentIncrease*k)));
                cout<<"\n Increase in value of x by "<<percentIncrease*j<<" ";
                cout<<"Increase in value of y by "<<percentIncrease*k<<"\n";

                cout<<result[i]<<" ";


            }else if(menuSelection==4){

                result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(minX+(percentIncrease*j)) * cos(3 * (minY+(percentIncrease*k)));   
                cout<<result[i]<<" ";

            }

            //cout<<"J is"<<j<<"\n";
            //cout<<"K is"<<k<<"\n";
            //cout<<"I is"<<i<<"\n";
        }else{

            if(menuSelection==1){
                result[i]=sin(minX+(percentIncrease*j))*cos(minY+(percentIncrease*k));
                cout<<((result[i] > 0 )?"O":"X");

            }else if(menuSelection==2){

                result[i]=sin(minX+(percentIncrease*j))+pow(cos(minY+(percentIncrease*j)),2)-(minX+(percentIncrease*j))/(minY+(percentIncrease*k));
                cout<<((result[i] > 0 )?"O":"X");

            }else if(menuSelection==3){

                result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(0.5 *cos(minY+(percentIncrease*k)));
                cout<<((result[i] > 0 )?"O":"X");


            }else if(menuSelection==4){

                result[i]=(0.5 * sin(minX+(percentIncrease*j)))+(minX+(percentIncrease*j)) * cos(3 * (minY+(percentIncrease*k)));       
                cout<<((result[i] > 0 )?"O":"X");

               }


        }//End display choice if


        //Increment Array Index
        i++;
        //cout<<"Bottom of j";
    }//End of j loop

cout<<"\n";

//Increment y-values
if(k<graduationVal){

    k++;
   }

  }//End of While

}


int main(){

    int menuChoice=-1;
    int displaychoice=0;

    double distanceFromMinMaxX=4+6;
    double distanceFromMinMaxY=12+5;

    int graduations=0;

    // double precisionX;
    // double precisionY;

    double pctIncrease;

    while(menuChoice!=0){

      cerr<<"Select your function\n";
      cerr<<"1. sin(x)cos(y)\n";
      cerr<<"2. sin(x)+cos^2(x)-x/y\n";
      cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
      cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
      cerr<<"0. Quit\n";
      cin>>menuChoice;

      if(menuChoice == 0){

        return 0;
      }

      cerr<<"Number of graduations per axis: ";
      cin>>graduations;

      pctIncrease=1/(double)(graduations - 1);
      int values_size=graduations * graduations;

      double values[values_size];


      /*int yValues[graduationVal];*/

     // precisionX=distanceFromMinMaxX/graduations;
     // precisionY=distanceFromMinMaxY/graduations;


      displayValues(values,values_size,graduations,pctIncrease,menuChoice);


    }

 }

编辑:

    #include <iostream>
    #include <cmath>
    using namespace std;
    double minX=-4;
    double maxX=6;
    double minY=-12;
    double maxY=5;


double* calculateValues(double val[],int val_size,int graduationVal,double xPrecision,double yPrecision,int menuSelection){

  /*int displaySelection=0;
  cerr<<"(0) Bitmap or (1) Values?";
  cin>>displaySelection;*/

  int k=0;
  int i=0;

  //Add Values into array until result-1 values
  while(i < val_size){
    for(int j=0; j<graduationVal;j++){
                if(menuSelection==1){
                    val[i]=sin(minX+(xPrecision*j))*cos(minY+(yPrecision*k));

                }else if(menuSelection==2){
                    val[i]=sin(minX+(xPrecision*j))+pow(cos(minY+(xPrecision*j)),2)-(minX+(xPrecision*j))/(minY+(yPrecision*k));

                }else if(menuSelection==3){

                    val[i]=(0.5 * sin(minX+(xPrecision*j)))+(0.5 *cos(minY+(yPrecision*k)));

                }else if(menuSelection==4){

                    val[i]=(0.5 * sin(minX+(xPrecision*j)))+(minX+(xPrecision*j)) * cos(3 * (minY+(yPrecision*k))); 

                }       

            //Increment Array Index
            i++;
    }//End of j loop



    //Increment y-values
    if(k<graduationVal){

        k++;
    }

 }//End of While
return val;
}

void displayValues(double result[],int result_size,int numOfGraduations){

  int displaySelection=0;
  cerr<<"(0) Bitmap or (1) Values?";
  cin>>displaySelection;

  int k=0;
  int i=0;


  while(i< result_size){


    for(int j=0;j<numOfGraduations;j++){

       if(displaySelection==1){
        cout<<result[i]<<" ";
       }else{

        cout<<((result[i] > 0 )?"O":"X");

       }

        i++;
    }   

    cout<<"\n";

  }

}


int main(){

 int menuChoice=-1;
 int displaychoice=0;

 double distanceFromMinMaxX=4+6;
 double distanceFromMinMaxY=12+5;

 int graduations=0;

double precisionX;
double precisionY;

 double pctIncrease;

   while(menuChoice!=0){

          cerr<<"Select your function\n";
          cerr<<"1. sin(x)cos(y)\n";
          cerr<<"2. sin(x)+cos^2(x)-x/y\n";
          cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
          cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
          cerr<<"0. Quit\n";
          cin>>menuChoice;

          if(menuChoice == 0){

            return 0;
          }

          cerr<<"Number of graduations per axis: ";
          cin>>graduations;

          pctIncrease=1/(double)(graduations - 1);
          int values_size=graduations * graduations;

          double values[values_size];


          /*int yValues[graduationVal];*/

          precisionX=distanceFromMinMaxX/graduations;
          precisionY=distanceFromMinMaxY/graduations;

          /*cout << "# of graduations: " << graduations << endl;
          cout << "Precision: "<< endl;
          cout << "x: " << precisionX << endl;
          cout << "y: " << precisionY << endl;*/

          calculateValues(values,values_size,graduations,precisionX,precisionY,menuChoice);
          displayValues(values,values_size,graduations);




   }

}

编辑:我正在使用gcc

2 个答案:

答案 0 :(得分:0)

这会产生类似于您链接的screencap的输出(用户选择选项2)。 TextPlot2d函数采用lambda或functor计算所需的函数,以及两个PlotRange结构,用于指定x和y轴应如何缩放。我也使用不同的输出字符,因为我很难区分&#39; X&#39;与&#39; O&#39;。

#include <iostream>
#include <cmath>

struct PlotRange {
    double begin, end, count;
    double get_step() const { return (end - begin) / count; }
};

template <typename F>
void TextPlot2d(F func, PlotRange range_x, PlotRange range_y) {
    const auto step_x = range_x.get_step();
    const auto step_y = range_y.get_step();
    // multiply steps by iterated integer
    // to avoid accumulation of error in x and y
    for(int j = 0;; ++j) {
        auto y = range_y.begin + step_y * j;
        if(y >= range_y.end) break;
        for(int i = 0;; ++i) {
            auto x = range_x.begin + step_x * i;
            if(x >= range_x.end) break;
            auto z = func(x, y);
            if(z != z) { std::cout << '?'; } // NaN outputs a '?'
            else       { std::cout << (z < 0 ? '#':'o'); }
        }
        std::cout << '\n';
    }
}

int main() {
    TextPlot2d(
        [](double x, double y){ return std::sin(x) + std::cos(y/2)*std::cos(y/2) - x/y; },
        {-4.0, 6.0, 40.0},
        {-12.0, 5.0, 40.0}
    );
}

多田!

ooooooo######ooooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
ooooo#########oooooooooooooooooooooooooo
oooo###########ooooooooooooooooo######oo
ooo#############ooooooooooooooo#######oo
ooo#############ooooooooooooooo########o
oo##############ooooooooooooooo########o
ooo#############ooooooooooooooo########o
ooo#############oooooooooooooooo######oo
oooo###########oooooooooooooooooo####ooo
ooooo#########oooooooooooooooooooooooooo
ooooo#########oooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
ooooooo######ooooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
oooooo#######ooooooooooooooooooooooooooo
ooooo#########oooooooooooooooooooooooooo
ooo############ooooooooooooooooooooooooo
oo#############ooooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
################oooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
###############ooooooooooooooooooooooooo
################oooooooooooooooooooooooo
oooooooooooooooooo######################
oooooooooooooooooooooo##################
oooooooooooooooooooooooo################
ooooooooooooooooooooooooo###############
ooooooooooo###ooooooooooo###############
ooooooooo#######ooooooooo###############
oooooooo########oooooooooo##############
ooooooo#########oooooooooo##############
ooooooo#########ooooooooooo#############
ooooooo########oooooooooooo#############
oooooooo######oooooooooooooo############

答案 1 :(得分:0)

我终于得到了输出!!看起来功能#2错了,这就是我没有得到正确答案的原因。

    #include <iostream>
    #include <cmath>
    using namespace std;
    double minX=-4;
    double maxX=6;
    double minY=-12;
    double maxY=5;


    void calculateValues(double val[],int val_size,int graduationVal,double xPrecision,double yPrecision,int menuSelection){

      /*int displaySelection=0;
      cerr<<"(0) Bitmap or (1) Values?";
      cin>>displaySelection;*/

      //double val[];
      int k=0;
      int i=0;

      //Add Values into array until result-1 values
      while(i < val_size){


        for(int j=0; j<graduationVal;j++){
                    if(menuSelection==1){
                        val[i]=sin(minX+(xPrecision*j))*cos(minY+(yPrecision*k));

                    }else if(menuSelection==2){
                        val[i]=sin(minX+(xPrecision*j))+pow(cos((minY+(yPrecision*k))/2),2)-(minX+(xPrecision*j))/(minY+(yPrecision*k));

                    }else if(menuSelection==3){

                        val[i]=(0.5 * sin(minX+(xPrecision*j)))+(0.5 *cos(minY+(yPrecision*k)));

                    }else if(menuSelection==4){

                        val[i]=(0.5 * sin(minX+(xPrecision*j)))+(minX+(xPrecision*j)) * cos(3 * (minY+(yPrecision*k))); 

                    }       

                //Go to next value in array
                i++;
        }//End of j loop



        //Increment y-values
        if(k<graduationVal){

            k++;
        }

     }//End of While
      }

    void displayValues(double result[],int result_size,int numOfGraduations){

      int displaySelection=0;
      cerr<<"(0) Bitmap or (1) Values?";
      cin>>displaySelection;

      int k=0;
      int i=0;


      while(i< result_size){


        for(int j=0;j<numOfGraduations;j++){

           if(displaySelection==1){
            cout<<result[i]<<" ";
           }else{

            cout<<((result[i] > 0 )?"O":"#");

           }

            i++;
        }   

        cout<<"\n";

      }

    }


    int main(){

     int menuChoice=-1;
     int displaychoice=0;

     double distanceFromMinMaxX=maxX-minX;
     double distanceFromMinMaxY=maxY-minY;

     int graduations=0;

    double precisionX;
    double precisionY;

     double pctIncrease;

       while(menuChoice!=0){

              cerr<<"Select your function\n";
              cerr<<"1. sin(x)cos(y)\n";
              cerr<<"2. sin(x)+cos^2(y/2)-x/y\n";
              cerr<<"3. 1/2 sin(x) + 1/2 cos(y)\n";
              cerr<<"4. 1/2 sin(x) + xcos(3y)\n";
              cerr<<"0. Quit\n";
              cin>>menuChoice;

              if(menuChoice == 0){

                return 0;
              }

              cerr<<"Number of graduations per axis: ";
              cin>>graduations;

              //pctIncrease=1/(double)(graduations - 1);
              int values_size=graduations * graduations;

              double values[values_size];


              /*int yValues[graduationVal];*/

              precisionX=(distanceFromMinMaxX)/graduations;
              precisionY=(distanceFromMinMaxY)/graduations;

              /*cout << "# of graduations: " << graduations << endl;
              cout << "Precision: "<< endl;
              cout << "x: " << precisionX << endl;
              cout << "y: " << precisionY << endl;*/

              calculateValues(values,values_size,graduations,precisionX,precisionY,menuChoice);
              displayValues(values,values_size,graduations);




       }

    }