vadimk772336

Untitled

Dec 18th, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 24.95 KB | None | 0 0
  1.  
  2. #include<iostream>
  3. #include <SFML/Graphics.hpp>
  4. #include<SFML/Audio.hpp>
  5. #include <cmath>
  6. #include<string>
  7. #include <list>
  8. #include<math.h>
  9. using namespace sf;
  10. using namespace std;
  11. const double A = -0.9, B = 0.9;
  12.  
  13. class Expression
  14. { // Abstract class
  15. public:
  16.     virtual Expression* diff() = 0;
  17.     virtual void print() = 0;
  18.     virtual bool isZero() = 0; // Zero Check
  19.     virtual bool isConst() = 0; // Constant check(Number)
  20.     virtual float constValue() = 0; // Constant value(Number)
  21.     virtual double evaluate(double x) = 0;
  22. };
  23. Expression* ExpressionString(char *string);
  24. Expression* Simplify(Expression *ex);
  25.  
  26. class Number : public Expression
  27. { // Number
  28. private:
  29.     float num;
  30. public:
  31.     //float num;
  32.     Number() : num(0) {}
  33.     Number(float val) : num(val) { }
  34.     double evaluate(double x)
  35.     {
  36.         return num;
  37.     }
  38.     Expression* diff()
  39.     {
  40.         Expression *d_num = new Number(0);
  41.         return d_num;
  42.     }
  43.     void print() { cout << num; }
  44.     bool isZero() { return num == 0; }
  45.     bool isConst() { return true; }
  46.     float constValue() { return num; }
  47. };
  48.  
  49. class Variable : public Expression
  50. { // Variable
  51. public:
  52.     char var;
  53.     Variable() : var('x') { }
  54.     Variable(char ch) : var(ch) { }
  55.     double evaluate(double x)
  56.     {
  57.         return x;
  58.     }
  59.     Expression* diff()
  60.     {
  61.  
  62.  
  63.         Expression *d_var = new Number(1);
  64.         return d_var;
  65.  
  66.     }
  67.     void print() { cout << var; }
  68.     bool isZero() { return false; }
  69.     bool isConst() { return false; }
  70.     float constValue() { return 0; }
  71. };
  72.  
  73. class Add : public Expression
  74. { // The sum of two expressions
  75. protected:
  76.     //Expression *left, *right;
  77. public:
  78.     Expression *left, *right;
  79.     Add(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
  80.     Expression* diff()
  81.     {
  82.         Expression *ld = Simplify(left->diff());
  83.         Expression *rd = Simplify(right->diff());
  84.         Expression *result = Simplify(new Add(Simplify(ld), Simplify(rd)));
  85.         return result;
  86.     }
  87.     double evaluate(double x)
  88.     {
  89.         return left->evaluate(x) + right->evaluate(x);
  90.     }
  91.     void print()
  92.     {
  93.         cout << "(";
  94.         left->print();
  95.         cout << "+";
  96.         right->print();
  97.         cout << ")";
  98.     }
  99.     bool isZero() { return left->isZero() && right->isZero(); }
  100.     bool isConst() { return left->isConst() && right->isConst(); }
  101.     float constValue() { return left->constValue() + right->constValue(); }
  102. };
  103.  
  104. class Sub : public Expression
  105. { // The sub of two expressions
  106. public:
  107.     Expression *left, *right;
  108.     Sub(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
  109.     Expression* diff()
  110.     {
  111.         Expression *ld = Simplify(left->diff());
  112.         Expression *rd = Simplify(right->diff());
  113.         Expression *result = Simplify(new Sub(Simplify(ld), Simplify(rd)));
  114.         return result;
  115.     }
  116.     double evaluate(double x)
  117.     {
  118.         return left->evaluate(x) - right->evaluate(x);
  119.     }
  120.     void print()
  121.     {
  122.         cout << "(";
  123.         left->print();
  124.         cout << "-";
  125.         right->print();
  126.         cout << ")";
  127.     }
  128.     bool isZero() { return left->isZero() && right->isZero(); }
  129.     bool isConst() { return left->isConst() && right->isConst(); }
  130.     float constValue() { return left->constValue() - right->constValue(); }
  131. };
  132.  
  133. class Mul : public Expression
  134. { // Composition (ab)' = a'b + ab';
  135. public:
  136.     Expression *left, *right;
  137.     Mul(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
  138.     void print()
  139.     {
  140.         cout << "(";
  141.         left->print();
  142.         cout << "*";
  143.         right->print();
  144.         cout << ")";
  145.     }
  146.     double evaluate(double x)
  147.     {
  148.         return left->evaluate(x) * right->evaluate(x);
  149.     }
  150.     Expression* diff()
  151.     {
  152.         Expression *ld = Simplify(new Mul(left->diff(), right));
  153.         Expression *rd = Simplify(new Mul(left, right->diff()));
  154.         Expression *result = Simplify(new Add(ld, rd));
  155.         return result;
  156.     }
  157.     bool isZero() { return left->isZero() || right->isZero(); }
  158.     bool isConst() { return left->isConst() && right->isConst(); }
  159.     float constValue() { return left->constValue() * right->constValue(); }
  160. };
  161.  
  162. class Div : public Expression
  163. { // quotient (a/b)' = (a'b - ab')/b^2
  164. public:
  165.     Expression *left, *right;
  166.     Div(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
  167.     void print()
  168.     {
  169.         cout << "(";
  170.         left->print();
  171.         cout << "/";
  172.         right->print();
  173.         cout << ")";
  174.     }
  175.     double evaluate(double x)
  176.     {
  177.         return left->evaluate(x) / right->evaluate(x);
  178.     }
  179.     Expression *diff()
  180.     {
  181.         Expression *dl = Simplify(left->diff());
  182.         Expression *dr = Simplify(right->diff());
  183.         Expression *l = Simplify(new Mul(dl, right));
  184.         Expression *r = Simplify(new Mul(left, dr));
  185.         Expression *bottom = Simplify(new Mul(right, right));
  186.         Expression *result = new Div(Simplify(new Sub(l, r)), bottom);
  187.         return result;
  188.     }
  189.     bool isZero() { return left->isZero(); }
  190.     bool isConst() { return left->isConst() && (right->isConst() && !right->isZero()); }
  191.     float constValue() { return left->constValue() / right->constValue(); }
  192. };
  193.  
  194. class Pow : public Expression
  195. { //  (x^y)' = y*x^(y-1)
  196. public:
  197.     Expression *arg;
  198.     Expression *degree;
  199.     Pow(Expression *arg1, Expression *arg2) : arg(arg1), degree(arg2) { }
  200.     void print()
  201.     {
  202.         cout << "(";
  203.         arg->print();
  204.         cout << "^";
  205.         degree->print();
  206.         cout << ")";
  207.     }
  208.     double evaluate(double x)
  209.     {
  210.         return pow(arg->evaluate(x), degree->evaluate(x));
  211.     }
  212.     Expression *diff()
  213.     {
  214.         Expression *dosn = Simplify(arg->diff());
  215.         Expression *newdeg = Simplify(new Sub(degree, new Number(1)));
  216.         Expression *first = new Mul(Simplify(dosn), Simplify(new Number(degree->constValue())));
  217.         Expression *newpow = new Pow(Simplify(arg), Simplify(new Sub(degree, new Number(1))));
  218.         Expression *result = Simplify(new Mul(Simplify(first), Simplify(newpow)));
  219.         return result;
  220.     }
  221.     bool isZero() { return arg->isZero(); }
  222.     bool isConst() { return arg->isConst(); }
  223.     float constValue() { return pow(arg->constValue(), degree->constValue()); }
  224. };
  225.  
  226.  
  227.  
  228. Expression* Simplify(Expression *ex)
  229. { // A function that simplifies the appearance of an expression.
  230.     // NUMBER
  231.     if (ex->isConst())
  232.     { // For Number
  233.         return new Number(ex->constValue());
  234.     }
  235.     // MUL
  236.     if (typeid(*ex) == typeid(Mul))
  237.     {
  238.         Mul *exM = (Mul*)ex;          ///Как это дерьмо работает? Спросить Марка
  239.         Expression *exMl = Simplify(exM->left);
  240.         Expression *exMr = Simplify(exM->right);
  241.         if (typeid(*exMl) == typeid(Variable) && typeid(*exMr) == typeid(Variable))
  242.         { // x*x
  243.             Variable *v = (Variable*)exMl;
  244.             return new Pow(new Variable(v->var), new Number(2)); // x^2
  245.         }
  246.         if (exMr->isConst() && exMr->constValue() == 1)
  247.         { // x*1
  248.             return Simplify(exMl);
  249.         }
  250.         if (exMl->isConst() && exMl->constValue() == 1)
  251.         { // 1*x
  252.             return Simplify(exMr);
  253.         }
  254.         if (exMr->isZero())
  255.         { // x*0
  256.             return new Number(0);
  257.         }
  258.         if (exMl->isZero())
  259.         { // 0*x
  260.             return new Number(0);
  261.         }
  262.         if (exMl->isConst())
  263.         { // const*x
  264.             if (typeid(*exMr) == typeid(Mul))
  265.             { // const*(a*b)
  266.                 Expression *a = ((Mul*)exMr)->left;
  267.                 Expression *b = ((Mul*)exMr)->right;
  268.                 if (a->isConst())
  269.                 { // const*(const*b)
  270.                     return new Mul(Simplify(new Mul(exMl, a)), b);
  271.                 }
  272.                 if (b->isConst())
  273.                 { // const*(a*const)
  274.                     return new Mul(a, Simplify(new Mul(exMl, b)));
  275.                 }
  276.             }
  277.         }
  278.         if (exMr->isConst())
  279.         { // Similar to the top
  280.             if (typeid(*exMl) == typeid(Mul))
  281.             {
  282.                 Expression *a = ((Mul*)exMl)->left;
  283.                 Expression *b = ((Mul*)exMl)->right;
  284.                 if (a->isConst())
  285.                 {
  286.                     return new Mul(Simplify(new Mul(exMr, a)), b);
  287.                 }
  288.                 if (b->isConst())
  289.                 {
  290.                     return new Mul(a, Simplify(new Mul(exMr, b)));
  291.                 }
  292.             }
  293.         }
  294.     }
  295.     // ADD
  296.     if (typeid(*ex) == typeid(Add))
  297.     { //
  298.         Add *exA = (Add*)ex;
  299.         Expression *a1 = Simplify(exA->left); // a1 + b1
  300.         Expression *b1 = Simplify(exA->right);
  301.         if (typeid(*a1) == typeid(Variable) && typeid(*b1) == typeid(Variable))
  302.         { // x+x
  303.             Variable *v = (Variable*)a1;
  304.             return new Mul(new Number(2), new Variable(v->var));
  305.         }
  306.         if (a1->isZero())
  307.         { // 0+b1
  308.             return Simplify(b1);
  309.         }
  310.         if (b1->isZero())
  311.         { // a1+0
  312.             return Simplify(a1);
  313.         }
  314.     }
  315.     // DIV
  316.     if (typeid(*ex) == typeid(Div))
  317.     {
  318.         Div *exD = (Div*)ex;
  319.         Expression *exDl = Simplify(exD->left);
  320.         Expression *exDr = Simplify(exD->right);
  321.         if (exDr->isConst() && exDr->constValue() == 1)
  322.         { // x/1
  323.             return Simplify(exDl);
  324.         }
  325.         if (exDl->isZero())
  326.         { // 0/x
  327.             return new Number(0);
  328.         }
  329.     }
  330.     // POW
  331.     if (typeid(*ex) == typeid(Pow))
  332.     {
  333.         Pow *exP = (Pow*)ex;
  334.         Expression *exPl = Simplify(exP->arg);
  335.         Expression *exPr = Simplify(exP->degree);
  336.         if (exPr->isZero())
  337.         { // x^0
  338.             return new Number(1);
  339.         }
  340.         if (exPr->isConst() && exPr->constValue() == 1)
  341.         { // x^1
  342.             return Simplify(exPl);
  343.         }
  344.         if (exPr->isConst())
  345.         { // If the degree is a numeric expression
  346.             return new Pow(Simplify(exPl), new Number(exPr->constValue()));
  347.         }
  348.     }
  349.     // SUB
  350.     if (typeid(*ex) == typeid(Sub))
  351.     {
  352.         Sub *exS = (Sub*)ex;
  353.         Expression *exSl = Simplify(exS->left);
  354.         Expression *exSr = Simplify(exS->right);
  355.         if (exSl->isZero())
  356.         { // 0-x = -x
  357.             Expression *result = Simplify(new Mul(new Number(-1), Simplify(exSr)));
  358.             return result;
  359.         }
  360.         if (exSr->isZero())
  361.         { // x-0
  362.             return Simplify(exSl);
  363.         }
  364.     }
  365.     return ex;
  366. }
  367.  
  368. int SearchFirstSymbol(char *str, char ch)
  369. { // Find the index of the first character to turn on
  370.     int len = strlen(str);
  371.     for (int i = 0; i < len; i++)
  372.     {
  373.         if (str[i] == ch)
  374.             return i;
  375.     }
  376.     return -1;
  377. }
  378.  
  379. int SearchLastSymbol(char *str, char ch)
  380. { // Search index of inclusion of the first from the end of the character
  381.     int len = strlen(str);
  382.     for (int i = len - 1; i >= 0; i--)
  383.     {
  384.         if (str[i] == ch)
  385.             return i;
  386.     }
  387.     return -1;
  388. }
  389.  
  390. char* SubString(char *str, int from, int to)
  391. { // From "from" to "to" inclusive to
  392.     char *result = new char[to - from + 1];
  393.     for (int i = 0; i < to - from; i++)
  394.     {
  395.         result[i] = str[from + i];
  396.     }
  397.     result[to - from] = '\0';
  398.     return result;
  399. }
  400.  
  401. char* DeleteSpace(char *str)
  402. { // Remove spaces at line edges
  403.     char space = ' ';
  404.     int left = -1;
  405.     int lenght = (int)strlen(str);
  406.     int right = lenght;
  407.     // Left
  408.     for (int i = 0; i < lenght; i++)
  409.     {
  410.         if (str[i] != space)
  411.         {
  412.             break;
  413.         }
  414.         else
  415.             left = i;
  416.     }
  417.     for (int i = lenght - 1; i >= 0; i--)
  418.     {
  419.         if (str[i] == space)
  420.         {
  421.             right = i;
  422.         }
  423.         else
  424.             break;
  425.     }
  426.     return SubString(str, left + 1, right);
  427. }
  428.  
  429.  
  430.  
  431. int Max(int x, int y)
  432. {
  433.     if (x > y) return x;
  434.     else return y;
  435. }
  436.  
  437. bool isFloat(char *str)
  438. {
  439.     str = DeleteSpace(str);
  440.     int lenght = strlen(str);
  441.     int p = 0;
  442.     for (int i = 0; i < lenght; i++)
  443.     {
  444.         if (!((str[i] >= '0' && str[i] <= '9') || (str[i] == '.'))) p = 1;
  445.     }
  446.     if (p == 0) return true;
  447.     else return false;
  448. }
  449.  
  450.  
  451.  
  452.  
  453. Expression* GetBinaryFunc(char *left, char *func, char *right)
  454. {
  455.     if (func[0] == '+')
  456.     {
  457.         return new Add(ExpressionString(left), ExpressionString(right));
  458.     }
  459.     if (func[0] == '-')
  460.     {
  461.         return new Sub(ExpressionString(left), ExpressionString(right));
  462.     }
  463.     if (func[0] == '*')
  464.     {
  465.         return new Mul(ExpressionString(left), ExpressionString(right));
  466.     }
  467.     if (func[0] == '/')
  468.     {
  469.         return new Div(ExpressionString(left), ExpressionString(right));
  470.     }
  471.     if (func[0] == '^')
  472.     {
  473.         return new Pow(ExpressionString(left), ExpressionString(right));
  474.     }
  475. } //Экспр стринг
  476. Expression* SimpleExpression(char *str) //ГетБинарифанк
  477. { // if there are no brackets
  478.     // We are looking for a bin operation index + - * / ^
  479.     int index = Max(Max(Max(Max(SearchFirstSymbol(str, '+'), SearchFirstSymbol(str, '-')), SearchFirstSymbol(str, '*')), SearchFirstSymbol(str, '/')), SearchFirstSymbol(str, '^'));
  480.     // If not, then either a number or a variable
  481.     if (index == -1)
  482.     {
  483.         if (isFloat(str))
  484.         {
  485.             return new Number(stof(str));
  486.         }
  487.         else
  488.         { // .  x .
  489.             return new Variable(DeleteSpace(str)[0]);
  490.         }
  491.     }
  492.     else
  493.     { // Parse the left, the operation and the right side
  494.         char *left = SubString(str, 0, index);
  495.         char *right = SubString(str, index + 1, strlen(str));
  496.         GetBinaryFunc(left, SubString(str, index, index + 1), right);
  497.     }
  498. }//ГетБинарифанк
  499. Expression* ExpressionString(char *string)
  500. {
  501.     Expression *result; // Result
  502.     string = DeleteSpace(string); // We remove spaces on the sides
  503.     int lenght = strlen(string);
  504.     // Looking for external brackets
  505.     int l = SearchFirstSymbol(string, '(');
  506.     int r = SearchLastSymbol(string, ')');
  507.     // If there are no brackets
  508.     if (l == -1 && r == -1)
  509.     {
  510.         return Simplify(SimpleExpression(string));
  511.     }
  512.     // If there are brackets, consider the cases.
  513.     // Find paired brackets for external
  514.     int rp = -1, lp = -1;
  515.     int Counter = 0; // counter brackets
  516.     // We are looking for a pair for the left bracket
  517.     for (int i = 0; i < lenght; i++)
  518.     {
  519.         if (string[i] == '(')
  520.         {
  521.             Counter++;
  522.         }
  523.         if (string[i] == ')')
  524.         {
  525.             if (Counter == 1)
  526.             {
  527.                 lp = i;
  528.                 break;
  529.             }
  530.             Counter--;
  531.         }
  532.     }
  533.     Counter = 0;
  534.     for (int i = lenght - 1; i >= 0; i--)
  535.     {
  536.         if (string[i] == ')')
  537.         {
  538.             Counter++;
  539.         }
  540.         if (string[i] == '(')
  541.         {
  542.             if (Counter == 1)
  543.             {
  544.                 rp = i;
  545.                 break;
  546.             }
  547.             Counter--;
  548.         }
  549.     }
  550.     if (l == 0 && r == lenght - 1 && lp == r)
  551.     { // (.o.) one outer brackets
  552.         result = ExpressionString(SubString(string, 1, lenght - 1)); // Remove the external brackets and run the function again.
  553.     }
  554.     else
  555.         if (lp != r)
  556.         { // ().o.() two pairs of external brackets, between them there must be a binary operation
  557.             char *funcstr = SubString(string, lp + 1, rp); // pick up the substring where the operation should be
  558.             // We are looking for the binary operation index in the substring
  559.             int index = Max(Max(Max(Max(SearchFirstSymbol(funcstr, '+'), SearchFirstSymbol(funcstr, '-')), SearchFirstSymbol(funcstr, '*')), SearchFirstSymbol(funcstr, '/')), SearchFirstSymbol(funcstr, '^'));
  560.             index = index + lp + 1; // Get the index of the operation in the whole row
  561.             char *left = DeleteSpace(SubString(string, 0, index));
  562.             char *func = DeleteSpace(SubString(string, index, index + 1));
  563.             char *right = DeleteSpace(SubString(string, index + 1, lenght));
  564.             result = GetBinaryFunc(left, func, right);
  565.         }
  566.         else
  567.         {
  568.             if (r != lenght - 1)
  569.             { // ...()o.. or ()o..
  570.                 char *funcstr = SubString(string, lp + 1, lenght); // pick up the substring where the operation should be
  571.                 int index = Max(Max(Max(Max(SearchFirstSymbol(funcstr, '+'), SearchFirstSymbol(funcstr, '-')), SearchFirstSymbol(funcstr, '*')), SearchFirstSymbol(funcstr, '/')), SearchFirstSymbol(funcstr, '^'));
  572.                 index = index + lp + 1; // Get the index of the operation in the whole row
  573.                 char *left = DeleteSpace(SubString(string, 0, index));
  574.                 char *func = DeleteSpace(SubString(string, index, index + 1));
  575.                 char *right = DeleteSpace(SubString(string, index + 1, lenght));
  576.                 result = GetBinaryFunc(left, func, right);
  577.             }
  578.             else
  579.             { // ..o() or exp()
  580.                 char *funcstr = SubString(string, 0, l);
  581.                 int index = Max(Max(Max(Max(SearchFirstSymbol(funcstr, '+'), SearchFirstSymbol(funcstr, '-')), SearchFirstSymbol(funcstr, '*')), SearchFirstSymbol(funcstr, '/')), SearchFirstSymbol(funcstr, '^'));
  582.                 if (index == -1)
  583.                 { // then exp()
  584.                     char *arg = DeleteSpace(SubString(string, l + 1, r));
  585.  
  586.                 }
  587.                 else
  588.                 { // then ..o()
  589.                     char *left = DeleteSpace(SubString(string, 0, index));
  590.                     char *func = DeleteSpace(SubString(string, index, index + 1));
  591.                     char *right = DeleteSpace(SubString(string, index + 1, lenght));
  592.                     result = GetBinaryFunc(left, func, right);
  593.                 }
  594.             }
  595.         }
  596.     return Simplify(result);
  597. }  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604. //double func(double x) {
  605. // double y;
  606. // y = cos(6 * acos(x));
  607. // /*y = x*x*x*x*x+10*x*x-log(x);*/
  608. // return y;
  609. //}
  610. //
  611. //double der(double x) {
  612. // double y;
  613. // y = sin(6 * acos(x))*6*(1.0/sqrt(1-x*x));
  614. // /*y = 5*x*x*x*x+20*x-1.0/x;*/
  615. // return y;
  616. //}
  617.  
  618.  
  619.  
  620. void Razlinovka(RenderWindow &window, int W, int H, int X_ZERO, int Y_ZERO, int X1, double a1, double b1, Expression *e, Expression *de) {
  621.     Vertex line[6];
  622.  
  623.     double step = 20, arrow = 8, otstup = 1000;
  624.     //разлиновка :
  625.     double c;//
  626.     if (fabs(
  627.         a1) > fabs(b1)) c = a1;
  628.     else c = b1;
  629.     //горизонтали
  630.     if (c < 0) {
  631.         //горизонтали
  632.         for (int i = 0; i <= (otstup - c) / 10; i++) {
  633.             line[0] = Vertex(Vector2f(c - otstup, c - otstup + i * step));
  634.             line[1] = Vertex(Vector2f(otstup - c, c - otstup + i * step));
  635.             line[0].color = line[1].color = Color(200, 200, 200, 255);
  636.             window.draw(line, 2, Lines);
  637.         }
  638.         //вертикали
  639.         for (int i = 0; i <= (otstup - c) / 10; i++) {
  640.             line[0] = Vertex(Vector2f(c - otstup + i * step, c - otstup));
  641.             line[1] = Vertex(Vector2f(c - otstup + i * step, otstup - c));
  642.             line[0].color = line[1].color = Color(200, 200, 200, 255);
  643.             window.draw(line, 2, Lines);
  644.         }
  645.  
  646.  
  647.     }
  648.     else {
  649.         if (c > 0) {
  650.             //горизонтали
  651.             for (int i = 0; i <= (otstup + c) / 10; i++) {
  652.                 line[0] = Vertex(Vector2f(-otstup - c, -otstup - c + i * step));
  653.                 line[1] = Vertex(Vector2f(otstup + c, -otstup - c + i * step));
  654.                 line[0].color = line[1].color = Color(200, 200, 200, 255);
  655.                 window.draw(line, 2, Lines);
  656.             }
  657.             //вертикали
  658.             if (c > 0) {
  659.                 for (int i = 0; i <= (otstup + c) / 10; i++) {
  660.                     line[0] = Vertex(Vector2f(-otstup - c + i * step, -otstup - c));
  661.                     line[1] = Vertex(Vector2f(-otstup - c +i * step, otstup + c));
  662.                     line[0].color = line[1].color = Color(200, 200, 200, 255);
  663.                     window.draw(line, 2, Lines);
  664.                 }
  665.  
  666.             }
  667.  
  668.         }
  669.  
  670.         else
  671.         {
  672.             //горизонтали
  673.             for (int i = 0; i <= W / 10; i++) {
  674.                 line[0] = Vertex(Vector2f(-W, -W + i * step));
  675.                 line[1] = Vertex(Vector2f(W, -W + i * step));
  676.                 line[0].color = line[1].color = Color(200, 200, 200, 255);
  677.                 window.draw(line, 2, Lines);
  678.             }
  679.  
  680.             //вертикали
  681.             for (int i = 0; i <= W / 10; i++) {
  682.                 line[0] = Vertex(Vector2f(-W + i * step, -W));
  683.                 line[1] = Vertex(Vector2f(-W + i * step, W));
  684.                 line[0].color = line[1].color = Color(200, 200, 200, 255);
  685.                 window.draw(line, 2, Lines);
  686.             }
  687.         }
  688.  
  689.  
  690.     }
  691.  
  692.  
  693.     // Рисуем оси координат
  694.     if (a1 < 0) {
  695.         line[0] = sf::Vertex(sf::Vector2f(a1 - 1000, Y_ZERO));
  696.     }
  697.     else line[0] = sf::Vertex(sf::Vector2f(0, Y_ZERO));
  698.  
  699.     if (a1 > W) {
  700.         line[1] = sf::Vertex(sf::Vector2f(a1 + 1000, Y_ZERO));
  701.     }
  702.     else line[1] = sf::Vertex(sf::Vector2f(W, Y_ZERO));
  703.     if (b1 < 0) {
  704.         line[2] = sf::Vertex(sf::Vector2f(X_ZERO, b1 - 1000));
  705.     }
  706.     else line[2] = sf::Vertex(sf::Vector2f(X_ZERO, 0));
  707.     if (b1 > H) {
  708.         line[3] = sf::Vertex(sf::Vector2f(X_ZERO, b1 + 1000));
  709.     }
  710.     else line[3] = sf::Vertex(sf::Vector2f(X_ZERO, H));
  711.     line[0].color = line[1].color = line[2].color = line[3].color =
  712.         sf::Color::Black;
  713.     window.draw(line, 4, sf::Lines);
  714.  
  715.  
  716.     // стрелки для осей OX
  717.     line[0] = sf::Vertex(sf::Vector2f(W - arrow, Y_ZERO + arrow));
  718.     line[1] = sf::Vertex(sf::Vector2f(W, Y_ZERO));
  719.     line[2] = sf::Vertex(sf::Vector2f(W - arrow, Y_ZERO - arrow));
  720.     // Для оси OY
  721.     line[3] = sf::Vertex(sf::Vector2f(X_ZERO - arrow, arrow));
  722.     line[4] = sf::Vertex(sf::Vector2f(X_ZERO, 0));
  723.     line[5] = sf::Vertex(sf::Vector2f(X_ZERO + arrow, arrow));
  724.     line[0].color = line[1].color = line[2].color = sf::Color::Black;
  725.     line[3].color = line[4].color = line[5].color = sf::Color::Black;
  726.     window.draw(line, 6, sf::Triangles);
  727.  
  728.  
  729.  
  730.  
  731.     Vertex line3[4];
  732.  
  733.     //единичные отрезки
  734.     line3[0] = Vertex(Vector2f(X_ZERO + X1, Y_ZERO + 5));
  735.     line3[1] = Vertex(Vector2f(X_ZERO + X1, Y_ZERO - 5));
  736.     line3[2] = Vertex(Vector2f(X_ZERO + 5, Y_ZERO - X1));
  737.     line3[3] = Vertex(Vector2f(X_ZERO - 5, Y_ZERO - X1));
  738.     line3[0].color = line3[1].color = line3[2].color = line3[3].color = Color::Black;
  739.     window.draw(line3, 4, Lines);
  740.     //рисуем границы рассматриваемого отрезка
  741.     if (b1 < 0) {
  742.         line[0] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, b1 - otstup));
  743.         line[1] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, otstup - b1));
  744.         line[2] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, b1 - otstup));
  745.         line[3] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, otstup - b1));
  746.     }
  747.     else {
  748.         line[0] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, -b1 - otstup));
  749.         line[1] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, b1 + otstup));
  750.  
  751.         line[2] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, -b1 - otstup));
  752.         line[3] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, b1 + otstup));
  753.     }
  754.     line[0].color = line[1].color = line[2].color = line[3].color =
  755.         sf::Color::Green;
  756.     window.draw(line, 4, sf::Lines);
  757.  
  758.  
  759.     //функция
  760.     VertexArray line2(PrimitiveType::LinesStrip, W);
  761.     for (double x = -X_ZERO; x < W; x += 0.01) {
  762.  
  763.         line2.append(Vertex(Vector2f(X1*x + X_ZERO, Y_ZERO - X1 * /*func(x)*/e->evaluate(x)), Color::Black));
  764.  
  765.     }
  766.     window.draw(line2);
  767.  
  768.     //производная функции
  769.     VertexArray line4(PrimitiveType::LinesStrip, W);
  770.    
  771.     for (double x = -X_ZERO; x < W; x += 0.01) {
  772.  
  773.         line4.append(Vertex(Vector2f(X1*x + X_ZERO, Y_ZERO - X1 */*der(x)*/de->evaluate(x)), Color::Red));
  774.  
  775.     }
  776.     window.draw(line4);
  777.  
  778.  
  779.  
  780.  
  781. }
  782.  
  783.  
  784.  
  785.  
  786.  
  787. double DEL(double a, double b, int N, double E, Expression *de) {
  788.     double c, N1 = 0, x;
  789.  
  790.     while (fabs(b - a) >= E && N1 <= N) {
  791.         c = (a + b) / 2;
  792.         if (/*der(a)*der(c) */de->evaluate(a)*de->evaluate(c) < 0) {
  793.             b = c;
  794.         }
  795.         if (/*der(c)*der(b)*/de->evaluate(c)*de->evaluate(b) < 0) {
  796.             a = c;
  797.         }
  798.  
  799.     }
  800.     return (a + b) / 2;
  801. }
  802.  
  803. int main()
  804. {
  805.     Music music;//создаем объект музыки
  806.     music.openFromFile("1.ogg");//загружаем файл
  807.  
  808.     int W = 1080;
  809.     int H = 720;
  810.         int X1 = 20;
  811.     int W1 = 2000;
  812.     int H1 = 2000;
  813.     int Y_ZERO = H1 / 2, X_ZERO = W1 / 2;
  814.     int k = 0;//переменная для мерцания
  815.     RenderWindow window(VideoMode(W, H), "SMFL");
  816.  
  817.     window.clear(Color::White);
  818.     char *s;
  819.     //string s;
  820.     //cin >> s;
  821.     //s = "df";
  822.     Expression *test = ExpressionString("exp((2*x))");
  823.     Expression *de = e->diff();
  824.  
  825.  
  826.     double a, b;
  827.     double A1 = A;
  828.     double i = A;
  829.     double step = (B - A) / 100.0;
  830.     //поиск минимумов и максимумов функции
  831.     while (k == 0) {
  832.         k++;
  833.         while (/*der(A1)*der(i)*/de->evaluate(A1)*de->evaluate(i) > 0 && i < B) {
  834.             i += step;
  835.         }
  836.         if (/*der(i) == 0*/de->evaluate(i) == 0) {
  837.             a = i;
  838.             b = /*func(a)*/e->evaluate(a);
  839.  
  840.         }
  841.         else {
  842.             if (i >= B) {
  843.                 a = (A1 + B) / 2;
  844.                 b = /*func(a);*/e->evaluate(a);
  845.  
  846.             }
  847.             else {
  848.                 a = DEL(A1, i, 20000, 1.0e-5, de);
  849.                 b = /*func(a);*/e->evaluate(a);
  850.                 if (isnan(b))
  851.                 {
  852.                     k = 0;
  853.                     A1 = a;
  854.                     i += 1;
  855.                 }
  856.             }
  857.         }
  858.  
  859.     }
  860.  
  861.  
  862.  
  863.     k = 0;
  864.  
  865.  
  866.  
  867.  
  868.     //камера
  869.     View view(Vector2f(X_ZERO + X1 * a, Y_ZERO - X1 * b), Vector2f(300.f, 200.f));
  870.  
  871.     window.setView(view);
  872.  
  873.  
  874.  
  875.     /*Font font;
  876.     font.loadFromFile("18245.ttf");
  877.     Text text("", font, 20);
  878.     text.setFillColor(Color::Black);
  879.  
  880.     ostringstream playerScoreString;*/
  881.  
  882.  
  883.     music.play();
  884.     // Главный цикл приложения. Выполняется, пока открыто окно
  885.     while (window.isOpen())
  886.     {
  887.         //Vector2i pixelPos = Mouse::getPosition(window);
  888.         //Vector2f pos = window.mapPixelToCoords(pixelPos);
  889.         //cout << pixelPos.x << " ";//смотрим на координату Х позиции курсора в консоли (она не будет больше ширины окна)
  890.         //cout << pos.x << "\n";
  891.  
  892.  
  893.  
  894.  
  895.         // Обрабатываем очередь событий в цикле
  896.         Event event;
  897.         while (window.pollEvent(event))
  898.         {
  899.  
  900.             // Пользователь нажал на <<крестик>> и хочет закрыть окно?
  901.             if (event.type == Event::Closed)
  902.                 // Тогда закрываем его
  903.                 window.close();
  904.         }
  905.         if (Keyboard::isKeyPressed(Keyboard::Left)) {
  906.             view.move(-100, 0); window.setView(view); k = 0;
  907.         } //первая координата Х отрицательна =>идём влево
  908.         if (Keyboard::isKeyPressed(Keyboard::Right))
  909.         {
  910.             view.move(100, 0); window.setView(view); k = 0;
  911.         } //первая координата Х положительна =>идём вправо
  912.         if (Keyboard::isKeyPressed(Keyboard::Up)) {
  913.             view.move(0, -100); window.setView(view); k = 0;
  914.         } //вторая координата (У) отрицательна =>идём вверх (вспоминаем из предыдущих уроков почему именно вверх, а не вниз)
  915.         if (Keyboard::isKeyPressed(Keyboard::Down)) {
  916.             view.move(0, 100); window.setView(view); k = 0;
  917.         }
  918.         if (Mouse::isButtonPressed(Mouse::Left)) {
  919.             view.zoom(0.5); window.setView(view); k = 0;
  920.         }
  921.         if (Mouse::isButtonPressed(Mouse::Right)) {
  922.             view.zoom(2); window.setView(view); k = 0;
  923.         }
  924.  
  925.  
  926.         //window.clear(Color::White);
  927.         //i = 0;
  928.         //playerScoreString << pos.x;
  929.         //text.setString(playerScoreString.str());//задаем строку тексту и вызываем сформированную выше строку методом .str()
  930.         //text.setPosition(view.getCenter().x + 150, view.getCenter().y - 200);//задаем позицию текста, отступая от центра камеры
  931.         //if (i == 0) {
  932.         // window.draw(text);
  933.         // i++;
  934.         //}
  935.         /*отрисовка окна*/
  936.         if (k == 0) {
  937.             window.clear(Color::White);
  938.             Razlinovka(window, W1, H1, X_ZERO, Y_ZERO, X1, X_ZERO + X1 * a, Y_ZERO - X1 * b, e, de);
  939.             window.display();
  940.  
  941.         }
  942.         k++;
  943.  
  944.  
  945.  
  946.  
  947.  
  948.     }
  949.  
  950.  
  951.     return 0;
  952. }
Advertisement
Add Comment
Please, Sign In to add comment