OpenGL midpoint Bresenham draws a straight line algorithm

  • 2020-07-21 09:21:20
  • OfStack

The example of this paper shares the OpenGL midpoint Bresenham drawing line algorithm for your reference. The specific content is as follows

The 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 convention window must be square 
float height = wid; // Set the size of the window 
int numbers = 20; // Sets the number of grids to divide 
float t = wid/numbers; // Simulate units under pixels 1
/*
  Parameter setting Instructions: 
  Two points of the input line A(x1,y1);B(x2,y2)
  You should ensure that the parameter range is within -400 ~ 400. And is an integer. 
 * Support different slopes 
 * Support two points reversed 
 */
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 part 
  Parameter description: and openGL Existing line function 1 The user is required to provide the starting point of the point ( x1,y1 ) and the end (x2,y2)
  To make it easier to see, we will draw the line under the original pixel. 
  So the coordinates here are going to be -1  ~  1
 */
 int k_kind = 0; //k_kind That's the type of slope. 0 is 0 ~ 1 ; 1 is 1 ~ infinite; 2 is 0 ~ -1 ; 3 It's minus infinity -1
 int d_kind =0; //d_kind Used to represent dy The types of positive and negative. 
 if (x1 > x2) {
 swap(x1,x2);
 swap(y1,y2);
 }
 int dx = abs(x2-x1), dy = abs(y2-y1);
 if (y1 > y2) {// If it's downwards 
 y1 = -y1;
 y2 = -y2;
 d_kind = 1;
 }
 if (dy > dx) { // The slope between 1 ~ infinity, will be regarded as the coordinate transformation (in this case the coordinate transformation). 
 swap(x1, y1);
 swap(x2,y2);
 swap(dx,dy);
 k_kind = 1;
 }
 float d = (dy +dy -dx)*t; // make d Amount for decision making ( Here use d = dx*w*2 Avoid floating point operations )
 float x = x1+0.0,y = y1+0.0;
 draw_point(translater(x),translater(y),k_kind,d_kind); // Drawn under 1 A point 
 while( x < x2){  // In order to x For the step length 
 if (d < 0){
  d += 2*dy*t;
 }
 else{
  d += 2*(dy-dx)*t;
  y += t; // The instructions should be drawn at the top 
 }
 x= x + t;
 draw_point(translater(x),translater(y),k_kind,d_kind); // Drawn under 1 A point 
 }
}
float translater(int x){
 /*
  Function description: convert the coordinates under pixel coordinates to openGL coordinates 
  Parameter description: pass in point pixel coordinates -wid-wid To return to -1 ~ 1 coordinates 
 */
 return x/wid;
}
void draw_point(float x , float y, int k_kind,int d_kind){
 /*
  Function description: Draw the pixel point, here set the size of the point to 7 . 
  The color is blue. 
  Parameter description: floating point number x . y is openGl Coordinate system. kind That's the type of 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 to make it easy to put real pixels pixel To the pixels that we're simulating 
 */
 glClearColor(0, 0, 0, 0);// This is setting the background color , Must be in glclear Before the call 
 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 buffer to ensure that drawing command can be executed 
 bresenham(x1, y1,x2,y2);
}
int main(int argc, char *argv[]) {
 // Initialize the GLUT library
 glutInit(&argc, argv);
 // Initialize the size of the window 
 glutInitWindowSize(700,700);
 glutInitWindowPosition(300,200);
 //  Sets the position where the window appears 
 //glutInitWindowPosition(int x, int y);
 glutInitDisplayMode(GLUT_RGBA);
 glutCreateWindow("class16_hw1");
 glutDisplayFunc(&grid);
 glutMainLoop();
 return 0;

Related articles: