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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <cctype>
#include <iostream>
#include <cstring>
#include <sstream>
#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;
    }



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104


#include <iostream>
#include <SDL2/SDL.h>
#include <stdio.h>
#include <string.h>
#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
consider instead
1
2
3
4
5
6
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
1
2
for(int K=0; K<WIDTH); ++K)
	setPixel(K, 3.14*K + 2.79, 0, 255, 0);


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.