amath  1.8.5
Simple command line calculator
plot.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 "plot.h"
31 #include "amath.h"
32 #include "amathc.h"
33 #include "lib/ntextd.h"
34 #include "system/program.h"
35 #include "../fgrid.h"
36 #include "../userfunction.h"
37 
38 PlotStatement::PlotStatement(const char* name, const char* parameter, const char* file) :
40 {
41  AllocAndCopy(&this->name, name);
42  AllocAndCopy(&this->parameter, parameter);
43  AllocAndCopy(&this->file, file);
44 }
45 
46 PlotStatement::PlotStatement(const char* name, const char* parameter) :
48 {
49  AllocAndCopy(&this->name, name);
50  AllocAndCopy(&this->parameter, parameter);
51  file = nullptr;
52 }
53 
55 {
56  delete [] name;
57  delete [] parameter;
58 
59  if (file != nullptr)
60  {
61  delete [] file;
62  }
63 }
64 
66 {
68 
69  if (function == nullptr)
70  {
71  return (char*)("Function does not exists." NEWLINE);
72  }
73 
75 
76  Grid* grid = new Grid(function);
77  static const int width = 400;
78  static const int height = 300;
79 
80  grid->SetScreenBounderues(0, width, 20, height);
81  bool first = true;
82 
83  static const double min = -5.0;
84  static const double max = +5.0;
85  grid->SetFunctionBounderies(min, max);
86 
87  double x = min;
88  double step = grid->GetHorizontalResolution();
89 
90  int screenX;
91  int screenY;
92 
93  RealNumber* n = new RealNumber();
94  NumeralSystem* ns = new DecimalSystem(5);
95 
96  while (x < max)
97  {
98  grid->GetScreenCoordinates(x, &screenX, &screenY);
99 
101 
102  if (screenX != -1 && screenY != -1)
103  {
104  if (first)
105  {
106  output->Append('(');
107  n->SetRealValue(screenX);
109  output->Append(',');
110  n->SetRealValue(height - screenY);
112  output->Append(')');
114  first = false;
115  }
116  else
117  {
118  output->Append('(');
119  n->SetRealValue(screenX);
121  output->Append(',');
122  n->SetRealValue(height - screenY);
124  output->Append(')');
126  }
127  }
128 
129  x = x + step;
130  }
131 
132  delete n;
133  delete ns;
134  delete grid;
135 
136  return output->GetString();
137 }
void Append(const char c)
Definition: charbuf.cpp:245
#define NEWLINE
Definition: amath.h:222
void SetScreenBounderues(int minX, int maxX, int minY, int maxY)
Definition: fgrid.cpp:72
UserFunction * GetFunctionDef(const char *name, const char *argument) const
PlotStatement(const char *name, const char *parameter, const char *file)
Definition: plot.cpp:38
void Empty()
Definition: charbuf.cpp:218
char * GetString() const
Definition: charbuf.cpp:306
char * Execute()
Definition: plot.cpp:65
Base class for all statements in a syntax tree.
Definition: node.h:40
Grid(UserFunction *function)
Definition: fgrid.cpp:33
Definition: fgrid.h:42
PlotStatement(const char *name, const char *parameter)
Definition: plot.cpp:46
char * file
Definition: plot.h:46
void Append(const char *source)
Definition: charbuf.cpp:262
virtual const char * GetText(Number *number)=0
void SetFunctionBounderies(double minX, double maxX)
Definition: fgrid.cpp:51
class FunctionList * Functions
Definition: program.h:78
Represent a real number with 15 significant digits.
Definition: real.h:45
char * parameter
Definition: plot.h:45
RealNumber()
Definition: real.cpp:37
double GetHorizontalResolution() const
Definition: fgrid.cpp:46
StatementNode()
Definition: node.cpp:34
void GetScreenCoordinates(double value, int *x, int *y) const
Definition: fgrid.cpp:92
char * name
Definition: plot.h:44
void EnsureGrowth(unsigned int size)
Definition: charbuf.cpp:169
A user defined function.
Definition: userfunction.h:44
Base class for all numeral systems.
Definition: ntext.h:49
void SetRealValue(double value)
Definition: real.cpp:100
~PlotStatement()
Definition: plot.cpp:54
CharBuffer * output
Definition: nodes.h:85
DecimalSystem(unsigned int digits)
Definition: ntextd.cpp:66
unsigned int AllocAndCopy(char **destination, const char *source)
Allocate memory and copy a string into the array.
Definition: alloccpy.c:40