#include <iostream>#include <vector>#include <algorithm>//std::any_of#include <numeric>//std::iotausingnamespacestd;structPredicate{int N;Predicate(intv):N(v){}//ctorbooloperator()(constintval)const{return val >= N;}};intmain(){ vector<int> v1 {1,2,3,4,-1};std::vector<int>v2(10);std::iota(v2.begin(),v2.end(),0);// creates range from 0 to 9int N=2; Predicate predicate{N}; cout <<"How many num of numbers greater or equal to "<< N <<"?\n"<<std::count_if(v1.begin(),v1.end(), predicate)<<endl;//calls predicate() on each value of v cout <<"How many num of numbers greater or equal to "<< N <<"?\n"<<std::count_if(v2.begin(),v2.end(), predicate)<<endl;//cout << std::count_if(v.begin(), v.end(),[N](int a){ return (a >= N);}) <<endl; //lambda expressionreturn0;}
std::iota creates range from the number of the 3rd param std::count_if count how many times the predicate returns true
Note: The better way would to be to replace our functor with a lambda expression
Revisiting the polynomial functor
ax2+bx+c
Same code but with color and different poly(x)
ADD IMAGE
Exercise
Find in the above code how many poly(3) are greater than 50 ax2+bx+c>50
Erase the ostream and couts
Exercise
Lets add Polynomial addition
+
(1x2+2x+3)+(10x2+10x+10)
=11x2+12x+13
+=
++ - pre/postfix: this will add 1 to the c of the polynomial
ax2+bx+(c+1)
SFML version
Before we begin lets install SFML
To compile
And to run
Lets now code it 😃
We should also normalize but we are going to keep it simple 1/max∗fx∗Height
Polynomial 3
ax3+bx2+cx+d
Add:
Integrals: prefix,postfix
Derivatives: prefix,postfix
Our definition of integration ∫(bx2+cx+d)dx=3bx3+2cx2+dx+0
∫(0x3+2x2+3x+4)dx=32x3+23x2+4x+0
f′(32x3+23x2+4x+0)=0x3+2x2+3x+4
SFML version
Adding the [] operator
Thinking exercise
Lets say we would like to add < operator what would you do?
I suggest we we start we can compare the a of ax^3 and
return the lowest . If the they have the same the compare the b of ax^2.
Now we are able to sort our polynimials
If extra time
Convert polynomial to array type instead of _a,_b
Deep copy
Inline
Without inline
When the program executes the function call instruction
the CPU stores the memory address of the instruction following the function call
copies the arguments of the function on the stack
and finally transfers control to the specified function.
The CPU then executes the function code
stores the function return value in a predefined memory location/register
and returns control to the calling function.
With inline
When the inline function is called whole code of the inline function gets inserted or substituted at the point of inline function call. This substitution is performed by the C++ compiler at compile time
Remember: inlining is only a request to the compiler, not a command Note: all the functions defined inside the class are implicitly inline.
Syntax
use the inline keyword before the function declaration
#include <SFML/Graphics.hpp>
#include <time.h>
#include <list>
#include <iostream>
#include <cmath>
using namespace sf;
using namespace std;
const int W = 800;
const int H = 800;
class Polynomial3 {
double _a,_b,_c,_d;
public:
Polynomial3(double a):_a(a), _b(0), _c(0),_d(0){ }
Polynomial3(double a, double b, double c, double d):_a(a), _b(b), _c(c),_d(d){ }
double operator() (double x) {
return _a*x*x*x + _b*x*x + _c*x+_d;
}
double operator()() { return 0; }
Polynomial3& operator+=(const Polynomial3& other_p){
this->_a += other_p._a;
this->_b += other_p._b;
this->_c += other_p._c;
this->_d += other_p._d;
return *this;
}
friend const Polynomial3 operator+ (const Polynomial3& p1, const Polynomial3& p2);
friend ostream& operator<< (ostream& os, const Polynomial3& p) {
return (os << "poly2 = "
<< p._a << "*x^3 + "
<< p._b << "*x^2 + "
<< p._c << "*x + "
<< p._d );
}
// Overloading the prefix operator, & T::operator++();
Polynomial3& operator++(){
if(_a!=0) //would be better to throw error
return *this;
//throw out_of_range("Cannot integrate more than 3rd degree: ");
_a = _b/3;
_b = _c/2;
_c = _d;
_d = 0; //should be c
return *this;
}
// Overloading the postfix operator, T T::operator++();
Polynomial3 operator++(int){
if(_a!=0) //would be better to throw error
return *this;
// //throw out_of_range("Cannot integrate more than 3rd degree: ");
Polynomial3 temp(_b/3, _c/2,_d , 0);
_d = _c; //should be c
_c = 2*_b;
_b = 3*_a;
_a = 0;
return temp;
}
// Overloading the prefix operator, & T::operator--();
Polynomial3& operator--(){
_d = _c; //should be c
_c = 2*_b;
_b = 3*_a;
_a = 0;
return *this;
}
// Overloading the postfix operator, T T::operator--();
Polynomial3 operator--(int){
Polynomial3 temp(0, 3*_a,2*_b , _c);
_a = 0;
_b = 3*_a;
_c = 2*_b;
_d = _c; //should be c
return temp;
}
};
int main(){
sf::RenderWindow window(sf::VideoMode(W, H), "Hello world!");
window.setFramerateLimit(30);
// ImGui::SFML::Init(window);
//https://www.youtube.com/watch?v=QyAjRULZkHw
//https://www.sfml-dev.org/tutorials/2.0/graphics-vertex-array.php
sf::Vertex line_y[] = {
sf::Vertex(sf::Vector2f(W/2, 0)),
sf::Vertex(sf::Vector2f(W/2, H))
};
sf::Vertex line_x[] = {
sf::Vertex(sf::Vector2f(0, H/2)),
sf::Vertex(sf::Vector2f(W, H/2))
};
// line_x[0].color = sf::Color::Blue;
// line_y.rotate(90);
// shape.setFillColor(sf::Color::Green);
int num_of_points = 200;
sf::VertexArray points(sf::Points, num_of_points);
Polynomial3 poly {0,2,3,4};
// Polynomial3 poly {0,3,-4}; //line
int fx_points[W];
int half_screen = W/2;
for (int i = 0; i < num_of_points ; ++i) {
fx_points[i] = poly(i-num_of_points/2);
std::cout << i-num_of_points/2 << ": "<< fx_points[i] << std::endl;
int temp = fx_points[i]*-1+H/2;
int temp2 = fx_points[i]*-1+H/2;
// cout << temp2 << endl;
points[i].position = sf::Vector2f(i+W/2, fx_points[i]*-1+H/2);//SFML is y is opposite
}
//2nd polynom
int fx_points2[W];
sf::VertexArray points2(sf::Points, num_of_points);
Polynomial3 poly2 = --poly;
//calculating and drawing points
for (int i = 0; i < num_of_points ; ++i) {
fx_points2[i] = poly2(i-num_of_points/2);
// std::cout << i-num_of_points/2 << ": "<< fx_points2[i] << std::endl;
points2[i].position = sf::Vector2f(i+W/2, fx_points2[i]*-1+H/2);//SFML is y is opposite
}
sf::Clock deltaClock;
/////main loop/////
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event)){
if (event.type == Event::Closed)
window.close();
}
//////draw//////
deltaClock.restart();
window.clear();
window.draw(points);
window.draw(points2);
window.draw(line_y, 2, sf::Lines);
window.draw(line_x, 2, sf::Lines);
window.display();
}
return 0;
}
const int operator[](uint index) const {
if (index >= 4)
throw out_of_range("Array index out of bounds: "+to_string(index));
switch (index) {
case 0:
return _d;
case 1:
return _c;
case 2:
return _b;
case 3:
return _a;
}
}
int main(){
Polynomial3 p{0,45,3,4};
cout << p << endl;
cout << p[2] << endl;
return 0;
}