Mercurial > dive4elements > river
diff flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 63ef889eea2b |
children | d8f2ab5b61c3 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java Fri Sep 28 12:14:39 2012 +0200 @@ -0,0 +1,75 @@ +package de.intevation.flys.jfree; + +import de.intevation.flys.artifacts.math.Function; + +import java.awt.Shape; + +import java.awt.geom.Rectangle2D; + +import java.util.Iterator; + +import org.jfree.chart.entity.ChartEntity; +import org.jfree.chart.entity.EntityCollection; + +import org.w3c.dom.Document; + +public class JFreeUtil { + + /** Do not instantiate. */ + private JFreeUtil() { + } + + + /** + * True if \param hotspot collides with a Entity in \param entities. + * @param hotspot Shape to compare against other shapes (bounds only). + * @param entities entities against which to compare shape. + * @param exclusiveEntityClass If not null, consider only entities of + * given class. + * @return true if a collision (non-zero intersection) exists between + * shapes. + */ + public static boolean collides(Shape hotspot, EntityCollection entities, + Class exclusiveEntityClass) { + if (entities == null) return false; + + Rectangle2D hotspotBox = hotspot.getBounds2D(); + + for (Iterator i = entities.iterator(); i.hasNext(); ) { + Object next = i.next(); + ChartEntity entity = (ChartEntity) next; + if (exclusiveEntityClass == null + || exclusiveEntityClass.isInstance(entity)) + { + if (entity.getArea().intersects(hotspotBox)) { + // Found collision, early stop. + return true; + } + } + } + + return false; + } + + + public static StyledXYSeries sampleFunction2D( + Function func, + Document theme, + String seriesKey, + int samples, + double start, + double end + ) { + StyledXYSeries series = new StyledXYSeries(seriesKey, theme); + + double step = (end - start) / (samples - 1); + + for (int i = 0; i < samples; i++) { + double x = start + (step * i); + series.add(x, func.value(x)); + } + + return series; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :