GEOG 490/590: GIS Programming
Lab2. VBA GUI Controls and Numerical Approximation
Due Jan 28 before class.
Introduction:
You will continue taking the “Learning
Visual Basic for Applications for New ArcGIS Developers” course on ESRI Virtual Campus. Last
week you finished Modules 1 and 2. You will be working on Modules 3 and 4 this
week.
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 Learning Visual Basic for Applications for New ArcGIS Developers course. Make sure you try the 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 (A) 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.
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 Deliverables:
Submit
the documents listed below in a zipped file.
1.
Your mxd
file that contains the following features:
1)
A
UserForm containing the student grade database exercise you did in the Work
with arrays exercise in Module 4.
2)
A second
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.
3)
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 |
|
|
|
|