amath  1.8.5
Simple command line calculator
fgrid.cpp
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * Project homepage:
26  * https://amath.innolan.net
27  *
28  */
29 
30 #include "fgrid.h"
31 #include "lib/real.h"
32 
33 Grid::Grid(UserFunction* function) :
34  function(function)
35 {
36  parameter = new RealNumber();
37  zoom = 1.0;
38  pad = 8;
39 }
40 
42 {
43  delete parameter;
44 }
45 
47 {
48  return (maxX - minX) / (screenMaxX - screenMinX);
49 }
50 
51 void Grid::SetFunctionBounderies(double minX, double maxX)
52 {
53  this->minX = minX;
54  this->maxX = maxX;
55  minY = FunctionValue(minX);
56  maxY = FunctionValue(maxX);
57 
58  scaleX = (screenMaxX - screenMinX) / (maxX - minX);
59  scaleY = (screenMaxY - screenMinY) / (maxY - minY);
60 
61  // Keep aspect ratio
62  if (scaleX < scaleY)
63  {
64  scaleX = scaleY;
65  }
66  else
67  {
68  scaleY = scaleX;
69  }
70 }
71 
72 void Grid::SetScreenBounderues(int minX, int maxX, int minY, int maxY)
73 {
74  screenMinX = minX + pad;
75  screenMaxX = maxX - pad;
76  screenMinY = minY + pad;
77  screenMaxY = maxY - pad;
78 
79  originX = (screenMaxX - screenMinX) / 2;
80  originY = (screenMaxY - screenMinY) / 2;
81 }
82 
83 double Grid::FunctionValue(double parameter) const
84 {
85  this->parameter->SetRealValue(parameter);
88  double value = res->GetRealValue();
89  return value;
90 }
91 
92 void Grid::GetScreenCoordinates(double value, int* x, int* y) const
93 {
94  double valueX = value * scaleX * zoom + originX;
95  double valueY = FunctionValue(value) * scaleY * zoom + originY;
96 
97  *x = valueX < screenMinX || valueX > screenMaxX ? -1 : static_cast<int>(valueX);
98  *y = valueY < screenMinY || valueY > screenMaxY ? -1 : static_cast<int>(valueY);
99 }
100 
101 void Grid::GetScreenCoordinates(double xval, int* x, double yval, int* y) const
102 {
103  double valueX = xval * scaleX * zoom + originX;
104  double valueY = yval * scaleY * zoom + originY;
105 
106  *x = valueX < screenMinX || valueX > screenMaxX ? -1 : static_cast<int>(valueX);
107  *y = valueY < screenMinY || valueY > screenMaxY ? -1 : static_cast<int>(valueY);
108 }
109 
110 void Grid::GetXAxis(int* xstart, int* xend, int* ystart, int* yend) const
111 {
112  *xstart = screenMinX;
113  *xend = screenMaxX;
114  *ystart = originY;
115  *yend = originY;
116 }
117 
118 void Grid::GetYAxis(int* xstart, int* xend, int* ystart, int* yend) const
119 {
120  *xstart = originX;
121  *xend = originX;
122  *ystart = screenMinY;
123  *yend = screenMaxY;
124 }
void SetScreenBounderues(int minX, int maxX, int minY, int maxY)
Definition: fgrid.cpp:72
void GetScreenCoordinates(double xval, int *x, double yval, int *y) const
Definition: fgrid.cpp:101
ExpressionNode * GetExpression() const
double maxY
Definition: fgrid.h:65
int originX
Definition: fgrid.h:73
Grid(UserFunction *function)
Definition: fgrid.cpp:33
Definition: fgrid.h:42
void GetXAxis(int *xstart, int *xend, int *ystart, int *yend) const
Definition: fgrid.cpp:110
double zoom
Definition: fgrid.h:75
void AssignValue(Number *value)
Definition: values.cpp:79
Definition: numb.h:66
RealNumber * parameter
Definition: fgrid.h:60
int screenMinX
Definition: fgrid.h:68
double scaleY
Definition: fgrid.h:77
void SetFunctionBounderies(double minX, double maxX)
Definition: fgrid.cpp:51
double minX
Definition: fgrid.h:62
Represent a real number with 15 significant digits.
Definition: real.h:45
Variable * GetVariable() const
virtual double GetRealValue()=0
int screenMinY
Definition: fgrid.h:70
RealNumber()
Definition: real.cpp:37
double GetHorizontalResolution() const
Definition: fgrid.cpp:46
virtual Number * Evaluate()=0
double minY
Definition: fgrid.h:64
UserFunction * function
Definition: fgrid.h:59
void GetYAxis(int *xstart, int *xend, int *ystart, int *yend) const
Definition: fgrid.cpp:118
double maxX
Definition: fgrid.h:63
void GetScreenCoordinates(double value, int *x, int *y) const
Definition: fgrid.cpp:92
int screenMaxX
Definition: fgrid.h:69
A user defined function.
Definition: userfunction.h:44
int originY
Definition: fgrid.h:74
double scaleX
Definition: fgrid.h:76
int pad
Definition: fgrid.h:67
void SetRealValue(double value)
Definition: real.cpp:100
double FunctionValue(double parameter) const
Definition: fgrid.cpp:83
int screenMaxY
Definition: fgrid.h:71
~Grid()
Definition: fgrid.cpp:41