C++ correcting the position for point(x,y) on the graph

Hello everyone, I am working on graphic calculator with OpenFrameworks. First I ask the user to enter how many point does he want on the graph and then I ask him for the point(X,Y) and then graph it.

The problem is when I put the point(X,Y), it position on the corner of the screen

Example:
if I put 50 for X and 30 for Y, P(50,30); The answer that I get back is

This is the code:

void ofApp::drawCircle(int x, int y){
    ofSetColor(0,0,0);
    ofCircle(x,y,10);
}
void ofApp::ofSetBackgroundColor(){
    ofBackgroundGradient(ofColor::white, ofColor::gray);
}
void ofApp::writeText(){

    ofSetColor(252,5,5);
    ofDrawBitmapString("X axis",500,690);
    ofDrawBitmapString("0   10   20    30   40   50   60   70   80   90   100   110   120   130   140   150  ",100,670);
    ofDrawBitmapString("Y axis",20,450);
    ofDrawBitmapString("10",70,610);
    ofDrawBitmapString("20",70,570);
    ofDrawBitmapString("30",70,530);
    ofDrawBitmapString("40",70,490);
    ofDrawBitmapString("50",70,450);
    ofDrawBitmapString("60",70,410);
    ofDrawBitmapString("70",70,370);
    ofDrawBitmapString("80",70,330);
    ofDrawBitmapString("90",70,290);
    ofDrawBitmapString("100",70,250);
    ofDrawBitmapString("110",70,210);
    ofDrawBitmapString("120",70,170);
    ofDrawBitmapString("130",70,130);
    ofDrawBitmapString("140",70,90);
    ofDrawBitmapString("150",70,50); 
}
void ofApp::DrawlineX(){
    ofSetColor(252,5,5);
    ofLine(100,30,100,650);
    ofSetColor(252,5,5);
    ofLine(100,650,750,650);
    ofSetColor(0,0,0);
    ofLine(100,650,750,270);
}
//--------------------------------------------------------------
void ofApp::update(){

}
//--------------------------------------------------------------
void ofApp::draw(){
    ofSetBackgroundColor();
    writeText();
    DrawlineX();

    if(pos==0){
    for(int i=0;i<TotoalDots;i++){
        drawCircle(arryX[i],arryY[i]);
    }
    }else{

        drawCircle(myX,myY);
    }
}

This iThis is the code:

hi,
you need to offset and probably scale that point so it matches the coordinate system of your graph. remember that OF, by default, considers the top left corner of the window as 0,0 with x increasing left to right and y increasing top to bottom.
An advise, use a for loop to put those labels, it will be cleaner, less error prone and faster to modify. Also, try to use variables for drawing instead of hard-coded numbers.

Best

Thank you for the advise because i am new to OpenFrameworks

I did get ofGetHeight()/2-myX,ofGetWidth()/2-myY, it movies the point to the middle of the graph but when i put new points, the position stay the same thing as the previous one

hello,

as roymacdonald said, remember the origin is top left corner.

i would recommend using ofPushMatrix() before drawing your points to move the origin wherever you need. aligning the drawing origin with your graph origin using ofTranslate() would be more handy

ofPushMatrix();
ofTranslate(graphX0, graphY0); //move draw origin to graph origin
//draw your points here
ofPopMatrix();

Thank you so much it finally worked

Excuse me, I need some help with my program as well. I am going to send my code, and show you guys what I got for my graph. I have a final project and I’m trying to fix it where I can use the slider to adjust the step size (number of samples) for the function I created. I have 2 parameters already for the slider, I am able to adjust alpha(a) and omega(w) with the slider. How do I fix the program so I can make it where the user can now also adjust the step size?

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());

}