Results 1 to 7 of 7

Thread: bresneham triangle

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2003
    Posts
    3

    bresneham triangle

    i am attempting to make a triangle using bresenham line drawing algorithm, so far I have the cord for a line but it comes up with errors:
    error C2449: found '{' at file scope (missing function header?)
    error C2059: syntax error : '}'
    Error executing cl.exe.
    Casn anyone help me at least getting the line working and if possible tell me how i will then go on to complete the triangle. here is my code so far... i need to have it done by tonight if possible so im desperate it just refuses to work:

    #include <stdlib.h>
    #include <math.h>

    #include <GL/glut.h>


    int dx,dy;

    int main();

    {
    public void lineBresenham(int x0, int y0, int x1, int y1, Color color) {
    int pix = color.getRGB();
    int dy = y1 - y0;
    int dx = x1 - x0;
    int stepx, stepy;
    if (dy < 0) { dy = -dy; stepy = -1;
    } else { stepy = 1;
    }
    if (dx < 0) { dx = -dx; stepx = -1;
    } else { stepx = 1;
    }
    dy <<= 1; // dy is now 2*dy
    dx <<= 1; // dx is now 2*dx

    raster.setPixel(pix, x0, y0);

    if (dx > dy) {
    int fraction = dy - (dx >> 1); // same as 2*dy - dx
    while (x0 != x1) {
    if (fraction >= 0) {
    y0 += stepy;
    fraction -= dx; // same as fraction -= 2*dx
    }
    x0 += stepx;
    fraction += dy; // same as fraction -= 2*dy
    raster.setPixel(pix, x0, y0);
    }
    } else {
    int fraction = dx - (dy >> 1);
    while (y0 != y1) {
    if (fraction >= 0) {
    x0 += stepx;
    fraction -= dy;
    }
    y0 += stepy;
    fraction += dx;
    raster.setPixel(pix, x0, y0);
    }
    }
    }
    }

  2. #2
    Senior Member Regular Contributor
    Join Date
    Sep 2003
    Posts
    135

    Re: bresneham triangle

    Originally posted by needhelpppp:

    Code :
    <snip>
    int main();
    {
    <snap>
    It should be:

    Code :
    int main()
    {
    Besides, what does has this to do with OpenGL?

  3. #3
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: bresneham triangle

    Below void main(); you have a curly bracer at file scope, as the first error says, which is illegal. Do you really mean to have a semicolon after void main()?

    If you don't mean to have that semicolon there, you will have a function defined within another function, which is also illegal. And you also have the public keyword in an illegal place.

  4. #4
    Senior Member Regular Contributor
    Join Date
    Sep 2003
    Posts
    135

    Re: bresneham triangle

    Oh and why did you put the funtion
    Code :
    public void lineBresenham(int x0, int y0, int x1, int y1, Color color)
    {
    into the main function? What wont work.

    No offense but you should grap yourself a book about C++ and start learning this prior to CG.

    Learn to walk before you start running.

  5. #5
    Junior Member Newbie
    Join Date
    Jan 2003
    Posts
    3

    Re: bresneham triangle

    ok so i see i have gone seriously wrong in attempting this, and it has to do with open gl as as soon as i have the triangle i will go onto rotate and clip and rasterise it and involve keyboard and mouse interaction. However as you can se ei am havin great difficulty starting off with this. I see what your all saying but anyone have any ideas how i should go about it?
    Thanks for all the help!

  6. #6
    Junior Member Newbie
    Join Date
    Jan 2003
    Posts
    3

    Re: bresneham triangle

    I have worked on the code a lil more to incorporate some of the open gl, but i am still having problems compiling it, anyone have any suggestions?
    #include <GL/glut.h>
    #include <stdlib.h>
    #include <math.h>

    void Display();
    void Reshape(int w,int h);
    void Mouse(int button,int state,int x,int y);
    void lineBresenham(int x0, int y0, int x1, int y1, GLfloat *color);

    int main(int argc,char **argv) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB);
    glutInitWindowSize(200,200);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[0]);
    glutDisplayFunc(Display);
    glutReshapeFunc(Reshape);
    glutMouseFunc(Mouse);

    glutMainLoop();
    return 0;
    }

    void lineBresenham(int x0, int y0, int x1, int y1,GLfloat *color)

    {
    int pix = color.getRGB();
    int dy = y1 - y0;
    int dx = x1 - x0;
    int stepx, stepy;
    if (dy < 0) { dy = -dy; stepy = -1;
    } else { stepy = 1;
    }
    if (dx < 0) { dx = -dx; stepx = -1;
    } else { stepx = 1;
    }
    dy <<= 1; // dy is now 2*dy
    dx <<= 1; // dx is now 2*dx

    raster.setPixel(pix, x0, y0);

    if (dx > dy) {
    int fraction = dy - (dx >> 1); // same as 2*dy - dx
    while (x0 != x1) {
    if (fraction >= 0) {
    y0 += stepy;
    fraction -= dx; // same as fraction -= 2*dx
    }
    x0 += stepx;
    fraction += dy; // same as fraction -= 2*dy
    raster.setPixel(pix, x0, y0);
    }
    } else {
    int fraction = dx - (dy >> 1);
    while (y0 != y1) {
    if (fraction >= 0) {
    x0 += stepx;
    fraction -= dy;
    }
    y0 += stepy;
    fraction += dx;
    raster.setPixel(pix, x0, y0);

    }

    void Display() {
    GLfloat color[]={1.0,1.0,1.0,1.0};

    glClear(GL_COLOR_BUFFER_BIT);
    /* Make calls to lineBresenham to draw the triangle(s) and/or line(s) */
    lineBresenham(10,10,300,300,color);

    glFlush();
    }

    void Reshape(int w,int h) {
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0,w,0,h);
    glMatrixMode(GL_MODELVIEW);
    }

    void Mouse(int button,int state,int x,int y) {
    if(state!=GLUT_DOWN)
    return;
    exit(0); /* Exit on mouse click */
    }

  7. #7
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: bresneham triangle

    You have to give us more details. If you excpect us to help, at least say what's wrong. "Doesn't compile" is not a description of your problem. What is the error message, on what line does it occcur.

    With a decent compiler, you should be able to figure out what the error is. Ofter when you don't understand the error, it's because you don't understand the language.

    Here's one error, I leave it up to you to figure out what's wrong. If you know the basics of C, you should see it directly.
    Code :
    GLfloat *color
    ...
    int pix = color.getRGB();

Similar Threads

  1. An alternative to triangle strips and triangle fans
    By vincoof in forum OpenGL: General
    Replies: 8
    Last Post: 10-26-2012, 04:04 AM
  2. Replies: 3
    Last Post: 10-09-2012, 09:39 PM
  3. triangle strip with a LINE triangle display error
    By Samme in forum OpenGL: Windows
    Replies: 22
    Last Post: 11-20-2008, 07:50 PM
  4. OT: fastest ray-triangle line -triangle intersection algo
    By V-man in forum OpenGL: Advanced Coding
    Replies: 23
    Last Post: 08-28-2002, 02:18 AM
  5. How to check triangle-triangle intersection?
    By alexlcm in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 02-18-2001, 08:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean