My Project
graphics/plot2d.cpp
/*******************************************************
* Copyright (c) 2014, ArrayFire
* All rights reserved.
*
* This file is distributed under 3-clause BSD license.
* The complete license agreement can be obtained at:
* http://arrayfire.com/licenses/BSD-3-Clause
********************************************************/
#include <arrayfire.h>
#include <cstdio>
#include <math.h>
using namespace af;
static const int ITERATIONS = 50;
static const float PRECISION = 1.0f/ITERATIONS;
int main(int argc, char *argv[])
{
try {
// Initialize the kernel array just once
af::Window myWindow(1024, 512, "2D Plot example: ArrayFire");
array Y;
int sign = 1;
array X = seq(-af::Pi, af::Pi, PRECISION);
array noise = randn(X.dims(0))/5.f;
myWindow.grid(1, 2);
for (double val=-af::Pi; !myWindow.close(); ) {
Y = sin(X);
myWindow(0,0).plot(X, Y);
myWindow(0,1).scatter(X, Y + noise, AF_MARKER_POINT);
myWindow.show();
X = X + PRECISION * float(sign);
val += PRECISION * float(sign);
if (val>af::Pi) {
sign = -1;
} else if (val<-af::Pi) {
sign = 1;
}
}
} catch (af::exception& e) {
fprintf(stderr, "%s\n", e.what());
throw;
}
return 0;
}
Window object to render af::arrays.
Definition: graphics.h:37
A multi dimensional data container.
Definition: array.h:27
Definition: exception.h:20
virtual const char * what() const
Definition: exception.h:34
seq is used to create seq for indexing af::array
Definition: seq.h:46
@ AF_MARKER_POINT
Definition: defines.h:387
AFAPI array sign(const array &in)
C++ Interface for getting the sign of input.
AFAPI array sin(const array &in)
C++ Interface for sin.
AFAPI array randn(const dim4 &dims, const dtype ty=f32)
AFAPI void info()
void scatter(const array &X, const array &Y, const af::markerType marker=AF_MARKER_POINT, const char *const title=NULL)
Renders the input arrays as a 2D scatter-plot to the window.
void plot(const array &X, const array &Y, const char *const title=NULL)
Renders the input arrays as a 2D plot to the window.
bool close()
Check if window is marked for close.
void show()
This function swaps the background buffer to current view and polls for any key strokes while the win...
void grid(const int rows, const int cols)
Setup grid layout for multiview mode in a window.
dim4 dims() const
Get dimensions of the array.
Definition: algorithm.h:15
AFAPI const double Pi