view flys-artifacts/src/main/java/de/intevation/flys/jfree/JFreeUtil.java @ 4793:c0d6391bec6f

Fixed facet activities. Return true for active facets only if the facet name is known.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 09 Jan 2013 13:17:09 +0100
parents 080411ac948f
children
line wrap: on
line source
package de.intevation.flys.jfree;

import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.Random;

import org.apache.log4j.Logger;
import org.jfree.chart.entity.ChartEntity;
import org.jfree.chart.entity.EntityCollection;
import org.w3c.dom.Document;

import de.intevation.flys.artifacts.math.Function;

public class JFreeUtil {

    private static final Logger logger = Logger.getLogger(JFreeUtil.class);

    /** 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;
    }


    /**
     * This function samples a randomized line that contains of x and y values
     * between <i>startX</i>, <i>endX</i>, <i>startY</i> and <i>endY</i>. The
     * number of points in the line is specified by <i>num</i>.
     *
     * @param num The number of points in the line.
     * @param startX The min value of the x values.
     * @param endX The max value of the x values.
     * @param startY The min value of the y values.
     * @param endY The max value of the y values.
     * @return an array with [allX-values, allY-values].
     * @throws IllegalArgumentException
     */
    public static double[][] randomizeLine(
        int    num,
        double startX,
        double endX,
        double startY,
        double endY
    ) throws IllegalArgumentException
    {
        if (num <= 0) {
            throw new IllegalArgumentException("Parameter 'num' has to be > 0");
        }

        Random random = new Random();

        double[] x = new double[num];
        double[] y = new double[num];

        for (int i = 0; i < num; i++) {
            double xFac = random.nextDouble();
            double yFac = random.nextDouble();

            x[i] = startX + xFac * (endX - startX);
            y[i] = startY + yFac * (endY - startY);

            logger.debug("Created new point: " + x[i] + "|" + y[i]);
        }

        return new double[][] { x, y };
    }


    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;
    }

    public static StyledXYSeries sampleFunction2DPositive(
        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);
            double v = func.value(x);
            if (x > 0d && v > 0d) {
                series.add(x, v);
            }
        }

        return series;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org