Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java @ 3938:c0cab28ba1ea
merged flys-artifacts
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:15:03 +0200 |
parents | 5a8f8fd5310c |
children | 080411ac948f |
comparison
equal
deleted
inserted
replaced
3865:436eec3be6ff | 3938:c0cab28ba1ea |
---|---|
1 package de.intevation.flys.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 de.intevation.flys.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 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |