GEOG 490/590: GIS Programming
Lab2. VBA GUI Controls and Numerical
Approximation
Due Oct 18 before class.
Introduction:
You will continue taking the
Introduction to VB6 course on ESRI Virtual Campus. Again check the Lab Instructions Update section
in this document for any change in the lab exercise.
Last week you finished Modules 1
and 2. You will be working on Modules 3 and 4 this week. You are not required
to take Modules 5 and 6 in this class. If you enjoyed taking those multiple
choice questions at the end of each module, then you can complete all six
modules. Upon finishing, you will receive a nice electronic certificate from
ESRI. Try your best to answer those tricky questions.
Instructions:
Open the ArcMap VBA IDE and go online to your ESRI training account (http://training.esri.com). Finish modules 3 and 4 of the Introduction to VB6 course. Make sure you try the VB programming techniques mentioned in these modules in ArcMap VBA.
After finishing the online modules, you will design and develop a simple AP that approximates the value of Pi. Pi or π is the ratio of a circle's circumference to its diameter in Euclidean geometry. Alternatively π can be also defined as the ratio of a circle's area to the area of a square whose side is the radius:
Pi = A / r^2 (Equation 1)
The numerical value of π truncated to 50 decimal places is:
3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510
There are many ways to calculate the value of π. One of the more intuitive approaches is based on the second definition mentioned above. You draw an imaginary circle of radius r and a square inscribing the circle (Figure 1). Finding a collection of points inside the square and the circle allows the square’s area (4 * r^2) and the circle's areas (A) to be approximated. Since the area A of a circle is π times the radius squared, π can be approximated by using Equation 1.
To implement this method, you:
1. Generate a huge number of random points (x, y) that fall within a, say,
1000 by 1000 (square unit) area. That is, the range of x and y values is
between 0 and 1000.
2. Check any point (x,y) whose distance d from the origin is less
than r (i.e., 1000). Those
points will be inside the circle. The
distance d is derived by the pythagorean theorem:
d = sqr(x^2 + y^2) (Equation
2)
3. Pi is estimated using the following equation.
Pi = #_points_with_d_less_than_r * 4 / Total_#_of_points_generated
The reason you multiply the numerator by 4 is because you only generate
random points in the first quadrant of the circle. You generate points in the
first quadrant of the square as well, but it is cancelled out because the total
area of the square is 4 * r^2.
Here is the pseudo-code of this algorithm.
Reset the point_in_circle to zero
Loop through the number_of_points
Generate
a set of positive random numbers x and y whose values are smaller than r
Calculate
the distance of x, y to the origin (i.e., 0, 0)
If
the distance is shorter then r then increase the value of point_in_circle by
one
Next point
Pi
= Point_in_circle * 4 / Number_of_points
To improve the accuracy
and precision of the approximation, scientists usually average the results from
multiple runs. You will need to incorporate another loop into your code so that
the AP can average the Pi values derived from multiple runs.
You will use the VB rnd function to generate
random numbers. The rnd function returns a value less than 1 but greater than or equal to zero.
Please look up the function in the online help for more information.
Lab Instructions Update:
In Module 3:
1.
Using a sub
procedure: Note that there is no menu tool in VBA, but you can still get the
main point of this topic.
2.
Skip Add
procedures to the slide show application.
3.
Skip
Working with menus and Microsoft Common Dialog controls and its sub-topics and
exercise.
In Module 4:
1.
Working
with multiple forms: Please note that VBA and VB6 handle multi-form projects
differently. However, VBA still allows multi-form in a project.
2.
Skip Add a
form for setting slide properties.
3.
Using a
standard module (aml_func.bas): You can find the aml_func.bas file in
C:\arcgis\arcexe9x\odetools\ODEObjects folder.
4.
Skip Create
a standard module for input validation exercise.
5.
Working
with 2-D dynamic array exercise: You won’t be able to load the vbp file
and see the demonstration. However, you are required to follow the instructions
and implement the program in VBA.
6.
Skip Add a
collection to the slide show application.
7.
Skip Show
the slides.
Lab Deliverables:
Submit the documents listed below in a zipped file.
1.
Your mxd
file that contains the following features:
1)
A UserForm
that uses the Keyword function in the aml_func.bas module. That is, you need to
write a short program that uses the Keyword function. This demonstrates that
you know how to use a function in VB. Make sure the aml_func.bas is imported to
your project.
2)
A second
UserForm containing the exercise you did in the Working with 2-D dynamic arrays
exercise.
3)
A third
UserForm that features the Pi approximation AP, which has four TextBox controls
and one command button (Figure 2). The TextBox
controls are for entering/displaying: (i) the radius of the circle, (ii) the
number of points generated, (iii) the number of runs, and (iv) the approximated
Pi value. When the command button is clicked, the AP calculates the value of
Pi. Please use labels to label the text boxes in your form as shown in Figure
2. In this AP, you must write a function that calculates the distance
between a random point (x, y) and the origin and use it in your AP.
4)
Make sure
your code is properly commented.
2.
Your
answers to the following questions:
1)
List the
names of all the functions in the aml_func.bas module.
2)
Do an experiment
using different parameters of the Pi approximation AP and fill the blanks in
the table below. Describe how you might get a better approximation of Pi.
|
Approx
# |
r |
Points |
Runs |
Pi |
|
1 |
500 |
500 |
10 |
|
|
2 |
500 |
500 |
100 |
|
|
3 |
500 |
1000 |
10 |
|
|
4 |
500 |
1000 |
100 |
|
|
5 |
1000 |
500 |
100 |
|
|
6 |
1000 |
1000 |
100 |
|
|
7 |
|
|
|
|
|
8 |
|
|
|
|
|
9 |
|
|
|
|
|
10 |
|
|
|
|