CS 43 Computer Graphcs I Lne Drawng and Clppng Week, Lecture 2 Davd Breen, Wllam Regl and Maxm Peysakhov Geometrc and Intellgent Computng Laboratory Department of Computer Scence Drexel Unversty http://gcl.mcs.drexel.edu Outlne Lne drawng Dgtal dfferental analyzer Bresenham s algorthm lne clppng Parametrc lne clppng 2 Scan-Converson Algorthms Scan-Converson: Computng pxel coordnates for deal lne on 2D raster grd Pxels best vsualzed as crcles/dots Why? Montor hardware 3 Dgtal Dfferental Analyzer (DDA) If slope s less then _x = (otherwze _y = ) Compute correspondng _y (_x) x k+ = x k + _x y k+ = y k + _y Issues: Avodng floatng pont ops, multplcatons, and real numbers y k+ y k _y _x x k x k+ 4 965 @ IBM Basc Idea: Only nteger arthmetc Incremental Consder the mplct equaton for a lne: 5 Gven: mplct lne equaton: Let: where r and q are ponts on the lne Then: Observe that all of these are ntegers and: for ponts above the lne for ponts below the lne Now.. 6
Suppose we just fnshed (assume <slope<) other cases symmetrc Whch pxel next? E or NE 7 Assume: = exact y value at y mdway between E and NE: Observe: If, then pck E Else pck NE If, t doesn t matter 8 Create modfed mplct functon (2x) Create a decson varable D to select, where D s the value of f at the mdpont: If then m s below the lne NE s the closest pxel If then m s above the lne E s the closest pxel 9 Note: because we multpled by 2x, D s now an nteger---whch s very good news How do we make ths ncremental?? Case I: When E s next What ncrement for computng an new D? Next mdpont s: Case II: When NE s next What ncrement for computng an new D? Next mdpont s: Hence, ncrement by: Hence, ncrement by: 2 2
How to get an ntal value for D? The Algorthm Suppose we start at: Intal mdpont s: Then: Assumptons: 3 Pre-computed: 4 5 6 7 8 3
9 2 2 22 23 Some ssues wth s Pxel densty vares based on slope straght lnes look darker, more pxels per CM Endpont order Lne from P to P2 should match P2 to P Always choose E when httng M, regardless of drecton 24 4
Some ssues wth s How to handle the lne when t hts the clp wndow? Vertcal ntersectons Could change lne slope Need to change nt cond. Horzontal ntersectons Agan, changes n the boundary condtons Can t just ntersect the lne w/ the box 25 Scssorng Clppng Performed durng scan converson of the lne (crcle, polygon) Compute the next pont (x,y) If x mn x x max and y mn y y max Then output the pont. Else do nothng Issues wth scssorng: Too slow Does more work then necessary We are not gong to use t. 26 The Lne Clppng Algorthm How to clp lnes to ft n wndows? easy to tell f whole lne falls w/n wndow harder to tell what part falls nsde Consder a straght lne And wndow: 27 Basc Idea: Frst, do easy test completely nsde or outsde the box? If no, we need a more complex test Note: we wll also need to fgure out how scan lne meets the box 28 The Easy Test: Compute 4-bt code based on endponts P and P 2 Lne s completely vsble ff both code values of endponts are,.e. If lne segments are completely outsde the wndow, then 29 3 5
Otherwse, we clp the lnes: We know that there s a bt flp, w.o.l.g. assume ts (x,x ) Whch bt? Try `em all! suppose ts bt 4 Then x < WL and we know that x WL We need to fnd the pont: (x c,y c ) 3 Clearly: Usng smlar trangles Solvng for y c gves 32 Replace (x,y ) wth (x c,y c ) Re-compute values Contnue untl all ponts are nsde the clp wndow QED 33 Parametrc Lne Clppng Developed by Cyrus and Beck n 978 Used to clp 2D/3D lnes aganst convex polygon/polyhedron Lang and Barsky (984) algorthm effcent n clppng uprght 2D/3D clppng regons Frst we wll follow orgnal Cyrus-Beck development to ntroduce parametrc clppng Then we wll reduce Cyrus-Beck to more effcent Lang-Barsky case 34 The Cyrus-Beck Technque algorthm computes (x,y) ntersectons of the lne and clppng edge Cyrus-Beck fnds a value of parameter t for ntersectons of the lne and clppng edges Smple comparsons used to fnd actual ntersecton ponts Lang-Barsky optmzes t by examnng t values as they generated to reject some lne segments mmedately 35 Fndng the Intersecton Ponts Lne P(t) = P +t(p -P ) Pont on the edge P E N [P(t)-P ] = N [P + t(p-p )-P ] = N [P -P ] + N t[p-p ] = E E Let D = (P-P ) N [P - PE ] t = - N D Make sure. D, or P P 2. N D lnes are not parallel E 36 6
Fndng the Lne Segment Classfy pont as potentally enterng (PE) or leavng (PL) PE f crosses edge nto nsde half plane => angle P P and N greater 9 => N D < PL otherwse. Fnd T e = max(t e ) Fnd T l = mn(t l ) Dscard f T e > T l If T e < T e = If T l > T l = Use T e, T l to compute ntersecton coordnates (x e,y e ), (x l,y l ) 37 7