1 / 13

Bresenham’s Line algorithm

Bresenham’s Line algorithm. #include<stdlib.h> #include<math.h> Int round( const float a) { Return int(a+0.5); }. Void lineBres(int x0,int y0,int xEnd,int yEnd) { Int dx=fabs(xEnd-x0),dy=fabs(yEnd-y0); Int p=2*dy –dx; Int twoDy=2*dy, twoDyMinusDx=2*(dy-dx); Int x,y;. If(x0>xEnd)

nituna
Download Presentation

Bresenham’s Line algorithm

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Bresenham’s Line algorithm

  2. #include<stdlib.h> #include<math.h> Int round( const float a) { Return int(a+0.5); }

  3. Void lineBres(int x0,int y0,int xEnd,int yEnd) { Int dx=fabs(xEnd-x0),dy=fabs(yEnd-y0); Int p=2*dy –dx; Int twoDy=2*dy, twoDyMinusDx=2*(dy-dx); Int x,y;

  4. If(x0>xEnd) { x=xEnd; Y=yEnd; xEnd=x0; } Else{ x=x0; Y=y0; } SetPixel(x,y);

  5. While(x<xEnd) { X++; If(p<0) P+=twoDy; Else{ Y++; P+=twoDyMinusDx; } SetPixel(x,y); } }

  6. Mid Point Circle Algorithm

  7. #include<iostream.h> #include<GL/glut.h> #include<GL/gl.h>

  8. class screenPt{ private: GLint x,y; public: screenPt(){ x=y=0; } void setCoords (GLint xCoordValue,GLint yCoordValue){ x=xCoordValue; y=yCoordValue; } GLint getx() const{ return x; } GLint gety() const{ return y; } void incrementx(){ x++; } void decrementy(){ y--; } };

  9. void setPixel(GLintxCoord,GLintyCoord){ glBegin(GL_POINTS); glVertex2i(xCoord,yCoord); glEnd(); } void circleMidpoint(GLintxc,GLintyc,GLint radius){ screenPtcircPt; GLint p=1-radius; circPt.setCoords(0,radius); void circlePlotPoints(GLint,GLint,screenPt); circlePlotPoints(xc,yc,circPt); while(circPt.getx()<circPt.gety()){ circPt.incrementx(); if(p<0) p+=2*circPt.getx()+1; else { circPt.decrementy(); p+=2*(circPt.getx()-circPt.gety())+1; } circlePlotPoints(xc,yc,circPt); } }

  10. void circlePlotPoints(GLintxc,GLintyc,screenPtcircPt){ setPixel(xc+circPt.getx(),yc+circPt.gety()); setPixel(xc-circPt.getx(),yc+circPt.gety()); setPixel(xc+circPt.getx(),yc-circPt.gety()); setPixel(xc-circPt.getx(),yc-circPt.gety()); setPixel(xc+circPt.gety(),yc+circPt.getx()); setPixel(xc-circPt.gety(),yc+circPt.getx()); setPixel(xc+circPt.gety(),yc-circPt.getx()); setPixel(xc-circPt.gety(),yc-circPt.getx()); }

  11. void init (void) { glClearColor(1.0,1.0,1.0,0.0); // set display window to white glMatrixMode(GL_PROJECTION); // set projection parameter gluOrtho2D(0.0,500.0,0.0,500.0); } void lineSegment(void) { glClear(GL_COLOR_BUFFER_BIT); // clear display window glColor3f(1.0,0.5,0.7); // set line segment color red circleMidpoint(50,50,40); glFlush(); // process all openGL routines as quickly as possible }

  12. void main(intargc,char** argv) { glutInit(&argc,argv); // initialize GLUT glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set top-left display-window posision glutInitWindowPosition(50,100); //set top left display windowposition glutInitWindowSize(500,500); // set dosplay window width and height glutCreateWindow("an example OpenGL program"); //create display window //create display window init(); // execute initialization-procedure glutDisplayFunc(lineSegment); // send graphics to display window glutMainLoop(); // display every thing and wait }

More Related