### co ordinate geometry problem with SDL

Hi guys,

I am just plotting a line with two points x and y,

I created a Coordinate class that will get the y point given an equation in the right format y = slope + y intercept

It does indeed plot straight lines but the lines don't seem to vary at all, to me they seem to be almost in the same pattern the only difference is when y gets much larger the points are far more spread out, no matter how much I change the Y intercept or slope the line seems to have the same slope and y intercept

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495`` ``````#include #include #include #include #include "Coordinate.h" using namespace std; Coordinate::Coordinate(){ equation = " "; y = 0; x = 0; } Coordinate::Coordinate(string equation,double x): equation(equation),y(0),x(x){} Coordinate::Coordinate(const Coordinate &other){ this->equation = other.equation; this->x = other.x; this->y = other.y; } Coordinate& Coordinate::operator=(const Coordinate &other){ this->equation = other.equation; this->x = other.x; this->y = other.y; return *this; } int Coordinate::findStartDigit(int index){ int startDigit = 0; while(true) { if(index == 0) { break; } if(!isdigit(equation.at(index))) { break; } startDigit = index; index--; } return startDigit; } double Coordinate::calculateY() { for(int i = 0; i < equation.size(); i++) { if(equation.at(i) == 'y') { continue; } if(equation.at(i) == 'x') { if(equation.at(i-1) != '=' && equation.at(i-1) != ' ') { int startDigit = findStartDigit(i-1); stringstream convert; string subStr; int number = 0; subStr = equation.substr(startDigit,i); convert << subStr; convert >> number; y = number * x; continue; } } if(equation.at(i) == '+') { stringstream convert; string subStr; subStr = equation.substr(i+1,equation.size()-1); int number = 0; convert << subStr; convert >> number; y += number; } } return y; } ``````

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104`` `````` #include #include #include #include #include "Coordinate.h" SDL_Renderer *renderer; SDL_Texture *texture; SDL_Surface *surface; SDL_Window *window; SDL_Event event; Uint32* buffer; const int HEIGHT = 500; const int WIDTH = 500; bool init(){ if(SDL_Init(SDL_INIT_EVERYTHING) < 0){ cout << "error" << endl; return false; } window = SDL_CreateWindow("lines",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,WIDTH,HEIGHT,SDL_WINDOW_SHOWN); renderer = SDL_CreateRenderer(window,-1,SDL_RENDERER_PRESENTVSYNC); texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBA8888,SDL_TEXTUREACCESS_STATIC,WIDTH,HEIGHT); buffer = new Uint32[WIDTH * HEIGHT]; } void setPixel(int x,int y,Uint8 red,Uint8 green,Uint8 blue){ Uint32 color = 0; color += red; color <<= 8; color += green; color <<= 8; color+=blue; color <<= 8; color += 0xFF; buffer[(y*WIDTH)+x] = color; } void render(){ SDL_UpdateTexture(texture,NULL,buffer,WIDTH*sizeof(Uint32)); SDL_RenderClear(renderer); SDL_RenderCopy(renderer,texture,NULL,NULL); SDL_RenderPresent(renderer); } void cleanUp(){ SDL_DestroyRenderer(renderer); renderer = NULL; SDL_DestroyWindow(window); window = NULL; delete[] buffer; buffer = NULL; SDL_Quit(); } int SDL_main(int argc,char* argv[]) { init(); // set background to white for(int y = 0; y < HEIGHT; y++){ for(int x = 0; x < WIDTH; x++){ setPixel(x,y,255,255,255); } } Coordinate* coords[100]; // draw line for(int i = 0; i < 100; i++){ coords[i] = new Coordinate("y=3x+10",i); coords[i]->calculateY(); setPixel(coords[i]->x,coords[i]->y,50,120,140); } while(true){ SDL_PollEvent(&event); if(event.type == SDL_QUIT){ break; } render(); } cleanUp(); }``````

thanks
Last edited on
** edit

they do seem to change but I will only notice the difference with the equation

y=0x+0 this seems to create a horizontal straight line as expected but apart from that the change isn't what I was expecting
¿does your parser work? ¿do you retrieve the slope and independent term correctly?
after that, ¿is the computation for different `x' values correct?

your `.calculateY()' function is doing too many things
 ``123456`` ``````struct equation{ double slope, intercept; double calculateY(double x){ return x*slope + intercept; } };``````
where the member variables may be obtained with the parser

After that, check out the graphics
you may do a simple loop
 ``12`` ``````for(int K=0; K

Then integrate both parts.

> when y gets much larger the points are far more spread out
you have slope >= 1, so you need to assure that `y' increase by 1 pixel in order to obtain continuity.
Topic archived. No new replies allowed.