Advertisement
Guest User

LR3

a guest
Dec 12th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <graphics.h>
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. const double PI = 3.141592653589;
  7.  
  8.    double x_min = PI / 2;   // Setting up our unique values for x min and x max
  9.    double x_max = PI * 7;  
  10.    int SMOOTHNESS = 1000;  
  11.    double scale = 25;    
  12.  
  13. // returns y(x)
  14. double function(double x) {
  15.   return (sin(x) * sin(x) - cos(x) * cos(x));
  16. }
  17.  
  18. int main() {
  19.    int graph_driver = DETECT;
  20.    int graph_mode;
  21.    int graph_error_code;
  22.    initgraph( & graph_driver, & graph_mode, "C:\\TURBOC3\\BGI");
  23.    
  24.  
  25. // Exception processing
  26.     graph_error_code = graphresult();
  27.     if (graph_error_code != grOk) {
  28.         char * msgError = grapherrormsg(graph_error_code);
  29.         printf("%s\n", msgError);
  30.         return graph_error_code;
  31.    }
  32.  
  33. // Setting up the (0,0) on the screen
  34.    double centerCordX = getmaxx() / 20;              
  35.    double centerCordY = getmaxy() / 2;               
  36.    setviewport(centerCordX, centerCordY, getmaxx(), getmaxy(), 0);
  37. // Start position and step init  
  38.    double step = (x_max - x_min) / SMOOTHNESS;
  39.    double currentX = x_min, currentY = function(currentX);
  40.    double previousX = currentX, previousY = currentY;
  41.    
  42.    settextstyle(DEFAULT_FONT, VERT_DIR, 1);
  43.    line(0, -getmaxy(), 0, getmaxy());
  44.    line(-getmaxx(), 0, getmaxx(), 0);
  45.    double maxY = currentY;
  46.    while (currentX < x_max) {
  47.       currentX += step;
  48.       currentY = function(currentX);
  49.       maxY = currentY > maxY ? currentY : maxY;
  50.       line(previousX * scale, -previousY * scale, currentX * scale, -currentY * scale);
  51.      
  52.       if (fmod(currentX, 1) < step) {
  53.          char title[8];
  54.          sprintf(title, "%3.1f", currentX);
  55.          outtextxy(previousX * scale, 0, title);
  56.       }
  57.       previousX = currentX;
  58.       previousY = currentY;
  59.    }
  60.    printf("Max: %f\n", maxY);
  61.    getch();
  62.    closegraph();
  63.    return 0;
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement