My name is Jiyaad by the way. Here’s my code:
//of App.h
#pragma once
#include “ofMain.h”
#include “ofxGui.h”
#include “ofMain.h”
#define N_MAX 20000
class ofApp : public ofBaseApp{
public:
//My Variables
float step = 0.001;
float xMin = 0.00;
float xMax = 10.0;
float yMin = -1.00;
float yMax = 1.00;
int nxGrid;
float xGridStep = 0.50;
int nyGrid;
float yGridStep = 0.20;
float xPixMin = 200;
float xPixMax = 1000;
float yPixMin = 600;
float yPixMax = 200;
int n; //number of samples
float x[N_MAX];
float y[N_MAX];
float xPix[N_MAX];
float yPix[N_MAX];
float xGrid[N_MAX];
float xGridPix[N_MAX];
float yGrid[N_MAX];
float yGridPix[N_MAX];
//My Functions
int getXSamples(float xMin, float xMax, float step, float x[]);
void getDampedCosSample(int n, float x[], float y[], float alpha, float w);
float map(float in, float inMin, float inMax, float outMin, float outMax);
void map_vec(int n, float in[], float out[], float inMin, float inMax, float outMin, float outMax);
void printArray(int dim, float x[], char label[]);
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxPanel gui;
ofxFloatSlider alpha;
ofxFloatSlider omega;
};
//ofApp.cpp
#include “ofApp.h”
int ofApp::getXSamples(float xMin, float xMax, float step, float x[])
{
int i;
int numSamples;
numSamples = (xMax - xMin) / step + 1;
for (i = 0; i < numSamples; i++)
{
x[i] = xMin + i * step;
}
return numSamples;
}
void ofApp::getDampedCosSample(int n, float x[], float y[], float alpha, float w)
{
int i;
for (i = 0; i < n; i++)
{
y[i] = exp(-alpha * x[i]) * cos(w * x[i]);
}
}
float ofApp::map(float in, float inMin, float inMax, float outMin, float outMax)
{
float m;
float b;
float out;
//slope
m = (outMax - outMin) / (inMax - inMin);
//y-inter
b = outMax - m * inMax;
//y = mx + b
out = m * in + b;
return out;
}
void ofApp::map_vec(int n, float in[], float out[], float inMin, float inMax, float outMin, float
outMax)
{
int i;
for (i = 0; i < n; i++)
{
out[i] = map(in[i], inMin, inMax, outMin, outMax);
}
}
void ofApp::printArray(int dim, float x[], char label[])
{
int i;
printf("\n%s: “, label);
for (i = 0; i < dim; i++)
{
printf(”%.2f ", x[i]);
}
}
//--------------------------------------------------------------
void ofApp::setup(){
gui.setup();
gui.add(alpha.setup("alpha(a)", 0.2, 0.1, 1));
gui.add(omega.setup("omega(w)", 5, 0, 10));
}
//--------------------------------------------------------------
void ofApp::update(){
n = getXSamples(xMin, xMax, step, x);
//printArray(n, x, “X values”);
getDampedCosSample(n, x, y, alpha, omega);
//printArray(n, y, “Y values”);
map_vec(n, x, xPix, xMin, xMax, xPixMin, xPixMax);
//printArray(n, xPix, “XPix”);
map_vec(n, y, yPix, yMin, yMax, yPixMin, yPixMax);
nxGrid = getXSamples(xMin, xMax, xGridStep, xGrid);
map_vec(nxGrid, xGrid, xGridPix, xMin, xMax, xPixMin, xPixMax);
nyGrid = getXSamples(yMin, yMax, yGridStep, yGrid);
map_vec(nyGrid, yGrid, yGridPix, yMin, yMax, yPixMin, yPixMax);
printArray(n, xGridPix, "x");
printArray(n, yGridPix, "y");
}
//--------------------------------------------------------------
void ofApp::draw(){
gui.draw();
ofSetBackgroundColor(255, 255, 255);
int i;
for (i = 0; i < n; i++)
{
ofSetColor(0, 0, 255);
ofSetLineWidth(1);
ofDrawCircle(xPix[i], yPix[i], 1);
}
for (i = 0; i < nxGrid; i++)
{
ofSetColor(0, 0, 255);
ofDrawLine(xGridPix[i], yPixMin, xGridPix[i], yPixMax);
n = xGridPix[i];
cout << "n" << n;
}
for (i = 0; i < nyGrid; i++)
{
ofSetColor(0, 0, 255);
ofDrawLine(xPixMin, yGridPix[i], xPixMax, yGridPix[i]);
cout << "y[i]" << yGridPix[i];
}
ofDrawBitmapString("The Damped Sinusoid Function: y = exp(-at)cos(wt)", 425, 150);
ofDrawBitmapString("y(t)", 120, 375);
ofDrawBitmapString("time, t", 550, 650);
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseEntered(int x, int y){
}
//--------------------------------------------------------------
void ofApp::mouseExited(int x, int y){
}
//--------------------------------------------------------------
void ofApp::windowResized(int w, int h){
}
//--------------------------------------------------------------
void ofApp::gotMessage(ofMessage msg){
}
//--------------------------------------------------------------
void ofApp::dragEvent(ofDragInfo dragInfo){
}
//main.cpp
#include “ofMain.h”
#include “ofApp.h”
//========================================================================
int main( ){
ofSetupOpenGL(1024,768,OF_WINDOW); // <-------- setup the GL context
// this kicks off the running of my app
// can be OF_WINDOW or OF_FULLSCREEN
// pass in width and height too:
ofRunApp(new ofApp());
}