| 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 |
package dr.geo; |
| 27 |
|
| 28 |
import dr.math.MathUtils; |
| 29 |
import dr.math.distributions.MultivariateNormalDistribution; |
| 30 |
|
| 31 |
import javax.swing.*; |
| 32 |
import java.awt.*; |
| 33 |
import java.awt.geom.AffineTransform; |
| 34 |
import java.awt.geom.Ellipse2D; |
| 35 |
import java.awt.geom.GeneralPath; |
| 36 |
import java.awt.geom.Point2D; |
| 37 |
import java.util.ArrayList; |
| 38 |
import java.util.List; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class VisualizeBrownianBridge2D extends JComponent { |
| 45 |
|
| 46 |
MultivariateNormalDistribution mnd; |
| 47 |
|
| 48 |
|
| 49 |
SpaceTime[] start, end; |
| 50 |
|
| 51 |
SpaceTimeRejector rejector; |
| 52 |
|
| 53 |
List<Shape> shapes; |
| 54 |
|
| 55 |
Paint shapeColor = Color.GRAY; |
| 56 |
|
| 57 |
Point2D topLeft; |
| 58 |
Point2D bottomRight; |
| 59 |
|
| 60 |
double scaleX; |
| 61 |
double scaleY; |
| 62 |
|
| 63 |
|
| 64 |
public VisualizeBrownianBridge2D() { |
| 65 |
|
| 66 |
start = new SpaceTime[]{new SpaceTime(0, new double[]{0, 0})}; |
| 67 |
end = new SpaceTime[]{new SpaceTime(1, new double[]{1, 1})}; |
| 68 |
|
| 69 |
topLeft = new Point2D.Double(-0.2, -0.2); |
| 70 |
bottomRight = new Point2D.Double(1.2, 1.2); |
| 71 |
|
| 72 |
shapes = new ArrayList<Shape>(); |
| 73 |
shapes.add(new Ellipse2D.Double(0.25, 0.25, 0.4, 0.4)); |
| 74 |
shapes.add(new Ellipse2D.Double(0.5, 0.7, 0.2, 0.2)); |
| 75 |
shapes.add(new Ellipse2D.Double(0.8, 0.2, 0.15, 0.15)); |
| 76 |
|
| 77 |
rejector = new SpaceTimeRejector() { |
| 78 |
|
| 79 |
ArrayList<Reject> rejects = new ArrayList<Reject>(); |
| 80 |
|
| 81 |
public boolean reject(double time, double[] space) { |
| 82 |
double x = space[0]; |
| 83 |
double y = space[1]; |
| 84 |
for (Shape s : shapes) { |
| 85 |
if (s.contains(x, y)) { |
| 86 |
rejects.add(new Reject(0, time, space)); |
| 87 |
return true; |
| 88 |
} |
| 89 |
} |
| 90 |
return false; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
public void reset() { |
| 95 |
rejects.clear(); |
| 96 |
} |
| 97 |
|
| 98 |
public List<Reject> getRejects() { |
| 99 |
return rejects; |
| 100 |
} |
| 101 |
}; |
| 102 |
|
| 103 |
mnd = new MultivariateNormalDistribution(new double[]{0.0}, new double[][]{{10, 0}, {0, 10}}); |
| 104 |
} |
| 105 |
|
| 106 |
public void setShapeColor(Color c) { |
| 107 |
shapeColor = c; |
| 108 |
} |
| 109 |
|
| 110 |
void computeScales() { |
| 111 |
scaleX = getWidth() / (bottomRight.getX() - topLeft.getX()); |
| 112 |
scaleY = getHeight() / (bottomRight.getY() - topLeft.getY()); |
| 113 |
} |
| 114 |
|
| 115 |
public void paintComponent(Graphics g) { |
| 116 |
|
| 117 |
System.out.println("entering paintComponent()"); |
| 118 |
|
| 119 |
computeScales(); |
| 120 |
|
| 121 |
Graphics2D g2d = (Graphics2D) g; |
| 122 |
|
| 123 |
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); |
| 124 |
|
| 125 |
g2d.setStroke(new BasicStroke(1.5f)); |
| 126 |
|
| 127 |
System.out.println("Painting shapes"); |
| 128 |
for (Shape s : shapes) { |
| 129 |
System.out.print("."); |
| 130 |
System.out.flush(); |
| 131 |
GeneralPath path = new GeneralPath(s); |
| 132 |
path.transform(getFullTransform()); |
| 133 |
g2d.setPaint(shapeColor); |
| 134 |
g2d.fill(path); |
| 135 |
} |
| 136 |
|
| 137 |
AffineTransform transform = getFullTransform(); |
| 138 |
|
| 139 |
for (int r = 0; r < getTrials(); r++) { |
| 140 |
Color c = new Color((float) MathUtils.nextDouble(), (float) MathUtils.nextDouble(), (float) MathUtils.nextDouble()); |
| 141 |
|
| 142 |
for (int s = 0; s < start.length; s++) { |
| 143 |
List<SpaceTime> points = null; |
| 144 |
g2d.setPaint(c); |
| 145 |
int topLevelRejects = -1; |
| 146 |
while (points == null) { |
| 147 |
topLevelRejects += 1; |
| 148 |
points = MultivariateBrownianBridge.divideConquerBrownianBridge(mnd, start[s], end[s], getMaxDepth(), getMaxTries(), rejector); |
| 149 |
} |
| 150 |
|
| 151 |
GeneralPath path = new GeneralPath(); |
| 152 |
path.moveTo((float) points.get(0).getX(0), (float) points.get(0).getX(1)); |
| 153 |
|
| 154 |
for (int i = 1; i < points.size(); i++) { |
| 155 |
path.lineTo((float) points.get(i).getX(0), (float) points.get(i).getX(1)); |
| 156 |
|
| 157 |
} |
| 158 |
|
| 159 |
path.transform(getFullTransform()); |
| 160 |
|
| 161 |
g2d.draw(path); |
| 162 |
|
| 163 |
g2d.setPaint(Color.black); |
| 164 |
|
| 165 |
SpaceTime.paintDot(start[s], 3, transform, g2d); |
| 166 |
SpaceTime.paintDot(end[s], 3, transform, g2d); |
| 167 |
|
| 168 |
} |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
System.out.println("leaving paintComponent()"); |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
AffineTransform getScale() { |
| 178 |
return AffineTransform.getScaleInstance(scaleX, scaleY); |
| 179 |
} |
| 180 |
|
| 181 |
AffineTransform getTranslate() { |
| 182 |
return AffineTransform.getTranslateInstance(-topLeft.getX(), -topLeft.getY()); |
| 183 |
} |
| 184 |
|
| 185 |
AffineTransform getFullTransform() { |
| 186 |
AffineTransform transform = getScale(); |
| 187 |
transform.concatenate(getTranslate()); |
| 188 |
return transform; |
| 189 |
} |
| 190 |
|
| 191 |
public static void main(String[] args) { |
| 192 |
|
| 193 |
JFrame frame = new JFrame("Boulders"); |
| 194 |
frame.getContentPane().add(BorderLayout.CENTER, new VisualizeBrownianBridge2D()); |
| 195 |
frame.setSize(600, 600); |
| 196 |
frame.setVisible(true); |
| 197 |
} |
| 198 |
|
| 199 |
public int getMaxDepth() { |
| 200 |
return 10; |
| 201 |
} |
| 202 |
|
| 203 |
public int getTrials() { |
| 204 |
|
| 205 |
return 10; |
| 206 |
} |
| 207 |
|
| 208 |
public int getMaxTries() { |
| 209 |
return 10; |
| 210 |
} |
| 211 |
} |