
ChaosGameApp that has the procedures described below.
This app consists of two parts:
Note thatTouch touch = canvas.waitForTouch(); double touchX = touch.getX(); double touchY = touch.getY();
**touch, touchX, touchY** could be any names of your choice.
Random Integers
Here is how to get a random integer in a given range [a , b]:
Note thatint number = canvas.getRandomInt( 10, 20 ); // picks random integer in [10..20] inclusive
**number** could be any name of your choice.
drawPattern(x1, y1, x2, y2, x3, y3, n)
This procedure takes as parameters:
(x, y) pairs which represent the coordinates of the vertices of a trianglen which represents a number of trials/repetitions; for testing use a big number for n, 10000 or moren:
Pick a random number in [1..3] (inclusive; usecanvas.getRandomInt):
- for 1, move the point of touch halfway towards vertex
(x1, y1)and draw the point at its new position in red- for 2, move the point of touch halfway towards vertex
(x2, y2)and draw the point at its new position in green- for 3, move the point of touch halfway towards vertex
(x3, y3)and draw the point at its new position in blue
(x, y) coordinates of the point get updated to be the average of its current coordinates and the coordinates of the chosen vertex.
Here is an example of a possible execution when the game is run for only 2 moves in the triangle (50, 100), (300, 100), (200, 380) and the player touched point (260,200):
n, 10000 or more.
What types of patterns could you create?
mystery(n)
This procedure computes the sum of the first n >= 0 terms in the following summation:
(Note:-4.0 + 4.0/3 - 4.0/5 + 4.0/7 - 4.0/9 + 4.0/11 - ... and so on
mystery(0) returns 0, since we asked for 0 terms.)
What mystery number is computed by this procedure? Try large values of n and then change the name mystery to a more appropriate name.
double cfrac(int n)
This procedure computes a continued fraction up to depth n >= 0. A continued fraction is an expression of the form given below. (Note: cfrac(0) returns 0.)
Hint: It is easier to work backwards (bottom to top) one fraction per loop cycle.
n = 0 returns: 0 |
n = 1 1 ----- 1 + 0 returns: 1 |
n = 2
1
-------
1
1 + -----
2 + 0
returns: 0.6666667
|
n = 3
1
--------
1
1 + -------
1
2 + -----
3 + 0
return: 0.7
|
n = 4
1
--------
1
1 + --------
1
2 + -------
1
3 + -----
4 + 0
returns: 0.6976744186046512
|
n = 5
1
-------
1
1 + -------
1
2 + -------
1
3 + -------
1
4 + -----
5 + 0
returns: 0.6977777777777778
|
n = 100
1
-------
1
1 + -------
1
2 + -------
.. .. .. ..
1
99 + -------
100 + 0
returns: 0.697774657964008
|
double carfc(int n)
This procedure computes a different continued fraction up to depth n >= 0. (Note: carfc(0) returns 0.)
Hint: It is easier to work backwards (bottom to top) one fraction per loop cycle.
n = 0 returns: 0 |
n = 1 1 ----- 2 + 0 returns: .5 |
n = 2
1
-------
3
2 + -----
4 + 0
returns: 0.36363636363636365
|
n = 3
1
-------
3
2 + -------
5
4 + -----
6 + 0
returns: 0.3815789473684211
|
n = 4
1
-------
3
2 + -------
5
4 + -------
7
6 + -----
8 + 0
returns: 0.3795620437956204
|
n = 5
1
-------
3
2 + -------
5
4 + -------
7
6 + -------
9
8 + ------
10 + 0
returns: 0.37974515529599157
|
n = 100
1
-------
3
2 + -------
5
4 + ---------
.. .. .. ..
199
198 + -------
200 + 0
returns: 0.37973195474099564
|
public void run(), for each procedure give at least three test cases of your own that demonstrate that your code is correct. These could be commented out as we do in class.