My favorites | Sign in
Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
* Copyright (C) 2008 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.android.divideandconquer;

import android.util.Log;
import android.content.Context;
import android.widget.Toast;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;


/**
* Keeps track of the current state of balls bouncing around within a a set of
* regions.
*
* Note: 'now' is the elapsed time in milliseconds since some consistent point in time.
* As long as the reference point stays consistent, the engine will be happy, though
* typically this is {@link android.os.SystemClock#elapsedRealtime()}
*/
public class BallEngine {
private final float mMinX;
private final float mMaxX;
private final float mMinY;
private final float mMaxY;

private int mNumBalls = 4;
private float mBallSpeed;
private float mBallRadius;

private Context mContext;

/**
* Holds onto new regions during a split
*/
private List<BallRegion> mNewRegions = new ArrayList<BallRegion>(8);

private List<BallRegion> mRegions = new ArrayList<BallRegion>(8);

public BallEngine(float minX, float maxX,
float minY,
float maxY,
float ballSpeed,
float ballRadius) {
mMinX = minX;
mMaxX = maxX;
mMinY = minY;
mMaxY = maxY;
mBallSpeed = ballSpeed;
mBallRadius = ballRadius;
}

public void setContext(Context mContext) {
this.mContext = mContext;
}

/**
* Update the notion of 'now' in milliseconds. This can be usefull
* when unpausing for instance.
* @param now Milliseconds since some consistent point in time.
*/
public void setNow(long now) {
for (int i = 0; i < mRegions.size(); i++) {
final BallRegion region = mRegions.get(i);
region.setNow(now);
}
}

/**
* Rest the engine back to a single region with a certain number of balls
* that will be placed randomly and sent in random directions.
* @param now milliseconds since some consistent point in time.
* @param numBalls
*/
public void reset(long now, int numBalls) {
mRegions.clear();

ArrayList<Ball> balls = new ArrayList<Ball>(numBalls);
for (int i = 0; i < numBalls; i++) {
Ball ball = new Ball.Builder()
.setNow(now)
.setPixelsPerSecond(mBallSpeed)
.setAngle(Math.random() * 2 * Math.PI)
.setX((float) Math.random() * (mMaxX - mMinX) + mMinX)
.setY((float) Math.random() * (mMaxY - mMinY) + mMinY)
.setRadiusPixels(mBallRadius)
.create();
balls.add(ball);
}
BallRegion region = new BallRegion(now, mMinX, mMaxX, mMinY, mMaxY, balls);

mRegions.add(region);
}

public List<BallRegion> getRegions() {
return mRegions;
}

public float getPercentageFilled() {
float total = 0f;
for (int i = 0; i < mRegions.size(); i++) {
BallRegion region = mRegions.get(i);
total += region.getArea();
Log.d("Balls", "total now " + total);
}
return 1f - (total / getArea());
}

/**
* @return the area in the region in pixel*pixel
*/
public float getArea() {
return (mMaxX - mMinX) * (mMaxY - mMinY);
}

/**
* Can any of the regions within start a line at this point?
* @param x The x coordinate.
* @param y The y coordinate
* @return Whether a region can start a line.
*/
public boolean canStartLineAt(float x, float y) {
for (BallRegion region : mRegions) {
if (region.canStartLineAt(x, y)) {
return true;
}
}
return false;
}

/**
* Start a horizontal line at a certain point.
* @throws IllegalArgumentException if there is no region that can start a
* line at the point.
*/
public void startHorizontalLine(long now, float x, float y) {
for (BallRegion region : mRegions) {
if (region.canStartLineAt(x, y)) {
region.startHorizontalLine(now, x, y);
return;
}
}
throw new IllegalArgumentException("no region can start a new line at "
+ x + ", " + y + ".");
}

/**
* Start a vertical line at a certain point.
* @throws IllegalArgumentException if there is no region that can start a
* line at the point.
*/
public void startVerticalLine(long now, float x, float y) {
for (BallRegion region : mRegions) {
if (region.canStartLineAt(x, y)) {
region.startVerticalLine(now, x, y);
return;
}
}
throw new IllegalArgumentException("no region can start a new line at "
+ x + ", " + y + ".");
}

/**
* @param now The latest notion of 'now'
* @return whether any new regions were added by the update.
* @throws BallHitMovingLineException if a collision is detected
*/
public boolean update(long now) throws BallHitMovingLineException {
boolean regionChange = false;
Iterator<BallRegion> it = mRegions.iterator();
while (it.hasNext()) {
final BallRegion region = it.next();
final BallRegion newRegion = region.update(now);

if (newRegion != null) {
regionChange = true;
if (!newRegion.getBalls().isEmpty()) {
mNewRegions.add(newRegion);
}

// current region may not have any balls left
if (region.getBalls().isEmpty()) {
it.remove();
}
} else if (region.consumeDoneShrinking()) {
regionChange = true;
}
}
mRegions.addAll(mNewRegions);
mNewRegions.clear();

return regionChange;
}
}
Show details Hide details

Change log

r134 by kros...@google.com on Oct 31, 2008   Diff
Added 'shrink to fit', removed EULA.
TODO: the way the main view knows that
the shrinkng of a region is done is kinda
hacky, I will improve that at some point.
Go to: 
Project members, sign in to write a code review

Older revisions

r40 by kros...@google.com on Sep 10, 2008   Diff
initial checkin, see README.txt for
overview
All revisions of this file

File info

Size: 6616 bytes, 208 lines