comparison flys-artifacts/src/main/java/org/dive4elements/river/jfree/JFreeUtil.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java@080411ac948f
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
1 package org.dive4elements.river.jfree;
2
3 import java.awt.Shape;
4 import java.awt.geom.Rectangle2D;
5 import java.util.Iterator;
6 import java.util.Random;
7
8 import org.apache.log4j.Logger;
9 import org.jfree.chart.entity.ChartEntity;
10 import org.jfree.chart.entity.EntityCollection;
11 import org.w3c.dom.Document;
12
13 import org.dive4elements.river.artifacts.math.Function;
14
15 public class JFreeUtil {
16
17 private static final Logger logger = Logger.getLogger(JFreeUtil.class);
18
19 /** Do not instantiate. */
20 private JFreeUtil() {
21 }
22
23
24 /**
25 * True if \param hotspot collides with a Entity in \param entities.
26 * @param hotspot Shape to compare against other shapes (bounds only).
27 * @param entities entities against which to compare shape.
28 * @param exclusiveEntityClass If not null, consider only entities of
29 * given class.
30 * @return true if a collision (non-zero intersection) exists between
31 * shapes.
32 */
33 public static boolean collides(Shape hotspot, EntityCollection entities,
34 Class exclusiveEntityClass) {
35 if (entities == null) return false;
36
37 Rectangle2D hotspotBox = hotspot.getBounds2D();
38
39 for (Iterator i = entities.iterator(); i.hasNext(); ) {
40 Object next = i.next();
41 ChartEntity entity = (ChartEntity) next;
42 if (exclusiveEntityClass == null
43 || exclusiveEntityClass.isInstance(entity))
44 {
45 if (entity.getArea().intersects(hotspotBox)) {
46 // Found collision, early stop.
47 return true;
48 }
49 }
50 }
51
52 return false;
53 }
54
55
56 /**
57 * This function samples a randomized line that contains of x and y values
58 * between <i>startX</i>, <i>endX</i>, <i>startY</i> and <i>endY</i>. The
59 * number of points in the line is specified by <i>num</i>.
60 *
61 * @param num The number of points in the line.
62 * @param startX The min value of the x values.
63 * @param endX The max value of the x values.
64 * @param startY The min value of the y values.
65 * @param endY The max value of the y values.
66 * @return an array with [allX-values, allY-values].
67 * @throws IllegalArgumentException
68 */
69 public static double[][] randomizeLine(
70 int num,
71 double startX,
72 double endX,
73 double startY,
74 double endY
75 ) throws IllegalArgumentException
76 {
77 if (num <= 0) {
78 throw new IllegalArgumentException("Parameter 'num' has to be > 0");
79 }
80
81 Random random = new Random();
82
83 double[] x = new double[num];
84 double[] y = new double[num];
85
86 for (int i = 0; i < num; i++) {
87 double xFac = random.nextDouble();
88 double yFac = random.nextDouble();
89
90 x[i] = startX + xFac * (endX - startX);
91 y[i] = startY + yFac * (endY - startY);
92
93 logger.debug("Created new point: " + x[i] + "|" + y[i]);
94 }
95
96 return new double[][] { x, y };
97 }
98
99
100 public static StyledXYSeries sampleFunction2D(
101 Function func,
102 Document theme,
103 String seriesKey,
104 int samples,
105 double start,
106 double end
107 ) {
108 StyledXYSeries series = new StyledXYSeries(seriesKey, theme);
109
110 double step = (end - start) / (samples - 1);
111
112 for (int i = 0; i < samples; i++) {
113 double x = start + (step * i);
114 series.add(x, func.value(x));
115 }
116
117 return series;
118 }
119
120 public static StyledXYSeries sampleFunction2DPositive(
121 Function func,
122 Document theme,
123 String seriesKey,
124 int samples,
125 double start,
126 double end
127 ) {
128 StyledXYSeries series = new StyledXYSeries(seriesKey, theme);
129
130 double step = (end - start) / (samples - 1);
131
132 for (int i = 0; i < samples; i++) {
133 double x = start + (step * i);
134 double v = func.value(x);
135 if (x > 0d && v > 0d) {
136 series.add(x, v);
137 }
138 }
139
140 return series;
141 }
142 }
143 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org