2017-02-27 22:23:06 +00:00
|
|
|
/*-
|
2018-08-04 18:44:35 +00:00
|
|
|
* Copyright (c) 2014-2018 Carsten Sonne Larsen <cs@innolan.net>
|
2015-03-08 22:14:50 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
2017-02-27 22:23:06 +00:00
|
|
|
* Project homepage:
|
2017-07-14 09:30:24 +00:00
|
|
|
* https://amath.innolan.net
|
2017-02-27 22:23:06 +00:00
|
|
|
*
|
2015-03-08 22:14:50 +00:00
|
|
|
*/
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
#include "fgrid.h"
|
2015-03-08 22:14:50 +00:00
|
|
|
#include "lib/real.h"
|
|
|
|
|
|
|
|
Grid::Grid(UserFunction* function) :
|
|
|
|
function(function)
|
|
|
|
{
|
|
|
|
parameter = new RealNumber();
|
|
|
|
zoom = 1.0;
|
|
|
|
pad = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
Grid::~Grid()
|
|
|
|
{
|
|
|
|
delete parameter;
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
double Grid::GetHorizontalResolution() const
|
2015-03-08 22:14:50 +00:00
|
|
|
{
|
|
|
|
return (maxX - minX) / (screenMaxX - screenMinX);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Grid::SetFunctionBounderies(double minX, double maxX)
|
|
|
|
{
|
|
|
|
this->minX = minX;
|
|
|
|
this->maxX = maxX;
|
|
|
|
minY = FunctionValue(minX);
|
|
|
|
maxY = FunctionValue(maxX);
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
scaleX = (screenMaxX - screenMinX) / (maxX - minX);
|
|
|
|
scaleY = (screenMaxY - screenMinY) / (maxY - minY);
|
2015-03-08 22:14:50 +00:00
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
// Keep aspect ratio
|
|
|
|
if (scaleX < scaleY)
|
|
|
|
{
|
2015-03-08 22:14:50 +00:00
|
|
|
scaleX = scaleY;
|
2017-02-27 22:23:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-08 22:14:50 +00:00
|
|
|
scaleY = scaleX;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Grid::SetScreenBounderues(int minX, int maxX, int minY, int maxY)
|
|
|
|
{
|
|
|
|
screenMinX = minX + pad;
|
|
|
|
screenMaxX = maxX - pad;
|
|
|
|
screenMinY = minY + pad;
|
|
|
|
screenMaxY = maxY - pad;
|
|
|
|
|
|
|
|
originX = (screenMaxX - screenMinX) / 2;
|
|
|
|
originY = (screenMaxY - screenMinY) / 2;
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
double Grid::FunctionValue(double parameter) const
|
2015-03-08 22:14:50 +00:00
|
|
|
{
|
|
|
|
this->parameter->SetRealValue(parameter);
|
|
|
|
function->GetVariable()->AssignValue(this->parameter);
|
2017-02-27 22:23:06 +00:00
|
|
|
Number* res = function->GetExpression()->Evaluate();
|
2015-03-08 22:14:50 +00:00
|
|
|
double value = res->GetRealValue();
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
void Grid::GetScreenCoordinates(double value, int* x, int* y) const
|
2015-03-08 22:14:50 +00:00
|
|
|
{
|
|
|
|
double valueX = value * scaleX * zoom + originX;
|
|
|
|
double valueY = FunctionValue(value) * scaleY * zoom + originY;
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
*x = valueX < screenMinX || valueX > screenMaxX ? -1 : static_cast<int>(valueX);
|
|
|
|
*y = valueY < screenMinY || valueY > screenMaxY ? -1 : static_cast<int>(valueY);
|
2015-03-08 22:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
void Grid::GetScreenCoordinates(double xval, int* x, double yval, int* y) const
|
2015-03-08 22:14:50 +00:00
|
|
|
{
|
|
|
|
double valueX = xval * scaleX * zoom + originX;
|
|
|
|
double valueY = yval * scaleY * zoom + originY;
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
*x = valueX < screenMinX || valueX > screenMaxX ? -1 : static_cast<int>(valueX);
|
|
|
|
*y = valueY < screenMinY || valueY > screenMaxY ? -1 : static_cast<int>(valueY);
|
2015-03-08 22:14:50 +00:00
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
void Grid::GetXAxis(int* xstart, int* xend, int* ystart, int* yend) const
|
2015-03-08 22:14:50 +00:00
|
|
|
{
|
|
|
|
*xstart = screenMinX;
|
|
|
|
*xend = screenMaxX;
|
|
|
|
*ystart = originY;
|
|
|
|
*yend = originY;
|
|
|
|
}
|
|
|
|
|
2017-02-27 22:23:06 +00:00
|
|
|
void Grid::GetYAxis(int* xstart, int* xend, int* ystart, int* yend) const
|
2015-03-08 22:14:50 +00:00
|
|
|
{
|
|
|
|
*xstart = originX;
|
|
|
|
*xend = originX;
|
|
|
|
*ystart = screenMinY;
|
|
|
|
*yend = screenMaxY;
|
|
|
|
}
|