Michener’s Algorithm An efficient scheme for drawing circles (and filling circular disks) on a raster graphics display.

Slides:



Advertisements
Similar presentations
Linux Serial Programming for POSIX Operating Systems
Advertisements

Computer Graphics 4: Bresenham Line Drawing Algorithm, Circle Drawing & Polygon Filling By:Kanwarjeet Singh.
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.
Line Drawing Algorithms. Rasterization-Process of determining which pixels give the best approximation to a desired line on the screen. Scan Conversion-Digitizing.
Larry F. Hodges (modified by Amos Johnson) 1 Design of Line, Circle & Ellipse Algorithms.
CS 352: Computer Graphics Chapter 7: The Rendering Pipeline.
Graphics Pipeline.
Terminal I/O POSIX termios Two terminal I/O modes Canonical and non-canonical Getting and setting terminal attributes Terminal window size: struct winsize.
Raster conversion algorithms for line and circle
Output Primitives Computer Graphics.
Crafting a ‘demo’ program A ‘walk-through’ of the program development cycle for an example in assembly language.
Drawing Lines The Bresenham Algorithm for drawing lines and filling polygons.
A code-walkthrough Commentary on our ‘wfmodel.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations.
CS5500 Computer Graphics © Chun-Fa Chang, Spring 2007 CS5500 Computer Graphics May 3, 2007.
Terminal Control operating systems. Terminal Control may seem like an arcane subject, but … It illustrates the relationship between devices and files.
Michener’s Algorithm An efficient scheme for drawing circles (and filling circular disks) on a raster graphics display.
Dynamic visualizations On ‘non-canonical’ keyboard-input and terminal escape-sequences for visualization effects.
A code-walkthrough Commentary on our ‘model3d.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations.
Course Website: Computer Graphics 5: Line Drawing Algorithms.
Drawing Lines The Bresenham Algorithm for drawing lines and filling polygons.
1 King ABDUL AZIZ University Faculty Of Computing and Information Technology CS 454 Computer graphicsIntroduction Dr. Eng. Farag Elnagahy
Ch 1 Intro to Graphics page 1CS 367 First Day Agenda Best course you have ever had (survey) Info Cards Name, , Nickname C / C++ experience, EOS experience.
Linux game programming An introduction to the use of interval timers and asynchronous input notifications.
Linux game programming An introduction to the use of interval timers and asynchronous input notifications.
Circle Drawing algo..
Tanenbaum & Woodhull, Operating Systems: Design and Implementation, (c) 2006 Prentice-Hall, Inc. All rights reserved OPERATING SYSTEMS DESIGN.
Basics of a Computer Graphics System Introduction to Computer Graphics CSE 470/598 Arizona State University Dianne Hansford.
Rasterizing primitives: know where to draw the line Dr Nicolas Holzschuch University of Cape Town Modified.
1/1/20001 Topic >>>> Scan Conversion CSE Computer Graphics.
Scan Conversion. Also known as rasterization In our programs objects are represented symbolically –3D coordinates representing an object’s position –Attributes.
1 CS 430/536 Computer Graphics I Circle Drawing and Clipping Week 3, Lecture 6 David Breen, William Regli and Maxim Peysakhov Geometric and Intelligent.
Dr. S.M. Malaek Assistant: M. Younesi
Jehee Lee Seoul National University
WHERE TO DRAW A LINE?? Line drawing is accomplished by calculating intermediate positions along the line path between specified end points. Precise definition.
Informationsteknologi Monday, November 26, 2007Computer Graphics - Class 121 Today’s class Drawing lines Bresenham’s algorithm Compositing Polygon filling.
Triangle Scan Conversion. 2 Angel: Interactive Computer Graphics 5E © Addison-Wesley 2009 Rasterization Rasterization (scan conversion) –Determine which.
CS 325 Introduction to Computer Graphics 02 / 01 / 2010 Instructor: Michael Eckmann.
Numeric Processing Chapter 6, Exploring the Digital Domain.
Graphics Output Primitives
Line Drawing and Generalization. Outline  overview  line drawing  circle drawing  curve drawing.
CGMB214: Introduction to Computer Graphics
The tty Interface An introduction to “systems programming” in the Linux environment.
Value and Reference Parameters. CSCE 1062 Outline  Summary of value parameters  Summary of reference parameters  Argument/Parameter list correspondence.
CAP4730: Computational Structures in Computer Graphics
Image Synthesis Rabie A. Ramadan, PhD 7. 2 Image Rasterization.
CSNB374: Microprocessor Systems Chapter 5: Procedures and Interrupts.
Review on Graphics Basics. Outline Polygon rendering pipeline Affine transformations Projective transformations Lighting and shading From vertices to.
Implementing ‘noecho’ Programming details regarding the Linux implementation for ‘struct termios’ objects.
Midpoint Circle Algorithm
CS 325 Introduction to Computer Graphics 02 / 03 / 2010 Instructor: Michael Eckmann.
GEOMETRY AND LINE GENERATION Geometry and Line Generation Chapter 2.
Today’s topics Signals and how to control the program behavior in handling signals. Terminal I/O.
Scan Conversion.
Lecture 13: Raster Graphics and Scan Conversion
Reconfigurable Computing - Options in Circuit Design John Morris Chung-Ang University The University of Auckland ‘Iolanthe’ at 13 knots on Cockburn Sound,
Computer Graphics : Bresenham Line Drawing Algorithm, Circle Drawing
Computer Graphics CC416 Lecture 04: Bresenham Line Algorithm & Mid-point circle algorithm Dr. Manal Helal – Fall 2014.
Write Bresenham’s algorithm for generation of line also indicate which raster locations would be chosen by Bresenham’s algorithm when scan converting.
Computer Graphics Inf4/MSc Computer Graphics Lecture 4 Line & Circle Drawing.
10/10/2006TCSS458A Isabelle Bichindaritz1 Line and Circle Drawing Algorithms.
Computer Graphics Lecture 06 Circle Drawing Techniques Taqdees A. Siddiqi
Primitive graphic objects
2D GPU Platform with Hardware-Accelerated Features
CS G140 Graduate Computer Graphics
Lecture 05: Mid-point Ellipse algorithm Dr. Manal Helal – Fall 2014
Chapter Three Part I Output Primitives CS 380.
Advanced UNIX programming
Lecture 13 Clipping & Scan Conversion
Chapter 3 Graphics Output Primitives
Presentation transcript:

Michener’s Algorithm An efficient scheme for drawing circles (and filling circular disks) on a raster graphics display

Scan conversion Geometric objects possess implicit parameters Example: A circle has a ‘center’ and a ‘radius’ Its equation is: (x – xc)2 + (y - yc)2 = R2 x and y are from the continuum of real numbers CRT display is a discrete 2D array of ‘pixels’ So drawing a circle requires a conversion, from something continuous into something discrete In computer graphics it’s called scan conversion Imperfections: unavoidable, but to be minimized

Graphics Animations Fast drawing is essential for animations Some guidelines for speed: eliminate all redundant computations prefer integer arithmetic to floating-point prefer add and subtract to multiply or divide Famous example: ‘Michener Algorithm’ We can use it later with our ‘pong’ game

Eight-fold symmetry (-x, y) (x, y) (-y, x) (y, x) (y, -x) (-y, -x)

Compute only one octant

Subroutine: draw_octant_points Arguments: int x, y, xcent, ycent, color; draw_pixel( xcent + x, ycent + y, color ); draw_pixel( xcent + y, ycent + x, color ); draw_pixel( xcent - x, ycent + y, color ); draw_pixel( xcent - y, ycent + x, color ); draw_pixel( xcent + x, ycent - y, color ); draw_pixel( xcent + y, ycent - x, color ); draw_pixel( xcent - x, ycent - y, color ); draw_pixel( xcent - y, ycent - x, color );

The “best” pixel to draw? Blue pixel: too far from center ( E > 0 ) Red pixel: too near the center ( E < 0 ) Error-term: E = (x2 + y2) – R2

Decision at n-th stage Pn-1 An ? yn-1 Bn ? xn-1 xn Algorithm: compute sum = error( An ) + error( Bn ); If ( sum < 0 ) choose A n; otherwise choose B n.

Formula: sum of error-terms Assume circle has radius R, center (0,0) error( An) = (xn-1+1)2 + (yn-1)2 – R2 error( Bn) = (xn-1+1)2 + (yn-1 – 1)2 – R2 sumn = 2(xn-1+1)2 + 2(yn-1)2 –2(yn-1) + 1-2R2 Now, how is sumn different from sumn+1: If An is chosen at the n-th step? If Bn is chosen at the n-th step?

Difference Equation Observe that: sumn+1 – sumn = 4xn-1 + 6 + 2(yn2 – yn-12) – 2(yn – yn-1) When An is selected at n-th stage: we will have yn = yn-1 thus: sumn+1 = sumn + 4xn-1 + 6 When Bn is selected at n-th stage: we will have yn = yn-1 - 1 thus: sumn+1 = sumn + 4(xn-1 – yn-1) + 10

Algorithm initialization We start with the point P0 = (x0,y0), where x0 = 0 and y0 = R In this case: A1 = (1, R) and B1 = (1, R-1) So the initial ‘sum-of-errors’ term will be: sum1 = error(A1) + error(B1) = [(12 + R2) – R2] + [(12 + R2–2R+1) – R2] = 3 – 2R

Michener’s Algorithm int x = 0, y = R, sum = 3 – 2*R; while ( x <= y ) { draw_octant_points( x, y, xc, yc, color ); if ( sum < 0 ) sum += 4*x + 6; else { sum += 4*(x - y) + 10; --y; } ++x; }

Reference Francis S. Hill, jr., “Computer Graphics,” Macmillan (1990), pp. 433-435. NOTE: Michener’s circle-drawing method owes its inspiration to a famous algorithm for efficient line-drawing, devised in 1965 by J. E. Bresenham (see the IBM Systems Journal, vol 4, pp. 305-311).

Circle ‘fill’ also exploits symmetry (-x, y) (x, y) (-y, x) (y, x) (y, -x) (-y, -x) (-x, -y) (x, -y)

Subroutine: draw_segments Arguments: int x, y, xc, yc, color; draw_horiz( xc – x, xc + x, yc + y, color ); draw_horiz( xc – x, xc + x, yc - y, color ); draw_horiz( xc – y, xc + y, yc + x, color ); draw_horiz( xc – y, xc + y, yc - x, color );

draw_horiz( int xlo, xhi, y, color ); Clipping to screen boundaries: If (( y < ymin )||( y > ymax )) return; if ( xlo < xmin ) xlo = xmin; if ( xhi > xmax ) xhi = xmax; Drawing the horizontal segment: for (x = xlo; x <= xhi; x++) draw_pixel( x, y, color );

Demo-program Try the ‘michener.cpp’ demo It uses VESA graphics-mode 0x4101 Screen resolution is 640x480 Color depth is 8 bits-per-pixel (8bpp) SVGA’s Linear Frame Buffer is enabled

In-Class Exercise Modify the ‘michener.cpp’ demo: Use the standard ‘rand()’ function Draw lots of color-filled circles Stop if user hits <ESCAPE> key NOTE: For the latter feature, we need to discuss setting up the terminal keyboard so it uses a ‘non-canonical’ input-mode

The ‘tty’ interface ‘tty’ is an acronyn for ‘TeleTYpe’ terminal Such devices have a keyboard and screen Behavior emulates technology from 1950s Usually a tty operates in ‘canonical’ mode: Each user-keystroke is ‘echoed’ to screen Some editing is allowed (e.g., backspace) The keyboard-input is internally buffered The <ENTER>-key signals an ‘end-of-line’ Programs receive input one-line-at-a-time

‘tty’ customization Sometimes canonical mode isn’t suitable (an example: animated computer games) The terminal’s behavior can be modified! UNIX provides a convenient interface: #include <termios.h> struct termios tty; int tcgetattr( int fd, struct termios *tty ); int tcsetattr( int fd, int flag, struct termios *tty );

TeleTYpe display device How does the ‘tty’ work? application User space tty_driver c_lflag Kernel space input handling c_iflag c_cc output handling c_oflag SOFTWARE struct tty { c_iflag; c_oflag; c_cflag; c_lflag; c_line; c_cc[ ]; }; terminal_driver c_cflag HARDWARE TeleTYpe display device

The ‘c_lflag’ field This field is just an array of flag bits Individual bits have symbolic names Names conform to a POSIX standard Linux names match other UNIX’s names Though actual symbol values may differ Your C/C++ program should use: #include <termios.h> for portability to other UNIX environments

ICANON and ECHO Normally the ‘c_lflag’ field has these set They can be cleared using bitwise logic: tty.c_lflag &= ~ECHO; // inhibit echo tty.c_lflag &= ~ICANON; // no buffering

The ‘c_cc[ ]’ array ‘struct termios’ objects include an array The array-indices have symbolic names Symbol-names are standardized in UNIX Array entries are ‘tty’ operating parameters Two useful ones for our purposes are: tty.c_cc[ VMIN ] and tty.c_cc[ VTIME ]

How to setup ‘raw’ terminal-mode Step 1: Use ‘tcgetattr()’ to get a copy of the current tty’s ‘struct termios’ settings Step 2: Make a working copy of that object Step 3: Modify its flags and control-codes Step 4: Use ‘tcsetattr()’ to install changes Step 5: Perform desired ‘raw’ mode input Step 6: Use ‘tcsetattr()’ to restore the terminal to its original default settings

‘raw’ mode needs four changes tty.c_cc[ VMIN ] = 1; so the ‘read()’ function will return as soon as at least one new input-character is available tty.c_cc[ VTIME ] = 0; so there will be no time-delay after each new key pressed until the ‘read()’ function returns tty.c_lflag &= ~ECHO; // no echoing tty.c_lflag &= ~ICANON; // no buffering

Demo program: ‘rawtty.cpp’ This program may soon prove useful It shows the keyboard scancode values It demonstrates ‘noncanonical’ tty mode It clears the ISIG bit (in ‘c_lflags’ field) This prevents <CONTROL>-C from being used to abort the program: the user must ‘quit’ by hitting the <ESCAPE>-key; so default terminal-settings will get reinstalled

In-class Exercise Use the Linux ‘tty’ interface-functions to reprogram your terminal for ‘raw’ input Draw ramdom color-filled circles -- until your user hits the <ESCAPE> key Algorithm: int done = 0; do { draw_next_circle( x, y, r, color ); int inch = 0; read( 0, &inch, 4 ); if ( inch == 0x1B ) done = 1; } while ( !done );