mouse input for x and y point to create a line using line equation algorithm (open gl)

I need to create line using the line equation algorithm where the value of x1,y1 and x2,y2 is via mouse input. here's my long long coding -

#include <stdio.h>;
#include <GL/glut.h>;

int counter;
int x1, x2;
int y1, y2;

void setPixel(int x, int y)
{
    float m;
    float b;
    float x3, y3;

    x3 = x2-x1;
    y3 = y2-y1;

    if(counter==1)
    {
        glPointSize(9.0f);
        glBegin(GL_POINTS);

        m = y3 / x3;

        while(x1 <= x2)
        {
            b =  y - (m*x2);
            y = (m*x1)+b;
            y = 400-y;
            glVertex2i(x1,y);
            x1++;

        }

        glEnd();
        glFlush();
    }

}
void myMouse(int button, int state, int x, int y)
{

    if(button ==  GLUT_LEFT_BUTTON && state == GLUT_DOWN )
    {

        setPixel(x,y);
        counter++;

        if(counter==0)
        { 
            x1 = x;
            y1 = y;

        }
        if(counter==1)

        { 
            x2 = x;
            y2 = y;

        }
    }
}

void myKeyboardAct(unsigned char c, int x, int y)
{
    switch(c)
    {
        case 's' : //show coordinates' x and y

            printf("what where and what are x and y %d %d \n" ,x,y);
            printf("what where and what are x and y %d %d \n" ,x2,y2);

            break;

        case 'e' : //flush and draw

            glFlush();
            break;
    }
}

void myDisplay(void)
{
    glClearColor(0,0,0,0);
    glColor3f(200.0,0.00,0);
    gluOrtho2D(-300.0,300.0,-200.0,200.0);
    //gluOrtho2D(0.0,600.0,400.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glutMouseFunc(myMouse);

    glFlush();

}

int main(int argc, char** argv)
{
    glutInitWindowSize(600,400);
    //glutInitWindowSize(1024,800);
    glutInitWindowPosition(100,100);
    glutCreateWindow("HELLO THERE YOU CAN JUST DRAW AWAYYYY DRAW       AWAAAAAYYYYYYYYYYYYYYYYYYYY");
    glutDisplayFunc(myDisplay);
    glutKeyboardFunc(myKeyboardAct);
    glutMainLoop ();
    return 0;
}

however, the problem ive encountered is that the line seems to appear a few points above or a few points below from where i've clicked. another thing is that if the gradient is negative, the line will not appear.

Answers


first test if you obtain the correct coordinates from mouse.

  • so draw some cross on those points
  • so you actually see if there is not some offset between mouse and cross.
  • try this also after you resize your window (unless it is full-screen).

You call setPixel() before you copy mouse position to your points !!! try it like this:

void myMouse(int button, int state, int x, int y)
 {
 if(button ==  GLUT_LEFT_BUTTON && state == GLUT_DOWN )
  {
  if(counter==0) { x1=x; y1=y; counter++; }
  if(counter==1) { x2=x; y2=y; }      
  setPixel(x,y);
  }
 }

Your Draw line has many bugs, it is an assigment so I will not repair it just give you hints

  • why to call it setPixel(x,y) ? when it draws a line
    • also what is x,y when you use x1,y1,x2,y2 as input points
    • this is why your line is bugged !!!
  • why x3,y3 not call dx,dy so it is more readable for others?
  • you are destroying x1 !!!
    • therefore after another click/setPixel call you will lost your line
  • you always use x as main axis (if |dx|<|dy| then you will have holes in your line)
  • you always increment x1++ what if x1>x2 ?
    • either swap (x1,y1) with (x2,y2)
    • or instead of x1++ use x1+=dir ... and set direction to +/-1 according to the x1 < x2
  • you compute b constant in each pixel why ? move it before loop
  • why y=400-y ?
    • should not you use screen y-size instead ?
    • if your OpenGL screen height is not 400px then it will generate offset !!!

Need Your Help

warning: passing argument 1 of ‘gets’ makes pointer from integer without a cast

c

I have read other posts that get the "makes pointer from integer w/o cast" error but I am confused about exactly what it means. Is it saying that the function gets makes the int variable input a po...

How to implement the incremental upgrade of application in android

java android

How to implement the incremental upgrade of application in android ?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.