1 / 21

To use macros in this file…

To use macros in this file…. Reduce your security settings by changing Tools → Options → Security → Macro Security to Medium, and restart PowerPoint. Start a default project in Visual Basic (the default name is Project1, don't change it)

michel
Download Presentation

To use macros in this file…

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. To use macros in this file… Reduce your security settings by changing Tools → Options → Security → Macro Security to Medium, and restart PowerPoint. Start a default project in Visual Basic (the default name is Project1, don't change it) Set form size to be 9000 by 9000, and set AutoRedraw to be True.

  2. Visual BasicLoops

  3. Loops are to avoid punishment Private Sub Form_Click() Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" Form1.Print "I will not pass notes in class" End Sub

  4. The better way to Repeat Private Sub Form_Click() For n=1 to 10 Form1.Print "I will not pass notes in class" Next n End Sub

  5. Private Sub Form_Click() Form1.Print "1 - I will not" Form1.Print "2 - I will not" Form1.Print "3 - I will not" Form1.Print "4 - I will not" Form1.Print "5 - I will not" Form1.Print "6 - I will not" Form1.Print "7 - I will not" Form1.Print "8 - I will not" Form1.Print "9 - I will not" Form1.Print "10 - I will not" End Sub Private Sub Form_Click() For n=1 to 10 Form1.Print n; Form1.Print "- I will not" Next n End Sub Repeating with one change

  6. Writing Modular CodeFunctions and Subs

  7. Private Sub Form_Click() DrawBox DrawLines End Sub Private Sub DrawBox() Form1.Line (1000, 1000)-(1000, 8000) Form1.Line (1000, 8000)-(8000, 8000) Form1.Line (8000, 8000)-(8000, 1000) Form1.Line (8000, 1000)-(1000, 1000) End Sub Private Sub DrawLines() Form1.Line (1000, 7000)-(2000, 1000) Form1.Line (1000, 6000)-(3000, 1000) Form1.Line (1000, 5000)-(4000, 1000) Form1.Line (1000, 4000)-(5000, 1000) Form1.Line (1000, 3000)-(6000, 1000) Form1.Line (1000, 2000)-(7000, 1000) End Sub Top –Down Approach

  8. Modular Code • Increases Understandability • Giving long "imperative" (that means commands as given by an imperial master) names makes for easy to follow programs • Allows Code Reusability • Improves Maintainability

  9. Private Sub Form_Click() DrawBox DrawLines End Sub Private Sub DrawBox() Form1.Line (1000, 1000)-(1000, 8000) Form1.Line (1000, 8000)-(8000, 8000) Form1.Line (8000, 8000)-(8000, 1000) Form1.Line (8000, 1000)-(1000, 1000) End Sub Private Sub DrawLines() For i=1000 to 8000 step 1000 Form1.Line (1000, 9000 - i)-(1000, i) Next i End Sub OOPS! The long code was… Private Sub DrawLines() Form1.Line (1000, 7000)-(2000, 1000) Form1.Line (1000, 6000)-(3000, 1000) Form1.Line (1000, 5000)-(4000, 1000) Form1.Line (1000, 4000)-(5000, 1000) Form1.Line (1000, 3000)-(6000, 1000) Form1.Line (1000, 2000)-(7000, 1000) End Sub Use loops (Be wasteful)

  10. Private Sub Form_Click() DrawBox DrawLines End Sub Private Sub DrawBox() Form1.Line (1000, 1000)-(1000, 8000) Form1.Line (1000, 8000)-(8000, 8000) Form1.Line (8000, 8000)-(8000, 1000) Form1.Line (8000, 1000)-(1000, 1000) End Sub Private Sub DrawLines() For i=1000 to 8000 step 1000 Form1.Line (1000, 9000 - i)-(I, 1000) Next i End Sub The long code was… Private Sub DrawLines() Form1.Line (1000, 7000)-(2000, 1000) Form1.Line (1000, 6000)-(3000, 1000) Form1.Line (1000, 5000)-(4000, 1000) Form1.Line (1000, 4000)-(5000, 1000) Form1.Line (1000, 3000)-(6000, 1000) Form1.Line (1000, 2000)-(7000, 1000) End Sub Use loops (Corrected)

  11. Private Sub Form_Click() DrawBox 1000, 8000 DrawBox 2000, 7000 DrawBox 3000, 6000 DrawBox 4000, 5000 End Sub Private Sub DrawBox(a, b) Form1.Line (a, a)-(a, b) Form1.Line (a, b)-(b, b) Form1.Line (b, b)-(b, a) Form1.Line (b, a)-(a, a) End Sub Remember it used to be Private Sub DrawBox() Form1.Line (1000, 1000)-(1000, 8000) Form1.Line (1000, 8000)-(8000, 8000) Form1.Line (8000, 8000)-(8000, 1000) Form1.Line (8000, 1000)-(1000, 1000) End Sub Use Arguments (not fights)

  12. Modular Code • Increases Understandability • Allows Code Reusability • Writing a flexible general purpose Sub allows you to reuse the same code many times in the same program. • You can also copy the same Sub to other programs if it does a useful thing. • Documentation is important • Improves Maintainability

  13. Documentation ' Draws a square on Form1 with one corner at (a, a) ' and the other corner at (b, b) Private Sub DrawBox(a, b) Form1.Line (a, a)-(a, b) Form1.Line (a, b)-(b, b) Form1.Line (b, b)-(b, a) Form1.Line (b, a)-(a, a) End Sub

  14. Private Sub Form_Click() DrawBox 1000, 8000 DrawLines 1000, 8000, 100 End Sub Private Sub DrawBox(a, b) Form1.Line (a, a)-(a, b) Form1.Line (a, b)-(b, b) Form1.Line (b, b)-(b, a) Form1.Line (b, a)-(a, a) End Sub Private Sub DrawLines(a,b,s) For i=a to b step s Form1.Line (a, a + b - i)-(i, a) Next i End Sub Use Arguments (not fights)

  15. Dividing up a line Suppose a line is (1,2)-(5,9) Mid-point is (3, 5.5), but if five equal pieces? 1.0, 1.8, 2.6, 3.4, 4.2, 5.0 2.0, 3.4, 4.8, 6.2, 7.6, 9.0 The kth point in n pieces from (x0,y0)-(x1,y1) is given by the formula x = x0 + (x1 - x0)*k/n y = y0 + (y1 - y0)*k/nwhere k can be from 0 to n

  16. Private Sub Form_Click() Form1.Line (1000, 2000)-(5000, 9000) Form1.Circle (1000, 2000), 100 Form1.Circle (1800, 3400), 100 Form1.Circle (2600, 4800), 100 Form1.Circle (3400, 6200), 100 Form1.Circle (4200, 7600), 100 Form1.Circle (5000, 9000), 100 End Sub Sub midx(k, n, x0, x1) x = x0 + (x1 - x0) * k / n End Sub Sub midy(k, n, y0, y1) y = y0 + (y1 - y0) * k / n End Sub Private Sub Form_Click() Form1.Line (1000, 2000)-(5000, 9000) For k = 0 To 5 midx k, 5, 1000, 5000 midy k, 5, 2000, 9000 Form1.Circle (x, y), 100 Next k End Sub Local Variables are Local

  17. Dim x, y Sub midx(k, n, x0, x1) x = x0 + (x1 - x0) * k / n End Sub Sub midy(k, n, y0, y1) y = y0 + (y1 - y0) * k / n End Sub Private Sub Form_Click() Form1.Line (1000, 2000)-(5000, 9000) For k=0 to 5 midx k, 5, 1000, 5000 midy k, 5, 2000, 9000 Form1.Circle (x, y), 100 Next k End Sub Function Mid(k, n, a0, a1) Mid = a0 + (a1 - a0) * k / n End Function Private Sub Form_Click() Form1.Line (1000, 2000)-(5000, 9000) For k=0 to 5 x = mid( k, 5, 1000, 5000 ) y = mid( k, 5, 2000, 9000 ) Form1.Circle (x, y), 100 Next k End Sub Global variables  – Functions 

  18. Modular Code • Increases Understandability • Allows Code Reusability • Improves Maintainability • If same code is repeated many places, and corrections are made, it is easy to forget correcting all the places

  19. Private Sub Form_Click() ax = 1000: ay = 1000 bx = 1000: by = 8000 cx = 8000: cy = 1000 dx = 1000: dy = 1000 For k=0 to 50 x0 = Mid(k, 50, ax, bx) y0 = Mid(k, 50, ay, by) x1 = Mid(k, 50, cx, dx) y1 = Mid(k, 50, cy, dy) Line (x0, y0)-(x1, y1) Next k End Sub Function Mid(k, n, a0, a1) Mid = a0 + (a1 - a0) * k / n End Function Drawing our Web

  20. Private Sub Form_Click() ' Web 10, 1000, 1000, 1000, 8000, 8000, 1000, 1000, 1000 a = Rnd * 9000 b = Rnd * 9000 c = Rnd * 9000 d = Rnd * 9000 e = Rnd * 9000 f = Rnd * 9000 g = Rnd * 9000 h = Rnd * 9000 u = 5 + Rnd * 10 Web u, a, b, c, d, e, f, g, h End Sub Sub Web(n, ax, ay, bx, by, cx, cy, dx, dy) For k=0 to n x0 = Mid(k, n, ax, bx) y0 = Mid(k, n, ay, by) x1 = Mid(k, n, cx, dx) y1 = Mid(k, n, cy, dy) Line (x0, y0)-(x1, y1) Next k End Sub Function Mid(k, n, a0, a1) Mid = a0 + (a1 - a0) * k / n End Function Drawing Random Webs

  21. Challenge Start with two different colors. Slowly blend the colors from the first to the last color, using the same Mid function

More Related