My homework on computer graphics is to implement OpenGL algorithms using only the ability to draw dots.
So obviously, I need to get drawLine() to work before I can draw anything else. drawLine() should only be executed using integers. No floating point.
This is what I was taught. In principle, the lines can be divided into 4 different categories, positive steep, positive small, negative steep and negative small. This is the picture I have to draw:

and this is the image that my program draws:

Colors are made for us. We are given the vertices, and we need to use the Bresenham Line algorithm to draw lines based on the start and end points.
This is what I still have:
int dx = end.x - start.x; int dy = end.y - start.y; //initialize varibales int d; int dL; int dU; if (dy > 0){ if (dy > dx){ //+steep d = dy - 2*dx; dL = -2*dx; dU = 2*dy - 2*dx; for (int x = start.x, y = start.y; y <= end.y; y++){ Vertex v(x,y); drawPoint(v); if (d >= 1){ d += dL; }else{ x++; d += dU; } } } else { //+shallow d = 2*dy - dx; dL = 2*dy; dU = 2*dy - 2*dx; for (int x = start.x, y = start.y; x <= end.x; x++) { Vertex v(x,y); drawPoint(v); // if choosing L, next y will stay the same, we only need // to update d by dL if (d <= 0) { d += dL; // otherwise choose U, y moves up 1 } else { y++; d += dU; } } } } else { if (-dy > dx){ cout << "-steep\n"; //-steep d = dy - 2*dx; //south dL = 2*dx; //southeast dU = 2*dy - 2*dx; for (int x = start.x, y = start.y; y >= end.y; --y){ Vertex v(x,y); drawPoint(v); //if choosing L, next x will stay the same, we only need //to update d if (d >= 1){ d -= dL; } else { x++; d -= dU; } } } else { cout << "-shallow\n"; //-shallow d = 2*dy - dx; dL = 2*dy; dU = 2*dy - 2*dx; for (int x = start.x, y = start.y; x <= end.x; x++){ Vertex v(x,y); drawPoint(v); if (d >= 0){ d += dL; } else { --y; d -= dU; } } } }
I know that my mistake will be something stupid, but I honestly cannot understand what I'm doing wrong. Why are some of the lines not drawn correctly as shown above?
c ++ graphics bresenham
Toastymallows
source share