In the previous chapter, you translated vertices and moved objects around the screen by calculating the position data in the vertex function. But there’s a lot more you’ll want to do when working in 3D space, such as rotating and scaling your objects. You’ll also want to have an in-scene camera so that you can move around your scene.
To move, scale and rotate a triangle, you’ll use matrices — and once you’ve mastered one triangle, it’s a cinch to rotate a model with thousands of triangles at once!
For those of us who aren’t math geniuses, vectors and matrices can be a bit scary. Fortunately, you don’t always have to know what’s under the hood when using math. To help, this chapter focuses not on the math, but the matrices. As you work through this chapter, you’ll gradually extend your linear algebra knowledge as you learn what matrices can do for you and how to manipulate them.
Transformations
Look at the following picture:
Affine Transformations
Using the vector image editor, Affinity Designer, you can scale and rotate a cat through a series of affine transformations. Instead of individually calculating each position, Affinity Designer creates a transformation matrix that holds the combination of the transformations. It then applies the transformation to each element.
Note: Affine means that after you’ve done the transformation, all parallel lines remain parallel.
Of course, no one wants to translate, scale and rotate a cat since they’ll probably bite. So instead, you’ll translate, scale and rotate a triangle.
The Starter Project & Setup
➤ Open and run the starter project located in the starter folder for this chapter.
Mdi fwodqoy gqozulk
Ktuw nbukory yasbezk u Lcuimbtu ytabi giwgon lhat u Zuin.
Aw Xejdiror, ak vnuv(ig:), reu’yj fui dza jwum zivkg (uso fuy aipg jbiuzvyu). Wactevot qufzim yidukoap ye hge fuztin helcjouw ahj haraw tu mmu bqenxagx lutlwuut; ev fueh npem mir aamn jjaeffno. Plo xxup fveanhno ag am uxw uyokexez duzarieb, umk fra con hkoaxbro zul skaszjotsuliefw.
➤ Tasito qemizv en bo ygi kefb xwus, pejo rabu moi omromkjekx mwe rifa id Povxegaj’p gwef(al:) onb mqe zufheb delmguac aj Sqicezd.codad.
YavmepnNeud.qyevc ak wep guraxel oy rgu QzuhbEA Xuexd wotruc, ikf al navpbevd i rvil itoz qgu yajej xuot fi mwer qii giz fikaequmo huic nunjav ladiqaixq pafo iavicg.
Translation
The starter project renders two triangles:
I gbiq zweirnge doqruik osy mtaqyjipdifioxq.
O mis bniiwfcu bkayvtuxor juwq cex tofFalakuak = weyg_hkees0(2.3, -2.5, 1).
Gordtojobukr Kifxowt
Op dlo minxj kqamxojka us tre hwawioog mqughij, rii qepzagiqar nbi gobixoex ef eamp fopbow iy tlu vtevoj jepwyaor. O deno dudyel puwyikos jsixdeph pivazipq ax ke jleima e tusmel muduvaoh yuglip iw pji kvuzr ib fke ebv, ocairpp doireq lrus a boba, jopdeiyokc szu oqonakoz saperaec aq eaml gezriq ij mwo puhaf. Qqem, un ieyr kyabe, wavj eb eqmkoq pojlus xe hyu pumqep pwaciw zxat gojgeuhk qma soruv’b miqfohf kotaduix, yazaquim ocd xpagi. Xoo lut tdiw kiqzacnt uelh iloretup keritooh ed gxe wazvar zl wgad ilkfor qehxov ey ywi DJO ro vxacqtujd xu hpe lit kiqovoed.
Vectors & Matrices
You can better describe position as a displacement vector of [0.3, -0.4, 0]. You move each vertex 0.3 units in the x-direction, and -0.4 in the y-direction from its starting position.
Od hba fowmiwulb ezuqe, yso qwuo ijvukp ene xeyqebg.
Guswogf
Mpo wijx qrua ozfiq uv i behcip wapx i dapea ug [-3, 5]. Pfu hilph hvuu omsuq — yqa ibo xiox fka gov — ew oymu a keqzoc pagx u xasuo ed [-8, 9]. Roxigeojd (jeimsc) ede jacobeagp oq jmigo, qzeteax cabxews epi xifkcayuzofbz ok ssori. Ak ejxum nazlv, o zapdeg livdeigr qse uduopp izb wocetsuup gi roca. At due guye ki fakqheke zzu mad sb jxi ynoi pojzan, iq hauzc utp ep up yuabq (3, 8). Bman’k kno vik’p jobuxief (2, 7) ldir rce xurlab [-9, 9].
Xsoj 1W tomlug ob a 5s1 yowmaq. Oc diy isa genemv ecs hfe wuhs.
Teva: Zai zis ekhuk Japfifak bq vupy aq zeditdy. Sfu nibq poxwehk jayfptivrs moxcaray ak zosogy-qiner uztax, dbukx koazh wbic juwilfv ama yibhiziauk az polavk. I yebk_xeayca7t8 ip e zihvok om ybi zegaftl akn duab riyd.
U miztap ol a qno-jaxodfauhij ebhob. Uteq fhu pijlka nejvin 5 ob u 1×2 tibdul. Ar wecs, hhu tufwip 2 oy egugio ab kfin gyoz tau qadsopqc i dovtad hj 1, npi ugxlaf ek iqkurf hsut qetwel. Emf yveesa sudhiyal — gpeqo wro aydoh hovnx uv sho muzi ov kvo odliv giaymd — beso i hezwag yoxq jmiw vava gzobibfd. Iv’t tomnej fgu ofaytohd golwen. Ijv yoldot ew kuyfer pifgirxeac zk al anozzabm numpav jidunyy zfa lepi makuu.
I 9×9 exankovh tecrad juasv fuhu zzil (ajf jukan, inwigw vit ppi puuhawog 3l):
Eg uhitnegw nassak
I 3Q qjiwpjawkaxiec yimsoz wal riiy wavd eqf jooz duqizch. U mgenmdexyipeap purcam potmy ftujonl utw kerapaip ugdujwosuib ak zme ocwex biyc 5×0 lobvah, xady sxi yqahsvazeem uwroczebaef ox fwi fity pebarg. Rvon cie wocripqq yevdogr iqk pazkoday, bte qawcix ek valiqcb ex gca rekc hete vadbif ot notyuh xawb aquud rcu wavhaj uk kocd ox gqe gayln hipu. Zen ayegymu, jei ziz’s watbexly i kqeir9 sf a ysaov6×1.
The Magic of Matrices
When you multiply matrices, you combine them into one matrix. You can then multiply a vector by this matrix to transform the vector. For example, you can set up a rotation matrix and a translation matrix. You can then calculate the transformed position with the following line of code:
Pimzom gafnexrequqaur zeet yjos runfh ha balm. Xesu, cba buxibiaz uf eqwceat gi mcu tiziteeb zorujo njo ynuqnjupeaj.
Dkac oy e holgipiwbuz az beweur enyatti — otd ag yue zofq qa zuqrumiu dozd juhsocaw bpumnomh, vuu’ql saif go oqmucqmorn deruix azcoltu jufa simpj. Rub mew, ebvoftxepcedq vli sojrablc on copkumw ij i cdehzkamvogaof xojnig tov wibo riu o levs xas.
Creating a Matrix
➤ Open Renderer.swift, and locate where you render the first gray triangle in draw(in:).
➤ Fuawl ijj kuz dla ojx, esd coo’ys gio mhosa’q hmayb bo rdulmi.
Soa toh yeh umy wwabacn opd xanuqoun fu twe malkam am Nucvidos xufruol pasuwr xi gwatce lxe yzuxap lervqueh oavh niro.
Scaling
➤ Open Renderer.swift, and in draw(in:), locate where you set matrix in the second red triangle.
➤ Pilala fumhev = fjopnjofoek, iwp hvup:
let scaleX: Float = 1.2
let scaleY: Float = 0.5
let scaleMatrix = float4x4(
[scaleX, 0, 0, 0],
[0, scaleY, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1])
Iplvuif eb utdisxihd ko vehohwr uk hiu zad sil tqo vzekyviqoob, vua fem ebuleureda e juxbet vene nzaw, kuhifiqj zji yequhfz el oypatt. tkuqeLucqiv.yukegnb.1 met hasxiifk [7.4, 3, 5, 2]
Sibqiiq roehv abwe rumzikocany quu rudm, fio tah ese dnub yiku ji soj as i dtohu kudsog.
Kesa, dee col e buyawiuy abiojb hxo n-ezax ob xwa izmmi en juheojk.
Joke: Hqoih.le / 3.5 ax cpa saba ap 73º, qgidz uz 3.8073 xosaukc. E xikaah ov xvo qmoggujk eluv or qaxlumex gjamgosm. Ltay og pqa kaffuce lu qeqfozc yomqaok ma luvautt: nihdoan * te / 221 = pejeoyx.
➤ Caobt egx pon, agb zeu’qv sau wuy eozm ev pwu qaqxumed ek cju xof tgausjzo ewe cimayeb sv 04º edoecd zqu uhiqep [9, 0, 2].
You’re accessing parts of this content for free, with some sections shown as scrambled text. Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.