newplot.cpp

来自「数据结构与程序设计教材源码 数据结构与程序设计教材源码」· C++ 代码 · 共 141 行

CPP
141
字号
 
#include <graphics.h>
 
int Plot::get_print_col(double x_value)
{
   double interp_x = ((double) max_col) * (x_value - x_low) / (x_high - x_low) + 0.5;
   int answer = (int) interp_x;
   if (answer < 0 || answer > max_col) answer = -1;
   return answer;
}
 
int Plot::get_print_row(double y_value)
/* 
 
Post:
Returns the row of the screen at which a point with
y-co-ordinate y_value should be plotted, or
returns -1 if the point would fall off the edge of
the screen.
 
*/

{
   double interp_y = ((double) max_row) * (y_value - y_low) / (y_high - y_low) + 0.5;
   int answer = (int) interp_y;
   if (answer < 0 || answer > max_row) answer = -1;
   return answer;
}
 
Error_code Plot::set_limits()
{
   double x, y;
   double new_increment = x_increment,
      new_x_low = x_low,
         new_x_high = x_high,
            new_y_low = y_low,
               new_y_high = y_high;

   cout << "Give a new value for x increment. Previous value = " << x_increment;
   cout << "\n Enter a value or a new line to keep the old value: " << flush;
   if (read_num(x) == success) new_increment = x;

   cout << "Give a new value for lower x limit. Previous value = " << x_low;
   cout << "\n Enter a value or a new line to keep the old value: " << flush;
   if (read_num(x) == success) new_x_low = x;

   cout << "Give a new value for upper x limit. Previous value = " << x_high;
   cout << "\n Enter a value or a new line to keep the old value: " << flush;
   if (read_num(x) == success) new_x_high = x;

   cout << "Give a new value for lower y limit. Previous value = " << y_low;
   cout << "\n Enter a value or a new line to keep the old value: " << flush;
   if (read_num(y) == success) new_y_low = y;

   cout << "Give a new value for upper y limit. Previous value = " << y_high;
   cout << "\n Enter a value or a new line to keep the old value: " << flush;
   if (read_num(y) == success) new_y_high = y;
   if (new_increment <= 0 || new_x_high <= new_x_low || new_y_high <= new_y_low)
      return fail;

   x_increment = new_increment;
   x_low = new_x_low;
   x_high = new_x_high;
   y_low = new_y_low;
   y_high = new_y_high;
   return success;
}
 
void Plot::clear()
{
   points.clear();
}
 
void Plot::find_points(Expression &postfix)
/* 
 
Post:
The Expression postfix is evaluated for values of
x that range from x_low to x_high in steps of
x_increment.  For each evaluation we add a Point 
of the corresponding data point to the Sortable_list points.
Uses:
Classes Token, Expression, Point, and List and the library <graphics.h>.
 
*/

{
   int graphicdriver = DETECT, graphicmode;
   initgraph(&graphicdriver, &graphicmode,"");   //  screen detection and
   graphresult();                                //  initialization
   max_col = getmaxx();                          //  with graphics.h library
   max_row = getmaxy();

   double x_val = x_low;
   double y_val;
   while (x_val <= x_high) {
      Token::set_x(x_val);
      postfix.evaluate_postfix(y_val);
      postfix.rewind();
      Point p(get_print_row(y_val), get_print_col(x_val));
      points.insert(0, p);
      x_val += x_increment;
   }
}
 
void Plot::draw()
/* 
 
Post:
All pixels represented in points have
been marked to produce a picture of the stored graph.
Uses:
Classes Point and Sortable_list and the library <graphics.h>
 
*/

{
   for (int i = 0; i < points.size(); i++) {
      Point q;
      points.retrieve(i, q);
 
      if (q.row < 0 || q.col < 0) continue;  //  off the scale of the graph
      if (q.row > max_row || q.col > max_col) continue;
      putpixel(q.col,q.row,3);               // graphics.h library function
   }

   cout "Enter a character to clear screen and continue: " << flush;
   char wait_for;
   cin >> wait_for;
   restorecrtmode();                         // graphics.h library function
}
 
Plot::Plot()
{
   x_low = -10;
   x_high = 10;
   y_low = -1000;
   y_high = 1000;
   x_increment = 0.01;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?