PDA

View Full Version : bresneham triangle

needhelpppp
01-07-2003, 10:53 AM
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);
}
}
}
}

1234!
01-07-2003, 11:14 AM
Originally posted by needhelpppp:

<snip>
int main();
{
<snap>

It should be:

int main()
{

Besides, what does has this to do with OpenGL?

Bob
01-07-2003, 11:15 AM
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.

1234!
01-07-2003, 11:19 AM
Oh and why did you put the funtion

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.

needhelpppp
01-07-2003, 11:23 AM
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!

needhelpppp
01-07-2003, 04:39 PM
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);
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 */
}

Bob
01-08-2003, 12:50 AM
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.

GLfloat *color
...
int pix = color.getRGB();