This Wk Computr Grphics Vctors nd Oprtions Vctor Arithmtic Gomtric Concpts Points, Lins nd Plns Eploiting Dot Products CSC 470 Computr Grphics 1 CSC 470 Computr Grphics 2 Introduction Introduction Wh do w nd to lrn bout ctors? w nd to know whr objcts r positiond in th world th si nd orinttion of th objcts how fr th objcts r from on nothr how rflctions work how phsics works how light flls on th objcts Coordints 2D 3D lft- hndd 3D right-hndd W will b using this on. CSC 470 Computr Grphics 3 CSC 470 Computr Grphics 4 Grphics Mths b Empl Lt s writ progrm whr ct chss rt round th scrn. ct rt Grphics Mths b Empl tpdf struct { GLflot, ; GLflot spd; } nimobjct; How do w mo th ct? nimobjct ct = {100,100,0.05}; nd bfor drwing th ct ch loop: ct.+=ct.spd; ct.+=ct.spd; CSC 470 Computr Grphics 5 -mouschs1. CSC 470 Computr Grphics 6
Grphics Mths b Empl Wht is th first problm w ncountr? Th ct s momnt hs nothing to do with th rt s loction. Th ct just mos off th scrn. Grphics Mths b Empl Thrfor w nd th ct s momnt to b towrds th rt. W cn s: ct.loction + = rt.loction or = rt.loction ct.loction This cn b dscribd s ctor (). (500,400) = (500,400) (100,100) = (400,300) (100,100) CSC 470 Computr Grphics 7 CSC 470 Computr Grphics 8 Vctors, closr look. Vctors A ctor hs lngth nd dirction. Vctors r prssd in th sm w s point coordints. Point (5,10) Vctor (5,10) But how r th diffrnt? = (5,10) P = (5,10) A point hs loction A ctor hs no loction A ctor is pth from on point to nothr. CSC 470 Computr Grphics 9 CSC 470 Computr Grphics 10 Vctors Vctors P = (1,10) Q = (8,1) A ctor cn b dtrmind b subtrcting point coordints. = Q P = (8-1,1-10) = (7, -9) In othr words, tlls us how to gt from P to Q. (Tk 7 stps cross nd 9 stps down!!) P = (1,10) Q = (8,1) Dfinitions Th diffrnc btwn two points is ctor: = Q-P Th sum of point nd ctor is point: Q = P + CSC 470 Computr Grphics 11 CSC 470 Computr Grphics 12
Vctors Your Turn Wht is th ctor tht gos from P = (9,10) to Q = (15,7)? = (6, -3) Wht is th rsulting point from dding ctor = (9,- 20) to point P = (1,2)? Q = (10, -18) Grphics Mths b Empl So now w know: ct.loction + (400,300) = rt.loction Rthr thn this: ct.+=ct.spd; ct.+=ct.spd; Should w writ this:. = rt. - ct.;. = rt. - ct.; ct.+=.; ct.+=.; = (400,300) (100,100) (500,400) CSC 470 Computr Grphics 13? Wht would hppn? -mouschs2. CSC 470 Computr Grphics 14 Grphics Mths b Empl Wht hppnd Th ct s loction ws updtd to b th loction of th rt. It mod to th rt s loction in on jump. Grphics Mths b Empl Ech loop w wnt th ct to mo long th ctor, but not th whol lngth. W wnt it to ppr to b moing towrds th loction. CSC 470 Computr Grphics 15 CSC 470 Computr Grphics 16 Thr r two fundmntl ctor oprtions: ou cn scl thm 8 if = (1,2) thn 8 = (8,16) ou cn dd thm + = (3,4), = (8,1) thn + = (11,5) Scling Vctor 2 0.5-0.5 CSC 470 Computr Grphics 17 CSC 470 Computr Grphics 18
Adding Vctors - + - Your Turn Gin ctor = (10,20,5) wht is: 2, 0.5 nd -0.2? 2 = (20,40,10) 0.5 = (5,10,2.5) -0.2 = (-2, -4, -1) Gin ctor = (1,1,1) nd = (8,4,2), wht is: +, nd + = (9,5,3) = (-7, -3, -1) = (7, 3, 2) CSC 470 Computr Grphics 19 CSC 470 Computr Grphics 20 Linr Combintions dding scld ctors togthr 8 + 2 Dfinition linr combintion of m ctors 1, 2,, m is ctor of th form: w = 1 1 + 2 2 + + m m Linr Combintions Empl = (1,2,3) nd = (1,1,1) 2 + 3 = (2,4,6) + (3,3,3) = (5,7,9) CSC 470 Computr Grphics 21 CSC 470 Computr Grphics 22 Linr Combintions Affin Combintions Cofficints dd up to unit in othr words th sclrs dd up to 1 1 + 2 + + m = 1.g. 3 + 2b 4c (3+2-4=1) Forcing n ffin combintion (1-t) + (t)b Chllng Qustion Th follow is n ffin trnsformtion: i+ jb+?c wht is th cofficint of c? i + j +? = 1? = 1 i j thrfor i + jb + (1-i-j)c CSC 470 Computr Grphics 23 CSC 470 Computr Grphics 24
Linr Combintions Con Combintions Cofficints dd up to unit. but ll cofficints must b btwn 0 nd 1 i.. 1 + 2 + + m = 1 nd 1 >= i >= 0 for ll 1,,m.g.9 +.1w.25 +.75w Linr Combintions Con Combintions Th st of ll con combintions of two ctors, 1 nd 2 is: = (1-) 1 + 2 s ris from 0 to 1 Wh? r possibl sclr rsion of 1 nd 2 is cord! CSC 470 Computr Grphics 25 CSC 470 Computr Grphics 26 Linr Combintions Con Combintions = (1-) 1 + 2 cn b rrrngd to gi: = 1 + ( 2-1 ) This shows tht th ctor will b 1 plus som scld rsion of th ctor joining 1 with 2 2 2 1 CSC 470 Computr Grphics 27 1 ( 2 1 ) Linr Combintions Con Combintions Gin 3 ctors 1, 2 nd 3 th st of linr combintions will b: = 1 1 + 2 2 + (1-1 - 2 ) 3 = 0.2 1 + 0.3 2 + 0.5 3 CSC 470 Computr Grphics 28 = 0.5 1 + 0.5 2 + 0 3 3 2 0.5 3 0.3 2 0.2 1 1 All lus of will point insid this rgion!! Linr Combintions Con Combintions Gin 3 ctors 1, 2 nd 3 th st of linr combintions will b: = 1 1 + 2 2 + (1-1 - 2 ) 3 3 0.5 2 2 0.5 1 1 All lus of will point insid this rgion!! Grphics Mths b Empl So rthr thn dd th whol ctor to th ct s loction, w could just dd scld rsion of it. If w tk on 500 th of th ctor ch loop: ct.+=(./500.0); ct.+=(./500.0); Th ct will mo long th ctor. = 0.2 1 + 0.3 2 + 0.5 3 CSC 470 Computr Grphics 29 = 0.5 1 + 0.5 2 + 0 3 -mouschs3. CSC 470 Computr Grphics 30
Vctor Lngth Mgnitud is th lngth of th ctor dtrmind using Pthgors Thorm cn ou rmmbr wht this is? Mgnitud Pthgors Thorm Th lngth of th hpotnus of right-ngld tringl is qul to th squr root of th sum of th squrs of th othr two sids. h h = 2 + b 2 b CSC 470 Computr Grphics 31 CSC 470 Computr Grphics 32 Mgnitud Pthgors Thorm Think of ctor s th hpotnus of right-ngld tringl. Th lngth of ctor is dnotd using = + 2 2 coordint Mgnitud Pthgors Thorm Empl: Wht is th mgnitud of = (5,10)? = sqrt(5 2 +10 2 ) = sqrt(25+100) = sqrt(125) 11.18 coordint CSC 470 Computr Grphics 33 CSC 470 Computr Grphics 34 Your Turn Find for: =(1,-2,5), w=(10,3,1) nd t=(1,1,1) = 5.5677 w = 10.488 t = 1.732 Mgnitud Sinc ctor is th pth btwn to points, thn th lngth of th ctor must b th distnc btwn th two!! P = (1,10) Q = (8,1) CSC 470 Computr Grphics 35 CSC 470 Computr Grphics 36
Mgnitud Somtims it is usful (s ou will s soon) to scl ctor so tht its lngth is unit (1). A norml ctor is dnotd b ht: â. This mns diiding th ctor coordints b th lngth of th ctor. â= / Mgnitud Empl: Wht is th norml rsion of = (1,5,3)? = sqrt(1 2 + 5 2 + 3 2 ) = 5.916 â = (1/5.916, 5/5.916, 3/5.916) = (0.169, 0.845, 0.5) CSC 470 Computr Grphics 37 CSC 470 Computr Grphics 38 Your turn Normlis th following: = (2,4,6) g = (1,1,1) h = (0,5,1) Answrs with gross rounding rrors â = (0.26,0.53,0.8) ĝ = (0.6,0.6,0.6) ĥ = (0,1,0.2) CSC 470 Computr Grphics 39 Importnt Vctors 3 Vctors ou should bcom good frinds with: Normlisd ctors prlll to: is (1,0,0); (clld i) is (0,1,0); (clld j) is (0,0,1); (clld k) Ths ctors r clld: th unit ctors CSC 470 Computr Grphics 40 Grphics Mths b Empl In th prious mpl, th ct mod towrds th rt nd it bgn to slow down bcus chngd ch clcultion s th ct got closr to th rt. NOTE: Th lngth of th ctor chngd but not th dirction. If w wnt th ct to mo t constnt spd w could us th normlisd rsion of, s w know it would lws h lngth of 1. CSC 470 Computr Grphics 41 Grphics Mths b Empl So rthr thn dd th whol ctor to th ct s loction, w could just dd th normlisd rsion of it. Thrfor w would do this:. = rt. - ct.;. = rt. - ct.;.normlis(); ct.+=.; ct.+=.; whr oid normlis() { flot lngth = sqrt(pow(,2)+pow(,2)); = /lngth; = /lngth; } -mouschs4. CSC 470 Computr Grphics 42
Grphics Mths b Empl Th mous now mos with constnt spd. It mos distnc of 1 ch loop. To slow or spd this, w nd to incorport spd ribl. Thus: ct. +=. * spd; ct. +=. * spd; Grphics Mths b Empl Now w h th ct moing towrds th rt!! Wht nt? Th ct should turn in th dirction it is moing. How do w do this? Us ctors of cours...!!! W cll, locit. -mouschs5. CSC 470 Computr Grphics 43 CSC 470 Computr Grphics 44 Grphics Mths b Empl W nd to dtrmin th ngl btwn th ct s initil locit (1) nd it s nw locit (2). To do this, w us th DOT product. 1 Ө 2 Th DOT Product Usd to sol gomtricl problms in computr grphics. Usful in dtrmining th w in which lins nd ctors intrsct. CSC 470 Computr Grphics 45 CSC 470 Computr Grphics 46 Th DOT Product Clcultd b multipling nd dding row lus with column lus. Dfinition Th dot product of two ctors is dnotd ٠w nd hs th lu: n iwi i= 1 Th DOT Product or whr = ( 1, 2 ) nd w = (w 1,w 2 ) th dot product, ٠ w will b: ( 1 w 1 + 2 w 2 ) Empl, = (2,1) nd w = (3,5) thn ٠ w will b: 2*3 + 1*5 = 11 Empl, = (2,2,2,2) nd w = (4,1,2,1.1), ٠ w will b: 2*4 + 2*1 + 2*2 + 2 * 1.1 = 16.2 CSC 470 Computr Grphics 47 CSC 470 Computr Grphics 48
Th DOT Product Th ngl btwn two ctors. Th dot product cn b usd to find th ngl btwn two ctors or intrscting lins. Gin 2 ctors nd c, th ngl btwn th ctors cn b clcultd. = ( cos Ө, sin Ө) c = ( c cos Өc, c sin Өc) Th dot product ٠ c is c cos(өc - Ө) or ٠ c = c cos(ө) Ө c whr Ө is th ngl btwn th ctors! Ө c Ө Th DOT Product ٠ c = c cos(ө) Diid both sids b c for mor compct form: ( ٠ c)/ c = c cos(ө)/ c ĉ ٠ ê = cos(ө ) Thrfor: Th ngl btwn two ctors is dot product of th normlisd ctors. Ө c Ө c Ө th CSC 470 Computr Grphics 49 CSC 470 Computr Grphics 50 Th DOT Product Empl: Find th ngl btwn (5,6) nd (8,2) cos(ө ) = ĉ ٠ ê ĉ = c/ c = (5,6) / sqrt(5 2 +6 2 ) = (5,6) / 7.8 = (0.64,0.77) ê = / = (8,2) / sqrt(8 2 +2 2 ) = (8,2) / 8.25 = (0.8,0.24) ĉ ٠ ê = 0.8248 Ө = cos- 1 (0.8248) = 34.43 Ө c Grphics Mths b Empl Thrfor th ct s turning ngl should b: ngl = cos(ct.locit.*. + ct.locit.*.); -mouschs6. In th mpl, th ct sms to b fcing th wrong dirction. Wht wnt wrong? 1 Ө 2 CSC 470 Computr Grphics 51 CSC 470 Computr Grphics 52 Grphics Mths b Empl Th ngl clcultion ws corrct! Howr, positi ngls in computr grphics turn objcts nticlockwis. How do w clcult ngti ngl? You cn t! How do w know whthr to turn lft or right? Wll, not b using th dot product lon. CSC 470 Computr Grphics 53 Entr th Cross Product s Th cross product of two ctors is nothr ctor. Th rsulting ctor is prpndiculr to both ctors. CSC 470 Computr Grphics 54
s Gin = (,, ) nd = (,, ) th cross product would b (in trms of th unit ctors): = i( - ) + j( - ) + k( - ) or if ou find it sir ou cn form dtrminnt, thus: = i j k CSC 470 Computr Grphics 55 s How do ou us this to clcult th dot product? Tk ch itm in th top row nd multipl b th diffrnc of th products of th itms in th othr columns. = i j k CSC 470 Computr Grphics 56 s i( - ) j( - ) k( - ) Now put thm togthr: = i( - ) + j( - ) + k( - ). nd ou h th CROSS PRODUCT!!! = i j k CSC 470 Computr Grphics 57 s i( - ) + j( - ) + k( - ) is just nothr w of writing 3d coordint.g. (,, ) ( ( - ), ( - ), ( - )) CSC 470 Computr Grphics 58 Now w h th cross product wht r w going to do with it? W cn us it to find prpndiculr ctors nd rs. is prpndiculr to both nd th lngth of quls th r of th prlllogrm boundd b nd Us th right hnd thumb rul to dtrmin th dirction of CSC 470 Computr Grphics 59 CSC 470 Computr Grphics 60
Finding th Norml to Pln Gin thr points on th pln w cn dtrmin th norml to th pln. P 1, P 2, P 3 -> = P 2 -P 1, w = P 3 -P 1 Find w to clcult th norml, n. An sclr multipl of n is lso norml to th pln. W cn lso... Dtrmin if ou nd to turn right or lft to go from fcing on ctor to nothr. Lt s s = (1,0,0) nd = (0,1,0) = (1*0-0*1, 0*0-1*0, 1*1 0*0) = (0,0,1) CSC 470 Computr Grphics 61 CSC 470 Computr Grphics 62 W cn lso... Dtrmin if ou nd to turn right or lft to go from fcing on ctor to nothr. Lt s s = (1,0,0) nd = (0,1,0) = (0*1-1*0, 1*0-0*0, 0*0 1*1) = (0,0,-1) W cn lso... Dtrmin if ou nd to turn right or lft to go from fcing on ctor to nothr. = (0,0,1) = (0,0,-1) Wht do ths rsults tll ou? CSC 470 Computr Grphics 63 CSC 470 Computr Grphics 64 W cn lso... Dtrmin if ou nd to turn right or lft to go from fcing on ctor to nothr. W cn lso... Dtrmin if ou nd to turn right or lft to go from fcing on ctor to nothr. if w wnt to turn from fcing to fcing, thn obious, b looking t it w h to turn to our lft. if w wnt to turn from fcing to fcing, thn obious, b looking t it w h to turn to our right. In this cs gis us rsulting In this cs gis us rsulting ctor with positi lu. ctor with ngti lu. CSC 470 Computr Grphics 65 CSC 470 Computr Grphics 66
Thrfor: if th lu of th cross product is ngti thn w turn clockwis if th lu of th cross product is positi thn w turn countrclockwis Thrfor: doubl turnangl(ctor 1, ctor 2) { doubl ngl = cos(ct.locit.*. + ct.locit.*.) * 180/3.14; //conrt to dgrs //if of cross product is ngti if( (1.*2. - 1.*2.) < 0) rturn -ngl; //if of cross product is positi ls if ((1.*2. - 1.*2.) > 0) rturn ngl; //if is 0 thn no turn ngl rturn 0; } -mouschs7. CSC 470 Computr Grphics 67 CSC 470 Computr Grphics 68 Lt s mo th Rt If w progrm mous click to chng th loction of th rt, w would ssum th ct will chng dirctions nd mo towrds it. This is grt tst for our progrm. Lt s mo th Rt oid mmous(int button, int stt, int, int ) { //if lft button clickd mo th rt if(button == GLUT_LEFT_BUTTON && stt == GLUT_DOWN) { rt. = ; rt. = SCREENHEIGHT - ; } } CSC 470 Computr Grphics 69 -mouschs8. CSC 470 Computr Grphics 70 Homognous Coordints Som grphics sstms nd OpnGL rprsnt points nd ctors s homognous coordints. This mns tht in 2D coordint hs 3 lus (,, ) nd in 3D, 4 lus (,,, ) Homognous Coordints For point = 1 For ctor = 0.g. Th point (2,4) bcoms (2,4,1)..g. Th ctor (3,5) bcoms (3,5,0)..g. Th point (3,4,1) bcoms (3,4,1,1)..g. Th ctor (3,6,7) bcoms (3,6,7,0). Whn it coms to working with th point or ctor th 1 nd 0 cn just b droppd. Th rson for this will bcom clr in ltr chptrs.. CSC 470 Computr Grphics 71 CSC 470 Computr Grphics 72
Empl Twning btwn shps Nt wk. Rottions Sclings nd Trnsltions Th Endqwrtuiop ;lkjhgfds - twn. CSC 470 Computr Grphics 73 CSC 470 Computr Grphics 74