The Art Of Computer Programming - Eclecticon.info

Transcription

The Art ofComputerProgrammingDr Andrew French

“I’d like to welcome you to this course onComputer Science. Actually, that’s aterrible way to start. Computer Science isa terrible name for this business. First ofall, it’s not a science. It might beengineering, or it might be art, or we’llactually see that computer so-calledscience actually has a lot in common withmagic”Harold Abelson, MIT (1986)2

“Science is what weunderstand wellenough to explain toa computer. Art iseverything else wedo.”Donald Knuth 1938Stanford University“Computer programming is an art, because itapplies accumulated knowledge to the world,because it requires skill and ingenuity, andespecially because it produces objects ofbeauty. A programmer who subconsciouslyviews himself as an artist will enjoy what hedoes and will do it better.”“I can’t go to a restaurant andorder food because I keeplooking at the fonts on themenu.”3

Lecture mapFast numerical calculation display systemsfunctionsUse of computerinterfacesprogramming as anartistic toolWhere you candesign and refinethe toolComplexity, and beauty, from simplicity(i.e. code)Case studiesGeneral thoughts on how humans bestinteract with information technologyWhat You See Is What You Need!4

SpheriumRandom rectangles& MondrianHarmonographCase studiesSoundsnipperCiphersUif Dpnfez pg Fsspstcz Xjmmjbn TiblftqfbsfThe SierpinskiTriangleJulia &fractalsGravitySimulator5

for n 1:Nr rand; %Generate a random numberif ( r 1/3 )%Move half way towards red starx 0.5*( xR x );y 0.5*( yR y );%Plot a red dotplot( x,y, ’r.’ );elseif ( r 1/3 ) && ( r 2/3 )%Move . blue starx 0.5*( xB x );y 0.5*( yB y );%Plot a blue dotplot( x,y, ’b.’ );else%Move . green starn 3x 0.5*( xG x );y 0.5*( yG y );%Plot a green dotplot( x,y, ’g.’ );end( xR , y R )endThe SierpinskiTriangle( xG , yG )n 2n 0( x, y )n 1n 4( xB , y B )6

N 10N 102N 103N 1057

cipher key {'A','B';.'a','b';.'B','C';.'b','c';.This simplesubstitution'C','D';.cipher iscalled a'c','d';.Caesar shift'D','E‘;.etc};8

fid fopen( filename, 'r' ); %Open file filename (read only)%Store filename text in a row vector A of characters, then close fileA fscanf(fid, '%c'); fclose(fid);e.g A ‘The Comedy of Errors .’%Open file for writingfid fopen( strrep( filename,'.txt',[‘-',cipher mode,'.txt'] ), 'w' );%Step through cipher key, replacing instances of the%characters with their plaintext or enciphered equivalentsB A; dim size(cipher key);if strcmp(cipher mode,'encrypt') 1e.g.%Encrypt file contentsplaintext.txtfor n 1:dim(1)would becomeindices strfind( A, cipher key{n,1} );plaintextB(indices) cipher key{n,2};encrypt.txtendelse%Decrypt file contentsfor n 1:dim(1)indices strfind( A, cipher key{n,2} );B(indices) cipher key{n,1};endendMATLAB codefor cipher.m%Write encrypted character array B to a appended, then close filefwrite(fid, B ); fclose(fid);9

Random rectanglesRGB colour modelcolours { [1,0,0], [1,1,0], [0,0,1], [0,0,0] };a 2/( sqrt(5) 1);[ red, green, blue ]for n 1:20x0 rand; x1 x0 rand; y0 a*rand; y1 y0 a*rand;patch( ‘xdata’, [ x0, x1, x1, x0],.‘ydata’, [ y0, y0, y1, y1],.‘FaceColor’, colours{ randi(3,1) } );end10

Composition with Yellow,Blue, and Red1937–42, Piet Mondrian.Oil on canvas; 72.5 x 69 cm.London, Tate Gallery.“a post or support”Randomly generatedfrom mondrian.mPiet Mondrian(1872-1944)De Stijl movement(Amsterdam, 1917-1931)“Neoplasticism”“Ultimate simplicity andabstraction”11

Mondrian14Cut a rectangle randomlyin horizontal and verticaldirections. Randomly divideinto two types1.2.3.4.Choose anintersection atrandomFind nearestintersection whichhas the same ycoordinateFind the nearestintersection fromthis which has thesame x coordinateConstruct arectangle25Shrink the ‘red’type to the blacklines3Find coordinatesof all line segmentintersectionsRepeat from 4cycling through red, blueand yellow colours12

Soundsnipper GUI12Snip a time segment of a.wav or .mp3 audio file12Amplitude vs time ofRight and Left stereochannelsPlayback speed orreverse optionsBrings up a‘choose file’windowCan you guess the tune?What You See Is What You NeedPerforms the snipand writes out a newaudio file13

Gaston Julia(1893-1978)

Mandlebrot transformations of complex numbersi 2 1z x iyzn 1 z z02nx Re( z )y Im( z )z4 1.8 0.8iThe Arganddiagramz x2 y 2(1 i )(1 i ) 1 2i i 1 2i 1 2i2z0 0.5 0.3iz1 0.66 0.6iz2 0.58 1.1iz3 0.4 1.6i15

julia.m plot option abs divergePlot a surface withheight h(x,y). This isthe iteration numberwhen z exceeds acertain value e.g. 4In this case colours indicateheight h(x,y). It is a ‘colour-map’.julia.m plot option plot zPlot a surface withheight h(x,y)x Re( z ), y Im( z )h ( x, y ) e- x2 y 216

Mandlebrot deep zoom (YouTube)The Mandlebrot Set hasinfinite complexity!. But a recursive fractalgeometryBenoit Mandlebrot (1924-2010)17

amaxan 4n 8Count the green squares thatcontain the pointsamaxan nlog n18

The Mandleplant slurping complexityfrom the Argand plane (!)

20

Much scarier than the cyclops in the Odyssey!

The light bulbzn 1 log ( z z0 )2n

7 steps to enlightenmentzn 1 tan 1(z2n z0 )

The Mandlerocket!zn 1 sin 1(z2n z0 )

Micro mandlebeastzn 1 ( z z0 )2n2

The profusion of powerzn 1 ( z z0 )2nzn

Remember h(x,y) isa surface .zn 1 z z02nx Re( z ), y Im( z )h ( x, y ) e- x2 y 2

zn 1 z z02nx Re( z ), y Im( z )h ( x, y ) e- x2 y 2

zn 1 z z02nx Re( z ), y Im( z )h ( x, y ) e- x2 y 2

Selection from Day of Julia. Mathematicon Exhibition, 2014

7 steps to enlightenmentzn 1 tan 1(z2n z0 )x Re( z ), y Im( z )h ( x, y ) e- x2 y 2

The Mandlerocketzn 1 sin 1(z2n z0 )x Re( z ), y Im( z )h ( x, y ) e- x2 y 2

34

35

MovieMovieHD36

R f (azi, elev)TransparencyR radiusazi longitudeelev latitude37

38

Atomic dragon spiralMathematicon, 201439

Klein bottlewith cloudyholestransparencymap40

Texture map41

42

The Harmonograph was aVictorian curiosity attributedto Professor Blackburn in 1844 Use two or three pendulums tocreate strange and beautifulpatternsExample of a lateralharmonographPenPaperPendulum 2Pendulum 1Photo from The Science Museum43

45

46

47

48

49

50

51

52

Rotary harmonograph with frequency dampingyx

Musical harmony The mathematics of music has been knownsince the time of Pythagoras, 2500 years ago Frequency intervals of simple fractions e.g. 3:2(a fifth) yield ‘harmonious’ music An octave means a frequency ratio of 2. Anoctave above concert A (440Hz) is therefore880Hz. An octave below is 220Hz. The modern ‘equal-tempered scale’ divides anoctave (the frequency ratio 2) into twelve partssuch thatnFn 2n / 12 21254

Musical harmony55

Represent musical harmoniesvisually with the harmonograph!octaveRotaryF 2, D 0.7,A 1, phi 0RotaryF 2.01, D 0.7,A 1, phi 0Note thedifference asmallchange in Fmakes.fifthRotaryF 3/2, D 0.7,A 1, phi 0RotaryF 1.51, D 0.7,A 1, phi 056

What You See Is What You NeedPDF output57

‘High productivity multi-tasking,’or are you just being distracted?checkme!58

What YYou See Is Whathat You NeedThe rise of Apps forSmartphones.Typicallysoftware designed for avery specific purpose. and a market for59your designs

Some final wisdom from Donald Knuth“The psychological profiling of aprogrammer is mostly the ability toshift levels of abstraction, from lowlevel to high level. To see somethingin the small and to see something inthe large.”“Email is a wonderful thing for people whoserole in life is to be on top of things. But not forme; my role is to be on the bottom of things.What I do takes long hours of studying anduninterruptible concentration.”60

Summary & QuestionsFast numerical calculation display systemsUse of computerprogramming as anartistic toolfunctionsinterfacesWhere you candesign and refinethe toolComplexity, and beauty, from simplicity(i.e. code)Case studiesGeneral thoughts on how humans bestinteract with information technologyWhat You See Is What You Need!61

Inspired?All welcome, regardless of prior experience62

a computer. Art is everything else we “Computer programming is an art, because it applies accumulated knowledge to the world, because it requires skill and ingenuity, and especially because it produces objects of beauty. A programmer who subconsciously do.” Donald Knuth 1938-Stanford University 3 views himself as an artist will enjoy what he