簡體   English   中英

如何將 x 和 y 坐標的結果組織為 ASCII XY 圖?

[英]how to organize the result of x and y coordinates as an ASCII X-Y plot?

我正在用 C++ 做一個以前的作業,並試圖弄清楚如何獲得正確的 ASCII XY 圖。

所以真正發生的是給用戶一個菜單,允許他們選擇特定的觸發函數。所有函數的范圍在 X 的 [-4..6] 和 Y 的 [-12...5] 之間。用戶將被允許選擇刻度數量(或受限制的 x 和 y 范圍之間的值],如果他們想查看結果值或位圖。最終輸出將在值/位圖中。我已經粘貼了函數的 wolfram alpha 鏈接在評論中。

我所做的是通過 1/(graduation-1)(即 1/3,如果畢業值為 4)和列 # 的乘積增加 2D 輸出中的每一列。一旦該乘積達到 1,我就沒有得到正確的輸出值。

#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)的輸出。 TextPlot2d函數采用一個 lambda 或函子來計算所需的函數,以及兩個PlotRange結構體,用於指定 x 和 y 軸的縮放方式。 我還使用了不同的輸出字符,因為我很難區分“X”和“O”。

#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############

我終於得到了輸出!!顯然函數#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);




       }

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM