Advertisement
sNow_32

QCustomParabola

Sep 27th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.36 KB | None | 0 0
  1. // qcustomparabola.h
  2. #ifndef QCUSTOMPARABOLA_H
  3. #define QCUSTOMPARABOLA_H
  4.  
  5. #include <qcustomplot.h>
  6.  
  7. class QCustomParabola
  8. {
  9.     QVector < double > x, y;
  10.     double x_min, x_max,
  11.     a, b, c, y_min, y_max;
  12.     int accuracy;
  13.     QCPGraph *parabola;
  14.     QCustomPlot *plot;
  15.     QString name;
  16.     void fillXY();
  17.     QBrush brush;
  18.     QPen pen;
  19.     QColor color;
  20.     QCPItemText *headLabel;
  21. public:
  22.     QCustomParabola( double __x_min,
  23.                      double __x_max,
  24.                      int __accuracy,
  25.                      double __a,
  26.                      double __b,
  27.                      double __c, QString __name,
  28.                      QCustomPlot *__plot );
  29.     QCustomParabola( QCustomPlot *__plot );
  30.     ~ QCustomParabola();
  31.  
  32. public slots:
  33.     void setXRange( double __x_min, double __x_max );
  34.     void setAccuracy( int __accuracy );
  35.     void setABC( double __a,
  36.                  double __b,
  37.                  double __c );
  38.     void setName( QString __name );
  39.     void setBrush( QBrush __brush );
  40.     void setPen( QPen __pen );
  41.     void setColor( QColor __color );
  42.     void show();
  43.     void showHead( bool visible );
  44.  
  45.     double getXMIN();
  46.     double getXMAX();
  47.     double getYMAX();
  48.     double getYMIN();
  49.     double getA();
  50.     double getB();
  51.     double getC();
  52.     QString getName();
  53.     double getHeadX();
  54.     double getHeadY();
  55.     double getDiscriminant();
  56.     QVector < double > getX();
  57.     QVector < double > getY();
  58.     QCPGraph *getGraph();
  59.     QBrush getBrush();
  60.     QPen getPen();
  61. };
  62.  
  63. #endif // QCUSTOMPARABOLA_H
  64.  
  65. //--------------------------------------------------------------------------------------------------------------------
  66.  
  67. // qcustomparabola.cpp
  68. #include "qcustomparabola.h"
  69.  
  70. void QCustomParabola::fillXY()
  71. {
  72.     int k = 0;
  73.     y_min = 0;
  74.     for ( double i = x_min * accuracy; i <= x_max * accuracy; ++i ) {
  75.         x.push_back( i/accuracy) ;
  76.         y.push_back( a * x[k] * x[k] + b * x[k] + c);
  77.         if (y[k] < y_min)
  78.             y_min = y[k];
  79.         k++;
  80.     }
  81.     y_max = y[y.size() - 1];
  82. }
  83.  
  84. QCustomParabola::QCustomParabola(double __x_min,
  85.                                  double __x_max,
  86.                                  int __accuracy,
  87.                                  double __a,
  88.                                  double __b,
  89.                                  double __c,
  90.                                  QString __name,
  91.                                  QCustomPlot *__plot )
  92. {
  93.     x.clear(); y.clear();
  94.     x_min = __x_min;
  95.     x_max = __x_max;
  96.     accuracy = __accuracy;
  97.     a = __a;
  98.     b = __b;
  99.     c = __c;
  100.     name = __name;
  101.  
  102.     fillXY();
  103.     plot = __plot;
  104.     parabola = plot->addGraph();
  105.     parabola->setName( __name );
  106.     brush = parabola->brush();
  107.     pen = parabola->pen();
  108.     parabola->setData( x, y );
  109. }
  110.  
  111. QCustomParabola::QCustomParabola( QCustomPlot *__plot )
  112. {
  113.     plot = __plot;
  114.     parabola = plot->addGraph();
  115. }
  116.  
  117. QCustomParabola::~QCustomParabola()
  118. {
  119.  
  120. }
  121.  
  122. void QCustomParabola::setXRange(double __x_min,
  123.                                 double __x_max)
  124. {
  125.     x_min = __x_min;
  126.     x_max = __x_max;
  127. }
  128.  
  129. void QCustomParabola::setAccuracy(int __accuracy)
  130. {
  131.     accuracy = __accuracy;
  132. }
  133.  
  134. void QCustomParabola::setABC(double __a, double __b, double __c)
  135. {
  136.     a = __a;
  137.     b = __b;
  138.     c = __c;
  139. }
  140.  
  141. void QCustomParabola::setName(QString __name)
  142. {
  143.     name = __name;
  144.     parabola->setName( name );
  145. }
  146.  
  147. void QCustomParabola::setBrush(QBrush __brush)
  148. {
  149.     brush = __brush;
  150.     parabola->setBrush( brush );
  151. }
  152.  
  153. void QCustomParabola::setPen(QPen __pen)
  154. {
  155.     pen = __pen;
  156.     parabola->setPen( pen );
  157.  
  158. }
  159.  
  160. void QCustomParabola::setColor(QColor __color)
  161. {
  162.     color = __color;
  163.     pen = QPen( color );
  164.     parabola->setPen( QPen( color ) );
  165. }
  166.  
  167. void QCustomParabola::show()
  168. {
  169.     x.clear(); y.clear();
  170.     fillXY();
  171.     parabola->setData( x, y );
  172. }
  173.  
  174. void QCustomParabola::showHead(bool visible)
  175. {
  176.     if ( visible == true ) {
  177.         headLabel = new QCPItemText( plot );
  178.         plot->addItem( headLabel );
  179.         headLabel->position->setCoords( this->getHeadX(),
  180.                                         this->getHeadY() - 2);
  181.         headLabel->setText("[" + QString::number( this->getHeadX()  )
  182.                            + ";" + QString::number( this->getHeadY() ) + "]");
  183.     } else {
  184.         delete headLabel;
  185.     }
  186. }
  187.  
  188. double QCustomParabola::getXMIN()
  189. {
  190.     return x_min;
  191. }
  192.  
  193. double QCustomParabola::getXMAX()
  194. {
  195.     return x_max;
  196. }
  197.  
  198. double QCustomParabola::getYMAX()
  199. {
  200.     return y_max;
  201. }
  202.  
  203. double QCustomParabola::getYMIN()
  204. {
  205.     return y_min;
  206. }
  207.  
  208. double QCustomParabola::getA()
  209. {
  210.     return a;
  211. }
  212.  
  213. double QCustomParabola::getB()
  214. {
  215.     return b;
  216. }
  217.  
  218. double QCustomParabola::getC()
  219. {
  220.     return c;
  221. }
  222.  
  223. QString QCustomParabola::getName()
  224. {
  225.     return name;
  226. }
  227.  
  228. double QCustomParabola::getHeadX()
  229. {
  230.     return ( -b/(2 * a) );
  231. }
  232.  
  233. double QCustomParabola::getHeadY()
  234. {
  235.     return ( -getDiscriminant() / (4 * a) );
  236. }
  237.  
  238. double QCustomParabola::getDiscriminant()
  239. {
  240.     return ( b * b - 4 * a * c );
  241. }
  242.  
  243. QVector<double> QCustomParabola::getX()
  244. {
  245.     return x;
  246. }
  247.  
  248. QVector<double> QCustomParabola::getY()
  249. {
  250.     return y;
  251. }
  252.  
  253. QCPGraph *QCustomParabola::getGraph()
  254. {
  255.     return parabola;
  256. }
  257.  
  258. QBrush QCustomParabola::getBrush()
  259. {
  260.     return brush;
  261. }
  262.  
  263. QPen QCustomParabola::getPen()
  264. {
  265.     return pen;
  266. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement