OpenGL midpoint Bresenham line drawing algorithm

Time:2021-11-24

This example shares the OpenGL midpoint Bresenham line drawing algorithm for your reference. The specific contents are as follows

environment

MacOS Xcode compiler

code

#include <GLUT/GLUT.h>
#include <iostream>
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
float wid = 400;  // Set the size of the window. The agreed window must be square
float height = wid; // Sets the size of the window
int numbers = 20; // Sets the number of meshes divided
float t = wid/numbers; // Unit under analog pixel 1
/*
 Parameter setting Description:
 Enter two points a (x1, Y1) of the straight line; B(x2,y2)
 You should ensure that the parameters range from - 400 to 400. And are integers.
 *Support different slopes
 *Support two-point position reversal
 */
int x1 = -300,y1=-400,x2 =400,y2 = 100;
void draw_point(float x, float y,int k_kind,int d_kind);
float translater(int x);
void swap(int &a, int &b)
{ int tmp = 0;
 tmp = b;
 b = a;
 a = tmp; }
void bresenham(int x1, int y1,int x2, int y2){
 /*
 Function Description: Bresenham algorithm
 Parameter Description: like the existing scribing function in OpenGL, the user is required to provide the starting point (x1, Y1) and ending point (X2, Y2) of the point
 For easy observation, we will draw the line under the original pixel.
 The coordinate requirements here are - 1 ~ 1
 */
 int k_ kind = 0; // k_ Kind is used to indicate the type of slope. 0 is 0 ~ 1; 1 is 1 ~ infinity; 2 is 0 ~ - 1; 3 is negative infinity ~ - 1
 int d_ kind =0; // d_ Kind is used to represent the positive and negative type of dy.
 if (x1 > x2) {
 swap(x1,x2);
 swap(y1,y2);
 }
 int dx = abs(x2-x1), dy = abs(y2-y1);
 If (Y1 > Y2) {// if it is downward
 y1 = -y1;
 y2 = -y2;
 d_kind = 1;
 }
 If (Dy > DX) {// if the slope is between 1 and infinity, it will be regarded as coordinate system transformation (coordinate transformation here).
 swap(x1, y1);
 swap(x2,y2);
 swap(dx,dy);
 k_kind = 1;
 }
 float d = (dy +dy -dx)*t; // Let d be the decision quantity (here, d = DX * w * 2 is used to avoid floating-point operation)
 float x = x1+0.0,y = y1+0.0;
 draw_ point(translater(x),translater(y),k_kind,d_kind); // Draw the next point
 While (x < x2) {// in steps of X
 if (d < 0){
  d += 2*dy*t;
 }
 else{
  d += 2*(dy-dx)*t;
  y += t; // The description should be drawn in the position above
 }
 x= x + t;
 draw_ point(translater(x),translater(y),k_kind,d_kind); // Draw the next point
 }
}
float translater(int x){
 /*
 Function Description: convert the coordinates under pixel coordinates into OpenGL coordinates
 Parameter Description: the pixel coordinates of the incoming point are - wid wid, and the - 1 ~ 1 coordinates are returned
 */
 return x/wid;
}
void draw_point(float x , float y, int k_kind,int d_kind){
 /*
 Function Description: draw the point of the pixel. Here, set the size of the point to 7.
 The color is blue.
 Parameter Description: floating point numbers x and y are OpenGL coordinate system. Kind is the type indicating the slope
 */
 glPointSize(7);
 glColor3f(0.0,0.0,1.0);
 glBegin(GL_POINTS);
 cout <<"k:"<<k_kind<<"d:" << d_kind << endl;
 if(k_kind==0&&d_kind==1){
 y = -y;
 }else if (k_kind ==1 &&d_kind==1){
 x= -x;
 swap(x,y);
 }else if (k_kind==1&&d_kind ==0){
 swap(x,y);
 }
 glVertex3f(x,y,0.0);
 glEnd();
 glFlush();
}
void grid(){
 /*
 Function Description: draw the grid in order to convert the real pixel pixel into our simulated pixel
 */
 glClearColor(0, 0, 0, 0);// This is setting background color and must be called before glclear.
 glClear(GL_COLOR_BUFFER_BIT);
 //Draw a straight line
 int wid_number = numbers;
 int hei_number = numbers;
 float delta_wid = wid / wid_number;
 float delta_hei = height / hei_number;
 glColor3f(1.0,1.0,0);
 for (int i = 1; i < 40 ; i ++ ) {
 glBegin(GL_LINES);
 glVertex2f(-1+i*delta_hei/height, -1);
 glVertex2f(-1+i*delta_hei/height, 1);
 glVertex2f(-1,-1+i*delta_hei/height);
 glVertex2f(1,-1+i*delta_hei/height);
 glEnd();
 glFlush();
 }
 glColor3f(1.0,0,0);
 glBegin(GL_LINES); // Draw the coordinate system for easy observation
 glVertex2f(-1,0);
 glVertex2f(1,0);
 glVertex2f(0,-1);
 glVertex2f(0,1);
 glEnd();
 glFlush();
 glBegin(GL_LINES);
 glColor3f(1.0,0.0,0.0);
 glVertex2f(translater(x1),translater(y1)); // Fixed point coordinate range
 glVertex2f(translater(x2),translater(y2));
 glEnd();
 glFlush();
 //Refresh the buffer to ensure that the drawing command can be executed
 bresenham(x1, y1,x2,y2);
}
int main(int argc, char *argv[]) {
 //Initialize glut Library
 glutInit(&argc, argv);
 //Initialize the size of the window
 glutInitWindowSize(700,700);
 glutInitWindowPosition(300,200);
 //Sets where the window appears
 //glutInitWindowPosition(int x, int y);
 glutInitDisplayMode(GLUT_RGBA);
 glutCreateWindow("class16_hw1");
 glutDisplayFunc(&grid);
 glutMainLoop();
 return 0;

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.