Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3236850
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:40:10+00:00 2026-05-17T17:40:10+00:00

I’m writing a MS paint-like application (I’m not allowed to use opengl primitives). So

  • 0

I’m writing a MS paint-like application (I’m not allowed to use opengl primitives). So far it only draws lines and points. I don’t understand why when rendering a single point on click this code prints it where it should, based on the transformation on the renderPoint function:

void renderPoint(double x, double y)
    {
        W = glutGet(GLUT_WINDOW_WIDTH);
        H = glutGet(GLUT_WINDOW_HEIGHT);

        float X;
        float Y;
        glBegin (GL_POINTS);
            X = (2*x/W) - 1;
            Y = (-2*y/H) + 1;
            glVertex2f (X, Y);
        glEnd ();
        glFlush ();

    }

It doesn’t plot properly when called inside the bresenham function (which I believe it’s correct since I implemented it line by line from the article in wikipedia). Am I doing something wrong in the transformations?

#include "GL/glut.h"

#include <stdio.h>
#include <math.h>
int i;

int mainWindow, subWindow;
int X1, Y1, X2, Y2;
int W = 320, H = 320;
void menuApp (int value)
{
    if (value == 1) printf("Linea\n");
    if (value == 2) printf("Circulo\n");
    if (value == 3) printf("Elipsis\n");
    if (value == 4) exit(0);

}

void crearMenu()
{
    //inicio Creando el menu
    int submenu;
    submenu = glutCreateMenu(menuApp);
    glutAddMenuEntry("Linea", 1);

    glutAddMenuEntry("Elipse",3);
    glutAddMenuEntry("Salir",4);
    glutCreateMenu(menuApp);
    glutAddSubMenu("SubMenu", submenu);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    //fin Creando el menu
}

void renderPoint(void) /*REVISAR ESTO*/
{
    glClear (GL_COLOR_BUFFER_BIT);
    glBegin (GL_POINTS);
        glVertex2f  (-0.98, 0.98);
    glEnd ();
    glFlush ();
}





void renderPoint(double x, double y)
{
    W = glutGet(GLUT_WINDOW_WIDTH);
    H = glutGet(GLUT_WINDOW_HEIGHT);

    float X;
    float Y;
    glBegin (GL_POINTS);
        X = (2*x/W) - 1;
        Y = (-2*y/H) + 1;
        glVertex2f (X, Y);
    glEnd ();
    glFlush ();

}

/*wiki pseudo:

function line(x0, x1, y0, y1) //x1
     boolean steep := abs(y1 - y0) > abs(x1 - x0)//x2
     if steep then//x3
         swap(x0, y0) //x4
         swap(x1, y1) //x5 
     if x0 > x1 then //x6
         swap(x0, x1) //x7
         swap(y0, y1) //x8
     int deltax := x1 - x0 //x9
     int deltay := abs(y1 - y0) //x10
     int error := deltax / 2 //x11
     int ystep //x12
     int y := y0 //x13
     if y0 < y1 then ystep := 1 else ystep := -1 //x14
     for x from x0 to x1 //x15
         if steep then plot(y,x) else plot(x,y) //x16
         error := error - deltay //x17
         if error < 0 then //x18
             y := y + ystep //x19
             error := error + deltax //x20
*/


void bresenham1(GLint x0, GLint y0, GLint x1, GLint y1) //function line(x0, x1, y0, y1)
{


    //double result1 = fabs((double)y1 - y0); //abs(y1 - y0)
    //double result2 = fabs((double)x1 - x0); //abs(x1 - x0)


    int result1 = abs(y1-y0);
    int result2 = abs(x1-x0);

bool steep = (result1 > result2); //boolean steep := abs(y1 - y0) > abs(x1 - x0)

if (steep){ //if steep then

        GLint aux1 = x0; //swap(x0, y0)
        x0=y0;
        y0 = aux1;

        GLint aux2 = x1; // swap (x1,y1)
        x1=y1;
        y1=aux2;

}

if(x0>x1){ // if (x0>x1)
        GLint aux3=x0; //swap(x0,x1)
        x0=x1;
        x1=aux3;

        GLint aux4=y0;//swap(y0,y1)
        y0=y1;
        y1=aux4;

}

int deltax = x1-x0; // deltax = x1-x0
int deltay = abs(y1-y0); //  int deltay := abs(y1 - y0) - revisar 
int error = (deltax / 2); //int error := deltax / 2 
int ystep; // int ystep

int y = y0;  //int y := y0

    if (y0<y1){  //if y0 < y1 then ystep := 1 else ystep := -1 

        ystep=1;

    }

    else {ystep=-1;}

for (int x=x0; x<=x1; x++){ //for x from x0 to x1
  if (steep){  // if steep then plot(y,x) else plot(x,y)

       renderPoint(y,x);
   }
   else {

    renderPoint(x,y);
   }

error = error - deltay; //error := error - deltay

if (error<0) {  //if error < 0 then 
  y = y + ystep; // y := y + ystep
  error = error + deltax; //error := error + deltax

} // end if (error<0)


}// end for from x0 to x1


}// end bresenham










void renderAll (void)
{
    glutSetWindow(mainWindow);
    glutPostRedisplay();
    glutSetWindow(subWindow);
    glutPostRedisplay();
}

void movimiento(int boton, int estado, int x, int y)
{
    if((estado == GLUT_DOWN) && (boton == GLUT_LEFT_BUTTON))//mouse down
    {
        X1 = x; Y1 = y;
        //printf("Down| x:%d, y:%d\n",x,y);

        renderPoint(x,y);
    }
    if((estado == GLUT_UP) && (boton == GLUT_LEFT_BUTTON))//mouse up
    {
        //printf("  Up|x:%d, y:%d\n",x,y);
        X2 = x; Y2 = y;
        //renderLine();
        bresenham1(X1,X2,Y1,Y2);
        //renderPoint(x, y);
    }
}

void MouseMove(int x, int y)
{
    //printf("x:%d  | y:%d\n", x,y);
    X2 = x; Y2 = y;
    //renderLine();
    //bresenham1(X1, Y1, X2, Y2);
}

void teclado(unsigned char key, int x, int y)
{
    if(key == 27)exit(0);
}

void especiales(int key, int x, int y)
{
    if(key == GLUT_KEY_F1) exit(0);
}

static void
key(unsigned char k, int x, int y)
{
  switch (k) {
  case 27:  /* Escape */
    exit(0);
    break;
  default:
    return;
  }
  glutPostRedisplay();
}

int main (int argc, char *argv []) 
{
    i = 0;
    //inicializa las operaciones de OpenGL/GLUT, db cr antes de usar funciones GLUT
    glutInit (&argc, argv);

    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
    glutInitWindowPosition (100, 100);
    glutInitWindowSize (W, H);
    //Crea una ventana  de Opengl
    glutCreateWindow ("tarea");
    glutDisplayFunc (renderPoint);
    glutMouseFunc(movimiento);
    glutKeyboardFunc(teclado);//teclas ASCII
    glutSpecialFunc(especiales);//captura las teclas [f1..f12]

    //glutPassiveMotionFunc(pasivo);
    glutKeyboardFunc(key);
    glutMotionFunc(MouseMove);
    crearMenu();
    glutMainLoop ();


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:40:11+00:00Added an answer on May 17, 2026 at 5:40 pm

    The issue is on bresenham1 being called wrong:

    bresenham1(X1,X2,Y1,Y2);
    

    should be:

    bresenham1(X1,Y1,X2,Y2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.