Line and Curve Drawing Algorithms

Slides:



Advertisements
Similar presentations
OpenGL Open a Win32 Console Application in Microsoft Visual C++.
Advertisements

OpenGL CMSC340 3D Character Design & Animation. What is OpenGL? A low-level graphics library specification designed for use with the C and C++ provides…
OPEN GL. Install GLUT Download package di sini Dari devcpp, buka Tools->PackageManager-
Line and Curve Drawing Algorithms. Line Drawing x0x0 y0y0 x end y end.
CS 376 Introduction to Computer Graphics 02 / 02 / 2007 Instructor: Michael Eckmann.
CS 450: COMPUTER GRAPHICS REVIEW: DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
Lecture 5 Rendering lines 1.Steps of line rendering 2.Scan-conversion for line segments 3.A1 tutorial CP411 Computer Graphics Fall 2007 Wilfrid Laurier.
Chapter 2: Graphics Programming
CS 376 Introduction to Computer Graphics 01 / 29 / 2007 Instructor: Michael Eckmann.
OPENGL.
#include int line_width = 1; void Display( void ) { glEnable( GL_LINE_STIPPLE ); glClearColor (0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT);
OpenGL (Graphics Library) Software Interface to graphics software Allows to create interactive programs that produce color images of moving 3D objects.
Expanded by Jozef Goetz Chapter 3 Graphics Output Primitives Some slides adapted from Benjamin Lok, Ilmi Yoon and Falko Kuester.
OpenGL (I). What is OpenGL (OGL)? OGL is a 3D graphics & modeling library Can also use it to draw 2D objects.
1 Angel: Interactive Computer Graphics 4E © Addison-Wesley 2005 Programming with OpenGL Part 2: Complete Programs Ed Angel Professor of Computer Science,
Ellipses Using OpenGL.
ITEPC 06 - Workshop on Fractal Creation Chiew-Lan Tai and Oscar Au.
Line Drawing by Algorithm. Line Drawing Algorithms Line drawn as pixels Graphics system –Projects the endpoints to their pixel locations in the frame.
Computer Graphics CS 385 February 7, Fundamentals of OpenGl and Glut Today we will go through the basics of a minimal OpenGl Glut project, explaining.
CS 450: COMPUTER GRAPHICS DRAWING LINES AND CIRCLES SPRING 2015 DR. MICHAEL J. REALE.
CAP 4703 Computer Graphic Methods Prof. Roy Levow Lecture 2.
Drawing Basic Graphics Primitives Lecture 4 Wed, Sep 3, 2003.
CS 325 Introduction to Computer Graphics 02 / 01 / 2010 Instructor: Michael Eckmann.
Computer Graphics CS 385 January 31, Fractals Some definitions Object which is self-similar at all scales. Regardless of scale the same level of.
Introduction to GL Geb Thomas. Example Code int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Graphics Output Primitives Hearn & Baker Chapter 3
CA 302 Computer Graphics and Visual Programming Lecture 2: Introduction to OpenGL Aydın Öztürk
Chun-Yuan Lin Introduction to OpenGL 2015/12/19 1 CG.
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
GEOMETRY AND LINE GENERATION Geometry and Line Generation Chapter 2.
NoufNaief.net 1 TA: Nouf Al-Harbi.
OpenGL Basic Drawing 2003 Spring Keng Shih-Ling
CS552: Computer Graphics Lecture 6: Viewing in 2D.
1 Programming with OpenGL Part 2: Complete Programs.
OpenGL API 2D Graphic Primitives Angel Angel: Interactive Computer Graphics5E © Addison-Wesley
OpenGL: Introduction #include main() { OpenWindow() ; glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0,
OpenGL Basic Drawing Jian-Liang Lin A Smidgen of OpenGL Code #include main() { InitializeAWindowPlease(); glClearColor (0.0, 0.0, 0.0, 0.0); glClear.
Introduction to OpenGL Muhammad Aamir Khan Lecturer, DCS, UOP.
Introduction to Graphics Programming. Graphics API.
Computer Graphics I, Fall Programming with OpenGL Part 2: Complete Programs.
10/10/2006TCSS458A Isabelle Bichindaritz1 Line and Circle Drawing Algorithms.
INTRODUCTION TO OPENGL
Computer Graphics (Fall 2003) COMS 4160, Lecture 5: OpenGL 1 Ravi Ramamoorthi Many slides courtesy Greg Humphreys.
Rasterization CENG 477 Introduction to Computer Graphics Slides from Steve Marschner, CS4620, 2008 Cornell University.
1 Computer Graphics –Lect 03 Graphics Output Primitives Chapter – 3 of Hearn & Baker using OPENGL.
Primitive graphic objects
Program Studi S-1 Teknik Informatika FMIPA Universitas Padjadjaran
Introduction to OpenGL (IDE: Eclipse)
Introduction to OpenGL
Chapter 3 Computer Graphics Software
Chapter Three Part I Output Primitives CS 380.
Programming with OpenGL Part 2: Complete Programs
Materi Anatomi OpenGL Fungsi GLUT Posisi Kamera Proyeksi
OpenGL API 2D Graphic Primitives
Programming with OpenGL Part 2: Complete Programs
Graphics Programming (I)
OpenGL (Open Graphics Library) Mr. B.A.Swamy Assistant Professor Dept of CSE.
Lab 3 Geometric Drawing Lab 3 Geometric Drawing.
גרפיקה ממוחשבת: מבוא ל-OpenGL
Coordinate Systems and Transforming the Coordinates
Introduction to OpenGL
OpenGL program.
CSE 411 Computer Graphics Lecture #4 Attributes of Output Primitives and Algorithms Prepared & Presented by Asst. Prof. Dr. Samsun M. BAŞARICI 1.
Programming with OpenGL Part 2: Complete Programs
Programming with OpenGL Part 2: Complete Programs
Scan Conversion (From geometry to pixels)
Chapter 3 Graphics Output Primitives
Primitive Drawing Algorithm
Primitive Drawing Algorithm
Programming with OpenGL Part 2: Complete Programs
Presentation transcript:

Line and Curve Drawing Algorithms

Line Drawing |m|<1 y = m . x + b m = (yend – y0) / (xend – x0) b = y0 – m . x0 yend y0 x0 xend

Line Drawing |m|>1 x = (y – b)/m m = (yend – y0) / (xend – x0) b = y0 – m . x0 yend y0 x0 xend

DDA Algorithm if |m|<1 xk+1 = xk + 1 yk+1 = yk + m if |m|>1 xk+1 = xk + 1/m yend y0 x0 xend yend y0 x0 xend

DDA Algorithm #include <stdlib.h> #include <math.h> inline int round (const float a) { return int (a + 0.5); } void lineDDA (int x0, int y0, int xEnd, int yEnd) { int dx = xEnd - x0, dy = yEnd - y0, steps, k; float xIncrement, yIncrement, x = x0, y = y0; if (fabs (dx) > fabs (dy)) steps = fabs (dx); /* |m|<1 */ else steps = fabs (dy); /* |m|>=1 */ xIncrement = float (dx) / float (steps); yIncrement = float (dy) / float (steps); setPixel (round (x), round (y)); for (k = 0; k < steps; k++) { x += xIncrement; y += yIncrement; }

Bresenham’s Line Algorithm yk+1 yk xk xk+1 yk+1 du y dl yk xk xk+1

Bresenham’s Line Algorithm #include <stdlib.h> #include <math.h> /* Bresenham line-drawing procedure for |m|<1.0 */ 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; /* Determine which endpoint to use as start position. */ if (x0 > xEnd) { x = xEnd; y = yEnd; xEnd = x0; } else { x = x0; y = y0; } setPixel (x, y); while (x < xEnd) { x++; if (p < 0) p += twoDy; y++; p += twoDyMinusDx;

Circle Drawing Pythagorean Theorem: x2 + y2 = r2 (x-xc)2 + (y-yc)2 = r2 (xc-r) ≤ x ≤ (xc+r) y = yc ± √r2 - (x-xc)2 (x, y) r yc xc

Circle Drawing change x change y

Circle Drawing using polar coordinates x = xc + r . cos θ y = yc + r . sin θ change θ with step size 1/r (x, y) r θ (xc, yc)

Circle Drawing using polar coordinates x = xc + r . cos θ y = yc + r . sin θ change θ with step size 1/r use symmetry if θ>450 (x, y) r θ (xc, yc) (y, -x) (y, x) (-x, y) (x, y) 450 (xc, yc)

Midpoint Circle Algorithm f(x,y) = x2 + y2 - r2 <0 if (x,y) is inside circle f(x,y) =0 if (x,y) is on the circle >0 if (x,y) is outside circle use symmetry if x>y yk yk-1/2 yk-1 xk xk+1

Midpoint Circle Algorithm #include <GL/glut.h> class scrPt { public: GLint x, y; }; void setPixel (GLint x, GLint y) { glBegin (GL_POINTS); glVertex2i (x, y); glEnd ( ); } void circleMidpoint (scrPt circCtr, GLint radius) scrPt circPt; GLint p = 1 - radius; circPt.x = 0; circPt.y = radius; void circlePlotPoints (scrPt, scrPt); /* Plot the initial point in each circle quadrant. */ circlePlotPoints (circCtr, circPt); /* Calculate next points and plot in each octant. */ while (circPt.x < circPt.y) { circPt.x++; if (p < 0) p += 2 * circPt.x + 1; else { circPt.y--; p += 2 * (circPt.x - circPt.y) + 1; } circlePlotPoints (circCtr, circPt); void circlePlotPoints (scrPt circCtr, scrPt circPt); { setPixel (circCtr.x + circPt.x, circCtr.y + circPt.y); setPixel (circCtr.x - circPt.x, circCtr.y + circPt.y); setPixel (circCtr.x + circPt.x, circCtr.y - circPt.y); setPixel (circCtr.x - circPt.x, circCtr.y - circPt.y); setPixel (circCtr.x + circPt.y, circCtr.y + circPt.x); setPixel (circCtr.x - circPt.y, circCtr.y + circPt.x); setPixel (circCtr.x + circPt.y, circCtr.y - circPt.x); setPixel (circCtr.x - circPt.y, circCtr.y - circPt.x);

OpenGL #include <GL/glut.h> // (or others, depending on the system in use) void init (void) { glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glMatrixMode (GL_PROJECTION); // Set projection parameters. gluOrtho2D (0.0, 200.0, 0.0, 150.0); } void lineSegment (void) glClear (GL_COLOR_BUFFER_BIT); // Clear display window. glColor3f (0.0, 0.0, 1.0); // Set line segment color to blue. glBegin (GL_LINES); glVertex2i (180, 15); // Specify line-segment geometry. glVertex2i (10, 145); glEnd ( ); glFlush ( ); // Process all OpenGL routines as quickly as possible. void main (int argc, char** argv) glutInit (&argc, argv); // Initialize GLUT. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode. glutInitWindowPosition (50, 100); // Set top-left display-window position. glutInitWindowSize (400, 300); // Set display-window width and height. glutCreateWindow ("An Example OpenGL Program"); // Create display window. init ( ); // Execute initialization procedure. glutDisplayFunc (lineSegment); // Send graphics to display window. glutMainLoop ( ); // Display everything and wait.

OpenGL Point Functions glVertex*( ); * : 2, 3, 4 i (integer) s (short) f (float) d (double) Ex: glBegin(GL_POINTS); glVertex2i(50, 100); glEnd(); int p1[ ]={50, 100}; glBegin(GL_POINTS); glVertex2iv(p1); glEnd();

OpenGL Line Functions Ex: GL_LINES GL_LINE_STRIP GL_LINE_LOOP glBegin(GL_LINES); glVertex2iv(p1); glVertex2iv(p2); glEnd();

OpenGL glBegin(GL_LINES); GL_LINES GL_LINE_STRIP glVertex2iv(p1); glEnd(); GL_LINE_LOOP p3 p3 p5 p1 p1 p2 p4 p2 p4 p3 p5 p1 p2 p4

Antialiasing No Antialiasing Ideal With Antialiasing

Antialiasing No Antialiasing With Antialiasing

Antialiasing Supersampling Count the number of subpixels that overlap the line path. Set the intensity proportional to this count.

Antialiasing Supersampling 1 2 1 3x3 Virtual Pixels 2 4 2 1 2 1 (255, 159, 159) 1 2 1 (255,255,255) (255,0,0) Actual Screen Pixels Example

Antialiasing Area Sampling Line is treated as a rectangle. Calculate the overlap areas for pixels. Set intensity proportional to the overlap areas. 80% 25%

Antialiasing Pixel Sampling Micropositioning Electron beam is shifted 1/2, 1/4, 3/4 of a pixel diameter.

Line Intensity differences Change the line drawing algorithm: For horizontal and vertical lines use the lowest intensity For 45o lines use the highest intensity