Presentation is loading. Please wait.

Presentation is loading. Please wait.

To use macros in this file… Reduce your security settings by changing Tools → Options → Security → Macro Security to Medium, and restart PowerPoint. Start.

Similar presentations


Presentation on theme: "To use macros in this file… Reduce your security settings by changing Tools → Options → Security → Macro Security to Medium, and restart PowerPoint. Start."— 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 Basic Loops

3 Loops are to avoid punishment Private Sub Form_Click() 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 Repeating with one change 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

6 Writing Modular Code Functions and Subs

7 Top –Down Approach 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

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 Use loops (Be wasteful) 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, 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

10 Use loops (Corrected) 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, 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

11 Use Arguments (not fights) 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

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 Use Arguments (not fights) 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

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, ,3.4,4.8,6.2,7.6,9.0 The k th point in n pieces from (x 0,y 0 )-(x 1,y 1 ) is given by the formulax = x 0 + (x 1 - x 0 )*k/n y = y 0 + (y 1 - y 0 )*k/n where k can be from 0 to n

16 Local Variables are Local 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

17 Global variables  – Functions 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

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 Drawing our Web 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

20 Drawing Random Webs 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

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


Download ppt "To use macros in this file… Reduce your security settings by changing Tools → Options → Security → Macro Security to Medium, and restart PowerPoint. Start."

Similar presentations


Ads by Google