Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include <SFML/Graphics.hpp>
- #include<SFML/Audio.hpp>
- #include <cmath>
- #include<string>
- #include <list>
- #include<math.h>
- using namespace sf;
- using namespace std;
- const double A = -0.9, B = 0.9;
- class Expression
- { // Abstract class
- public:
- virtual Expression* diff() = 0;
- virtual void print() = 0;
- virtual bool isZero() = 0; // Zero Check
- virtual bool isConst() = 0; // Constant check(Number)
- virtual float constValue() = 0; // Constant value(Number)
- virtual double evaluate(double x) = 0;
- };
- Expression* ExpressionString(char *string);
- Expression* Simplify(Expression *ex);
- class Number : public Expression
- { // Number
- private:
- float num;
- public:
- //float num;
- Number() : num(0) {}
- Number(float val) : num(val) { }
- double evaluate(double x)
- {
- return num;
- }
- Expression* diff()
- {
- Expression *d_num = new Number(0);
- return d_num;
- }
- void print() { cout << num; }
- bool isZero() { return num == 0; }
- bool isConst() { return true; }
- float constValue() { return num; }
- };
- class Variable : public Expression
- { // Variable
- public:
- char var;
- Variable() : var('x') { }
- Variable(char ch) : var(ch) { }
- double evaluate(double x)
- {
- return x;
- }
- Expression* diff()
- {
- Expression *d_var = new Number(1);
- return d_var;
- }
- void print() { cout << var; }
- bool isZero() { return false; }
- bool isConst() { return false; }
- float constValue() { return 0; }
- };
- class Add : public Expression
- { // The sum of two expressions
- protected:
- //Expression *left, *right;
- public:
- Expression *left, *right;
- Add(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
- Expression* diff()
- {
- Expression *ld = Simplify(left->diff());
- Expression *rd = Simplify(right->diff());
- Expression *result = Simplify(new Add(Simplify(ld), Simplify(rd)));
- return result;
- }
- double evaluate(double x)
- {
- return left->evaluate(x) + right->evaluate(x);
- }
- void print()
- {
- cout << "(";
- left->print();
- cout << "+";
- right->print();
- cout << ")";
- }
- bool isZero() { return left->isZero() && right->isZero(); }
- bool isConst() { return left->isConst() && right->isConst(); }
- float constValue() { return left->constValue() + right->constValue(); }
- };
- class Sub : public Expression
- { // The sub of two expressions
- public:
- Expression *left, *right;
- Sub(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
- Expression* diff()
- {
- Expression *ld = Simplify(left->diff());
- Expression *rd = Simplify(right->diff());
- Expression *result = Simplify(new Sub(Simplify(ld), Simplify(rd)));
- return result;
- }
- double evaluate(double x)
- {
- return left->evaluate(x) - right->evaluate(x);
- }
- void print()
- {
- cout << "(";
- left->print();
- cout << "-";
- right->print();
- cout << ")";
- }
- bool isZero() { return left->isZero() && right->isZero(); }
- bool isConst() { return left->isConst() && right->isConst(); }
- float constValue() { return left->constValue() - right->constValue(); }
- };
- class Mul : public Expression
- { // Composition (ab)' = a'b + ab';
- public:
- Expression *left, *right;
- Mul(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
- void print()
- {
- cout << "(";
- left->print();
- cout << "*";
- right->print();
- cout << ")";
- }
- double evaluate(double x)
- {
- return left->evaluate(x) * right->evaluate(x);
- }
- Expression* diff()
- {
- Expression *ld = Simplify(new Mul(left->diff(), right));
- Expression *rd = Simplify(new Mul(left, right->diff()));
- Expression *result = Simplify(new Add(ld, rd));
- return result;
- }
- bool isZero() { return left->isZero() || right->isZero(); }
- bool isConst() { return left->isConst() && right->isConst(); }
- float constValue() { return left->constValue() * right->constValue(); }
- };
- class Div : public Expression
- { // quotient (a/b)' = (a'b - ab')/b^2
- public:
- Expression *left, *right;
- Div(Expression *arg1, Expression *arg2) : left(arg1), right(arg2) { }
- void print()
- {
- cout << "(";
- left->print();
- cout << "/";
- right->print();
- cout << ")";
- }
- double evaluate(double x)
- {
- return left->evaluate(x) / right->evaluate(x);
- }
- Expression *diff()
- {
- Expression *dl = Simplify(left->diff());
- Expression *dr = Simplify(right->diff());
- Expression *l = Simplify(new Mul(dl, right));
- Expression *r = Simplify(new Mul(left, dr));
- Expression *bottom = Simplify(new Mul(right, right));
- Expression *result = new Div(Simplify(new Sub(l, r)), bottom);
- return result;
- }
- bool isZero() { return left->isZero(); }
- bool isConst() { return left->isConst() && (right->isConst() && !right->isZero()); }
- float constValue() { return left->constValue() / right->constValue(); }
- };
- class Pow : public Expression
- { // (x^y)' = y*x^(y-1)
- public:
- Expression *arg;
- Expression *degree;
- Pow(Expression *arg1, Expression *arg2) : arg(arg1), degree(arg2) { }
- void print()
- {
- cout << "(";
- arg->print();
- cout << "^";
- degree->print();
- cout << ")";
- }
- double evaluate(double x)
- {
- return pow(arg->evaluate(x), degree->evaluate(x));
- }
- Expression *diff()
- {
- Expression *dosn = Simplify(arg->diff());
- Expression *newdeg = Simplify(new Sub(degree, new Number(1)));
- Expression *first = new Mul(Simplify(dosn), Simplify(new Number(degree->constValue())));
- Expression *newpow = new Pow(Simplify(arg), Simplify(new Sub(degree, new Number(1))));
- Expression *result = Simplify(new Mul(Simplify(first), Simplify(newpow)));
- return result;
- }
- bool isZero() { return arg->isZero(); }
- bool isConst() { return arg->isConst(); }
- float constValue() { return pow(arg->constValue(), degree->constValue()); }
- };
- Expression* Simplify(Expression *ex)
- { // A function that simplifies the appearance of an expression.
- // NUMBER
- if (ex->isConst())
- { // For Number
- return new Number(ex->constValue());
- }
- // MUL
- if (typeid(*ex) == typeid(Mul))
- {
- Mul *exM = (Mul*)ex; ///Как это дерьмо работает? Спросить Марка
- Expression *exMl = Simplify(exM->left);
- Expression *exMr = Simplify(exM->right);
- if (typeid(*exMl) == typeid(Variable) && typeid(*exMr) == typeid(Variable))
- { // x*x
- Variable *v = (Variable*)exMl;
- return new Pow(new Variable(v->var), new Number(2)); // x^2
- }
- if (exMr->isConst() && exMr->constValue() == 1)
- { // x*1
- return Simplify(exMl);
- }
- if (exMl->isConst() && exMl->constValue() == 1)
- { // 1*x
- return Simplify(exMr);
- }
- if (exMr->isZero())
- { // x*0
- return new Number(0);
- }
- if (exMl->isZero())
- { // 0*x
- return new Number(0);
- }
- if (exMl->isConst())
- { // const*x
- if (typeid(*exMr) == typeid(Mul))
- { // const*(a*b)
- Expression *a = ((Mul*)exMr)->left;
- Expression *b = ((Mul*)exMr)->right;
- if (a->isConst())
- { // const*(const*b)
- return new Mul(Simplify(new Mul(exMl, a)), b);
- }
- if (b->isConst())
- { // const*(a*const)
- return new Mul(a, Simplify(new Mul(exMl, b)));
- }
- }
- }
- if (exMr->isConst())
- { // Similar to the top
- if (typeid(*exMl) == typeid(Mul))
- {
- Expression *a = ((Mul*)exMl)->left;
- Expression *b = ((Mul*)exMl)->right;
- if (a->isConst())
- {
- return new Mul(Simplify(new Mul(exMr, a)), b);
- }
- if (b->isConst())
- {
- return new Mul(a, Simplify(new Mul(exMr, b)));
- }
- }
- }
- }
- // ADD
- if (typeid(*ex) == typeid(Add))
- { //
- Add *exA = (Add*)ex;
- Expression *a1 = Simplify(exA->left); // a1 + b1
- Expression *b1 = Simplify(exA->right);
- if (typeid(*a1) == typeid(Variable) && typeid(*b1) == typeid(Variable))
- { // x+x
- Variable *v = (Variable*)a1;
- return new Mul(new Number(2), new Variable(v->var));
- }
- if (a1->isZero())
- { // 0+b1
- return Simplify(b1);
- }
- if (b1->isZero())
- { // a1+0
- return Simplify(a1);
- }
- }
- // DIV
- if (typeid(*ex) == typeid(Div))
- {
- Div *exD = (Div*)ex;
- Expression *exDl = Simplify(exD->left);
- Expression *exDr = Simplify(exD->right);
- if (exDr->isConst() && exDr->constValue() == 1)
- { // x/1
- return Simplify(exDl);
- }
- if (exDl->isZero())
- { // 0/x
- return new Number(0);
- }
- }
- // POW
- if (typeid(*ex) == typeid(Pow))
- {
- Pow *exP = (Pow*)ex;
- Expression *exPl = Simplify(exP->arg);
- Expression *exPr = Simplify(exP->degree);
- if (exPr->isZero())
- { // x^0
- return new Number(1);
- }
- if (exPr->isConst() && exPr->constValue() == 1)
- { // x^1
- return Simplify(exPl);
- }
- if (exPr->isConst())
- { // If the degree is a numeric expression
- return new Pow(Simplify(exPl), new Number(exPr->constValue()));
- }
- }
- // SUB
- if (typeid(*ex) == typeid(Sub))
- {
- Sub *exS = (Sub*)ex;
- Expression *exSl = Simplify(exS->left);
- Expression *exSr = Simplify(exS->right);
- if (exSl->isZero())
- { // 0-x = -x
- Expression *result = Simplify(new Mul(new Number(-1), Simplify(exSr)));
- return result;
- }
- if (exSr->isZero())
- { // x-0
- return Simplify(exSl);
- }
- }
- return ex;
- }
- int SearchFirstSymbol(char *str, char ch)
- { // Find the index of the first character to turn on
- int len = strlen(str);
- for (int i = 0; i < len; i++)
- {
- if (str[i] == ch)
- return i;
- }
- return -1;
- }
- int SearchLastSymbol(char *str, char ch)
- { // Search index of inclusion of the first from the end of the character
- int len = strlen(str);
- for (int i = len - 1; i >= 0; i--)
- {
- if (str[i] == ch)
- return i;
- }
- return -1;
- }
- char* SubString(char *str, int from, int to)
- { // From "from" to "to" inclusive to
- char *result = new char[to - from + 1];
- for (int i = 0; i < to - from; i++)
- {
- result[i] = str[from + i];
- }
- result[to - from] = '\0';
- return result;
- }
- char* DeleteSpace(char *str)
- { // Remove spaces at line edges
- char space = ' ';
- int left = -1;
- int lenght = (int)strlen(str);
- int right = lenght;
- // Left
- for (int i = 0; i < lenght; i++)
- {
- if (str[i] != space)
- {
- break;
- }
- else
- left = i;
- }
- for (int i = lenght - 1; i >= 0; i--)
- {
- if (str[i] == space)
- {
- right = i;
- }
- else
- break;
- }
- return SubString(str, left + 1, right);
- }
- int Max(int x, int y)
- {
- if (x > y) return x;
- else return y;
- }
- bool isFloat(char *str)
- {
- str = DeleteSpace(str);
- int lenght = strlen(str);
- int p = 0;
- for (int i = 0; i < lenght; i++)
- {
- if (!((str[i] >= '0' && str[i] <= '9') || (str[i] == '.'))) p = 1;
- }
- if (p == 0) return true;
- else return false;
- }
- Expression* GetBinaryFunc(char *left, char *func, char *right)
- {
- if (func[0] == '+')
- {
- return new Add(ExpressionString(left), ExpressionString(right));
- }
- if (func[0] == '-')
- {
- return new Sub(ExpressionString(left), ExpressionString(right));
- }
- if (func[0] == '*')
- {
- return new Mul(ExpressionString(left), ExpressionString(right));
- }
- if (func[0] == '/')
- {
- return new Div(ExpressionString(left), ExpressionString(right));
- }
- if (func[0] == '^')
- {
- return new Pow(ExpressionString(left), ExpressionString(right));
- }
- } //Экспр стринг
- Expression* SimpleExpression(char *str) //ГетБинарифанк
- { // if there are no brackets
- // We are looking for a bin operation index + - * / ^
- int index = Max(Max(Max(Max(SearchFirstSymbol(str, '+'), SearchFirstSymbol(str, '-')), SearchFirstSymbol(str, '*')), SearchFirstSymbol(str, '/')), SearchFirstSymbol(str, '^'));
- // If not, then either a number or a variable
- if (index == -1)
- {
- if (isFloat(str))
- {
- return new Number(stof(str));
- }
- else
- { // . x .
- return new Variable(DeleteSpace(str)[0]);
- }
- }
- else
- { // Parse the left, the operation and the right side
- char *left = SubString(str, 0, index);
- char *right = SubString(str, index + 1, strlen(str));
- GetBinaryFunc(left, SubString(str, index, index + 1), right);
- }
- }//ГетБинарифанк
- Expression* ExpressionString(char *string)
- {
- Expression *result; // Result
- string = DeleteSpace(string); // We remove spaces on the sides
- int lenght = strlen(string);
- // Looking for external brackets
- int l = SearchFirstSymbol(string, '(');
- int r = SearchLastSymbol(string, ')');
- // If there are no brackets
- if (l == -1 && r == -1)
- {
- return Simplify(SimpleExpression(string));
- }
- // If there are brackets, consider the cases.
- // Find paired brackets for external
- int rp = -1, lp = -1;
- int Counter = 0; // counter brackets
- // We are looking for a pair for the left bracket
- for (int i = 0; i < lenght; i++)
- {
- if (string[i] == '(')
- {
- Counter++;
- }
- if (string[i] == ')')
- {
- if (Counter == 1)
- {
- lp = i;
- break;
- }
- Counter--;
- }
- }
- Counter = 0;
- for (int i = lenght - 1; i >= 0; i--)
- {
- if (string[i] == ')')
- {
- Counter++;
- }
- if (string[i] == '(')
- {
- if (Counter == 1)
- {
- rp = i;
- break;
- }
- Counter--;
- }
- }
- if (l == 0 && r == lenght - 1 && lp == r)
- { // (.o.) one outer brackets
- result = ExpressionString(SubString(string, 1, lenght - 1)); // Remove the external brackets and run the function again.
- }
- else
- if (lp != r)
- { // ().o.() two pairs of external brackets, between them there must be a binary operation
- char *funcstr = SubString(string, lp + 1, rp); // pick up the substring where the operation should be
- // We are looking for the binary operation index in the substring
- int index = Max(Max(Max(Max(SearchFirstSymbol(funcstr, '+'), SearchFirstSymbol(funcstr, '-')), SearchFirstSymbol(funcstr, '*')), SearchFirstSymbol(funcstr, '/')), SearchFirstSymbol(funcstr, '^'));
- index = index + lp + 1; // Get the index of the operation in the whole row
- char *left = DeleteSpace(SubString(string, 0, index));
- char *func = DeleteSpace(SubString(string, index, index + 1));
- char *right = DeleteSpace(SubString(string, index + 1, lenght));
- result = GetBinaryFunc(left, func, right);
- }
- else
- {
- if (r != lenght - 1)
- { // ...()o.. or ()o..
- char *funcstr = SubString(string, lp + 1, lenght); // pick up the substring where the operation should be
- int index = Max(Max(Max(Max(SearchFirstSymbol(funcstr, '+'), SearchFirstSymbol(funcstr, '-')), SearchFirstSymbol(funcstr, '*')), SearchFirstSymbol(funcstr, '/')), SearchFirstSymbol(funcstr, '^'));
- index = index + lp + 1; // Get the index of the operation in the whole row
- char *left = DeleteSpace(SubString(string, 0, index));
- char *func = DeleteSpace(SubString(string, index, index + 1));
- char *right = DeleteSpace(SubString(string, index + 1, lenght));
- result = GetBinaryFunc(left, func, right);
- }
- else
- { // ..o() or exp()
- char *funcstr = SubString(string, 0, l);
- int index = Max(Max(Max(Max(SearchFirstSymbol(funcstr, '+'), SearchFirstSymbol(funcstr, '-')), SearchFirstSymbol(funcstr, '*')), SearchFirstSymbol(funcstr, '/')), SearchFirstSymbol(funcstr, '^'));
- if (index == -1)
- { // then exp()
- char *arg = DeleteSpace(SubString(string, l + 1, r));
- }
- else
- { // then ..o()
- char *left = DeleteSpace(SubString(string, 0, index));
- char *func = DeleteSpace(SubString(string, index, index + 1));
- char *right = DeleteSpace(SubString(string, index + 1, lenght));
- result = GetBinaryFunc(left, func, right);
- }
- }
- }
- return Simplify(result);
- }
- //double func(double x) {
- // double y;
- // y = cos(6 * acos(x));
- // /*y = x*x*x*x*x+10*x*x-log(x);*/
- // return y;
- //}
- //
- //double der(double x) {
- // double y;
- // y = sin(6 * acos(x))*6*(1.0/sqrt(1-x*x));
- // /*y = 5*x*x*x*x+20*x-1.0/x;*/
- // return y;
- //}
- void Razlinovka(RenderWindow &window, int W, int H, int X_ZERO, int Y_ZERO, int X1, double a1, double b1, Expression *e, Expression *de) {
- Vertex line[6];
- double step = 20, arrow = 8, otstup = 1000;
- //разлиновка :
- double c;//
- if (fabs(
- a1) > fabs(b1)) c = a1;
- else c = b1;
- //горизонтали
- if (c < 0) {
- //горизонтали
- for (int i = 0; i <= (otstup - c) / 10; i++) {
- line[0] = Vertex(Vector2f(c - otstup, c - otstup + i * step));
- line[1] = Vertex(Vector2f(otstup - c, c - otstup + i * step));
- line[0].color = line[1].color = Color(200, 200, 200, 255);
- window.draw(line, 2, Lines);
- }
- //вертикали
- for (int i = 0; i <= (otstup - c) / 10; i++) {
- line[0] = Vertex(Vector2f(c - otstup + i * step, c - otstup));
- line[1] = Vertex(Vector2f(c - otstup + i * step, otstup - c));
- line[0].color = line[1].color = Color(200, 200, 200, 255);
- window.draw(line, 2, Lines);
- }
- }
- else {
- if (c > 0) {
- //горизонтали
- for (int i = 0; i <= (otstup + c) / 10; i++) {
- line[0] = Vertex(Vector2f(-otstup - c, -otstup - c + i * step));
- line[1] = Vertex(Vector2f(otstup + c, -otstup - c + i * step));
- line[0].color = line[1].color = Color(200, 200, 200, 255);
- window.draw(line, 2, Lines);
- }
- //вертикали
- if (c > 0) {
- for (int i = 0; i <= (otstup + c) / 10; i++) {
- line[0] = Vertex(Vector2f(-otstup - c + i * step, -otstup - c));
- line[1] = Vertex(Vector2f(-otstup - c +i * step, otstup + c));
- line[0].color = line[1].color = Color(200, 200, 200, 255);
- window.draw(line, 2, Lines);
- }
- }
- }
- else
- {
- //горизонтали
- for (int i = 0; i <= W / 10; i++) {
- line[0] = Vertex(Vector2f(-W, -W + i * step));
- line[1] = Vertex(Vector2f(W, -W + i * step));
- line[0].color = line[1].color = Color(200, 200, 200, 255);
- window.draw(line, 2, Lines);
- }
- //вертикали
- for (int i = 0; i <= W / 10; i++) {
- line[0] = Vertex(Vector2f(-W + i * step, -W));
- line[1] = Vertex(Vector2f(-W + i * step, W));
- line[0].color = line[1].color = Color(200, 200, 200, 255);
- window.draw(line, 2, Lines);
- }
- }
- }
- // Рисуем оси координат
- if (a1 < 0) {
- line[0] = sf::Vertex(sf::Vector2f(a1 - 1000, Y_ZERO));
- }
- else line[0] = sf::Vertex(sf::Vector2f(0, Y_ZERO));
- if (a1 > W) {
- line[1] = sf::Vertex(sf::Vector2f(a1 + 1000, Y_ZERO));
- }
- else line[1] = sf::Vertex(sf::Vector2f(W, Y_ZERO));
- if (b1 < 0) {
- line[2] = sf::Vertex(sf::Vector2f(X_ZERO, b1 - 1000));
- }
- else line[2] = sf::Vertex(sf::Vector2f(X_ZERO, 0));
- if (b1 > H) {
- line[3] = sf::Vertex(sf::Vector2f(X_ZERO, b1 + 1000));
- }
- else line[3] = sf::Vertex(sf::Vector2f(X_ZERO, H));
- line[0].color = line[1].color = line[2].color = line[3].color =
- sf::Color::Black;
- window.draw(line, 4, sf::Lines);
- // стрелки для осей OX
- line[0] = sf::Vertex(sf::Vector2f(W - arrow, Y_ZERO + arrow));
- line[1] = sf::Vertex(sf::Vector2f(W, Y_ZERO));
- line[2] = sf::Vertex(sf::Vector2f(W - arrow, Y_ZERO - arrow));
- // Для оси OY
- line[3] = sf::Vertex(sf::Vector2f(X_ZERO - arrow, arrow));
- line[4] = sf::Vertex(sf::Vector2f(X_ZERO, 0));
- line[5] = sf::Vertex(sf::Vector2f(X_ZERO + arrow, arrow));
- line[0].color = line[1].color = line[2].color = sf::Color::Black;
- line[3].color = line[4].color = line[5].color = sf::Color::Black;
- window.draw(line, 6, sf::Triangles);
- Vertex line3[4];
- //единичные отрезки
- line3[0] = Vertex(Vector2f(X_ZERO + X1, Y_ZERO + 5));
- line3[1] = Vertex(Vector2f(X_ZERO + X1, Y_ZERO - 5));
- line3[2] = Vertex(Vector2f(X_ZERO + 5, Y_ZERO - X1));
- line3[3] = Vertex(Vector2f(X_ZERO - 5, Y_ZERO - X1));
- line3[0].color = line3[1].color = line3[2].color = line3[3].color = Color::Black;
- window.draw(line3, 4, Lines);
- //рисуем границы рассматриваемого отрезка
- if (b1 < 0) {
- line[0] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, b1 - otstup));
- line[1] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, otstup - b1));
- line[2] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, b1 - otstup));
- line[3] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, otstup - b1));
- }
- else {
- line[0] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, -b1 - otstup));
- line[1] = sf::Vertex(sf::Vector2f(X_ZERO + X1 * A, b1 + otstup));
- line[2] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, -b1 - otstup));
- line[3] = sf::Vertex(sf::Vector2f(Y_ZERO + X1 * B, b1 + otstup));
- }
- line[0].color = line[1].color = line[2].color = line[3].color =
- sf::Color::Green;
- window.draw(line, 4, sf::Lines);
- //функция
- VertexArray line2(PrimitiveType::LinesStrip, W);
- for (double x = -X_ZERO; x < W; x += 0.01) {
- line2.append(Vertex(Vector2f(X1*x + X_ZERO, Y_ZERO - X1 * /*func(x)*/e->evaluate(x)), Color::Black));
- }
- window.draw(line2);
- //производная функции
- VertexArray line4(PrimitiveType::LinesStrip, W);
- for (double x = -X_ZERO; x < W; x += 0.01) {
- line4.append(Vertex(Vector2f(X1*x + X_ZERO, Y_ZERO - X1 */*der(x)*/de->evaluate(x)), Color::Red));
- }
- window.draw(line4);
- }
- double DEL(double a, double b, int N, double E, Expression *de) {
- double c, N1 = 0, x;
- while (fabs(b - a) >= E && N1 <= N) {
- c = (a + b) / 2;
- if (/*der(a)*der(c) */de->evaluate(a)*de->evaluate(c) < 0) {
- b = c;
- }
- if (/*der(c)*der(b)*/de->evaluate(c)*de->evaluate(b) < 0) {
- a = c;
- }
- }
- return (a + b) / 2;
- }
- int main()
- {
- Music music;//создаем объект музыки
- music.openFromFile("1.ogg");//загружаем файл
- int W = 1080;
- int H = 720;
- int X1 = 20;
- int W1 = 2000;
- int H1 = 2000;
- int Y_ZERO = H1 / 2, X_ZERO = W1 / 2;
- int k = 0;//переменная для мерцания
- RenderWindow window(VideoMode(W, H), "SMFL");
- window.clear(Color::White);
- char *s;
- //string s;
- //cin >> s;
- //s = "df";
- Expression *test = ExpressionString("exp((2*x))");
- Expression *de = e->diff();
- double a, b;
- double A1 = A;
- double i = A;
- double step = (B - A) / 100.0;
- //поиск минимумов и максимумов функции
- while (k == 0) {
- k++;
- while (/*der(A1)*der(i)*/de->evaluate(A1)*de->evaluate(i) > 0 && i < B) {
- i += step;
- }
- if (/*der(i) == 0*/de->evaluate(i) == 0) {
- a = i;
- b = /*func(a)*/e->evaluate(a);
- }
- else {
- if (i >= B) {
- a = (A1 + B) / 2;
- b = /*func(a);*/e->evaluate(a);
- }
- else {
- a = DEL(A1, i, 20000, 1.0e-5, de);
- b = /*func(a);*/e->evaluate(a);
- if (isnan(b))
- {
- k = 0;
- A1 = a;
- i += 1;
- }
- }
- }
- }
- k = 0;
- //камера
- View view(Vector2f(X_ZERO + X1 * a, Y_ZERO - X1 * b), Vector2f(300.f, 200.f));
- window.setView(view);
- /*Font font;
- font.loadFromFile("18245.ttf");
- Text text("", font, 20);
- text.setFillColor(Color::Black);
- ostringstream playerScoreString;*/
- music.play();
- // Главный цикл приложения. Выполняется, пока открыто окно
- while (window.isOpen())
- {
- //Vector2i pixelPos = Mouse::getPosition(window);
- //Vector2f pos = window.mapPixelToCoords(pixelPos);
- //cout << pixelPos.x << " ";//смотрим на координату Х позиции курсора в консоли (она не будет больше ширины окна)
- //cout << pos.x << "\n";
- // Обрабатываем очередь событий в цикле
- Event event;
- while (window.pollEvent(event))
- {
- // Пользователь нажал на <<крестик>> и хочет закрыть окно?
- if (event.type == Event::Closed)
- // Тогда закрываем его
- window.close();
- }
- if (Keyboard::isKeyPressed(Keyboard::Left)) {
- view.move(-100, 0); window.setView(view); k = 0;
- } //первая координата Х отрицательна =>идём влево
- if (Keyboard::isKeyPressed(Keyboard::Right))
- {
- view.move(100, 0); window.setView(view); k = 0;
- } //первая координата Х положительна =>идём вправо
- if (Keyboard::isKeyPressed(Keyboard::Up)) {
- view.move(0, -100); window.setView(view); k = 0;
- } //вторая координата (У) отрицательна =>идём вверх (вспоминаем из предыдущих уроков почему именно вверх, а не вниз)
- if (Keyboard::isKeyPressed(Keyboard::Down)) {
- view.move(0, 100); window.setView(view); k = 0;
- }
- if (Mouse::isButtonPressed(Mouse::Left)) {
- view.zoom(0.5); window.setView(view); k = 0;
- }
- if (Mouse::isButtonPressed(Mouse::Right)) {
- view.zoom(2); window.setView(view); k = 0;
- }
- //window.clear(Color::White);
- //i = 0;
- //playerScoreString << pos.x;
- //text.setString(playerScoreString.str());//задаем строку тексту и вызываем сформированную выше строку методом .str()
- //text.setPosition(view.getCenter().x + 150, view.getCenter().y - 200);//задаем позицию текста, отступая от центра камеры
- //if (i == 0) {
- // window.draw(text);
- // i++;
- //}
- /*отрисовка окна*/
- if (k == 0) {
- window.clear(Color::White);
- Razlinovka(window, W1, H1, X_ZERO, Y_ZERO, X1, X_ZERO + X1 * a, Y_ZERO - X1 * b, e, de);
- window.display();
- }
- k++;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment