2-D Function plots
VectSharp.Plots can plot functions of two variables. These can be created using some overloads of the Plot.Create.Function
method:
There are three overloads of the Function
method that can be used to plot functions of two variables:
- An overload taking a first argument of type
Func<double, double, double>
(i.e., a function accepting twodouble
parameters and returning adouble
). - An overload taking a first argument of type
Func<double[], double>
(i.e., a function accepting adouble[]
parameter and returning adouble
). Thedouble[]
argument to the function will contain the values of the two variables that should be used to compute the function’s value. - An overload taking a first argument of type
Function2DGrid
. This consists of a function that has already been sampled over a range of values (see below).
The first two overloads are very similar; they have the same parameters, which include four required parameters (double minX
, double minY
, double maxX
, and double maxY
) used to define the range of values over which the function will be sampled, as well as some optional parameters that can be used to determine the appearance of some elements of the plot. Many of these are in common with other kinds of plots and are described in the page about scatter plots; here are the ones specific for function plots:
double resolutionX
anddouble resolutionY
determine how often the function is sampled along both axes. If these aredouble.NaN
(the default), the resolution is determined automatically based on the coordinate system.Function2DGrid.GridType? gridType
: this parameter determines the kind of grid along which the values are sampled. If this isFunction2DGrid.GridType.Rectangular
, values are sampled along a simple rectangular grid. If this isFunction2DGrid.GridType.HexagonHorizontal
orFunction2DGrid.GridType.HexagonVertical
, values are sampled along a hexagonal grid. If this isFunction2DGrid.GridType.Irregular
, values are sampled at randomly chosen values.Function2D.PlotType plotType
: this parameter determines what kind of plot is produced. If this isFunction2D.PlotType.SampledPoints
, a symbol is drawn at the values of the function that have been sampled. If this isFunction2D.PlotType.Tessellation
(the default), the plot area is coloured using shapes appropriates to the kind of grid along which the values have been sampled. If this isFunction2D.PlotType.Raster
, the sampled values are used to produce a raster image, which is then stretched to cover the plot area.int rasterResolutionX
andint rasterResolutionY
: if theplotType
isFunction2D.PlotType.Raster
, these parameters determine the width and height of the raster image used to show the function values.IDataPointElement pointElement
: if theplotType
isFunction2D.PlotType.SampledPoints
, this determines the symbol drawn at each sampled point.Func<double, Colour> colouring
: this parameter determines the function used to transform the function values into colours for the plot. By default, a viridis gradient is used. You should set this to a function accepting a singledouble
argument ranging between0
and1
(inclusive) and returning aColour
. You can also provide aGradientStops
object. For example, theVectSharp.Gradients
static class has a number of gradients and functions that are ready to use.
The following example shows the effect of some of these optional parameters (the example runs at a relatively low resolution because Blazor is slow):
The Function2DGrid
class
The Function2DGrid
parameter used by the third overload of this method consists of a collection of values that have been sampled for the function. This overload does not have the minX
, minY
, maxX
, maxY
, resolution
, and gridType
arguments, because these are determined by the Function2DGrid
; the other arguments are the same as the other overloads.
A Function2DGrid
can be created using the Function2DGrid
constructor; this has two overloads. The first overload takes an IReadOnlyList<IReadOnlyList<double>>
argument containing values that have already been sampled for the function; each element of this array is itself an array of double
s, where the first two elements are the $x$ and $y$ arguments of the function and the third element is the value of $f(x, y)$. The second overload takes the function delegate, as well as the range parameters minX
, minY
, maxX
, and maxY
. The int stepsX
and int stepsY
parameters determine how many times the function is sampled, while the GridType type
parameter determines the kind of grid (rectangular, hexagonal, or irregular).
You can manually instantiate a Function2DGrid
object rather than plotting the function directly if you wish to have more control over the sampling of the function (especially if you use the constructor taking the collection of sampled values).