to use macros in this file… reduce your security settings by changing tools → options →...

Post on 14-Dec-2015

233 Views

Category:

Documents

4 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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.

Visual BasicLoops

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

Run in VB

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

Run in VB

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

Run Loop

Run in VB

Writing Modular CodeFunctions and Subs

Top –Down ApproachPrivate Sub Form_Click()

DrawBoxDrawLines

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

Run in VB

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

Use loops (Be wasteful)Private Sub Form_Click()

DrawBoxDrawLines

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 iEnd 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

Run in VB

Use loops (Corrected)Private Sub Form_Click()

DrawBoxDrawLines

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 iEnd 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

Run in VB

Use Arguments (not fights)

Private Sub Form_Click() DrawBox 1000, 8000 DrawBox 2000, 7000 DrawBox 3000, 6000 DrawBox 4000, 5000End 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

Run in VB

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

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

Use Arguments (not fights)Private Sub Form_Click() DrawBox 1000, 8000 DrawLines 1000, 8000, 100End 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 iEnd Sub

Run in VB

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

Local Variables are LocalPrivate 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), 100End Sub

Sub midx(k, n, x0, x1) x = x0 + (x1 - x0) * k / nEnd Sub

Sub midy(k, n, y0, y1) y = y0 + (y1 - y0) * k / nEnd 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 kEnd Sub

Run in VB

Run in VB

Global variables – Functions Dim x, ySub midx(k, n, x0, x1) x = x0 + (x1 - x0) * k / nEnd Sub

Sub midy(k, n, y0, y1) y = y0 + (y1 - y0) * k / nEnd 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 kEnd Sub

Function Mid(k, n, a0, a1) Mid = a0 + (a1 - a0) * k / nEnd 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 kEnd Sub

Run in VB

Run in VB

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

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 kEnd Sub

Function Mid(k, n, a0, a1) Mid = a0 + (a1 - a0) * k / nEnd Function

Run in VB

Drawing Random WebsPrivate 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, hEnd 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 kEnd Sub

Function Mid(k, n, a0, a1) Mid = a0 + (a1 - a0) * k / nEnd Function

Run in VB

Challenge

Start with two different colors.

Slowly blend the colors from the first to the last color, using the same Mid function

top related