Example usage for java.lang Math cos

List of usage examples for java.lang Math cos

Introduction

In this page you can find the example usage for java.lang Math cos.

Prototype

@HotSpotIntrinsicCandidate
public static double cos(double a) 

Source Link

Document

Returns the trigonometric cosine of an angle.

Usage

From source file:edu.uci.ics.jung.visualization.util.VertexShapeFactory.java

/**
 * Returns a regular <code>num_sides</code>-sided 
 * <code>Polygon</code> whose bounding 
 * box's width and height are defined by this instance's size and
 * aspect ratio functions for this vertex.
 * @param num_sides the number of sides of the polygon; must be >= 3.
 *///from www. j a  v a2 s .c om
public Shape getRegularPolygon(V v, int num_sides) {
    if (num_sides < 3)
        throw new IllegalArgumentException("Number of sides must be >= 3");
    Rectangle2D frame = getRectangle(v);
    float width = (float) frame.getWidth();
    float height = (float) frame.getHeight();

    // generate coordinates
    double angle = 0;
    thePolygon.reset();
    thePolygon.moveTo(0, 0);
    thePolygon.lineTo(width, 0);
    double theta = (2 * Math.PI) / num_sides;
    for (int i = 2; i < num_sides; i++) {
        angle -= theta;
        float delta_x = (float) (width * Math.cos(angle));
        float delta_y = (float) (width * Math.sin(angle));
        Point2D prev = thePolygon.getCurrentPoint();
        thePolygon.lineTo((float) prev.getX() + delta_x, (float) prev.getY() + delta_y);
    }
    thePolygon.closePath();

    // scale polygon to be right size, translate to center at (0,0)
    Rectangle2D r = thePolygon.getBounds2D();
    double scale_x = width / r.getWidth();
    double scale_y = height / r.getHeight();
    float translationX = (float) (r.getMinX() + r.getWidth() / 2);
    float translationY = (float) (r.getMinY() + r.getHeight() / 2);

    AffineTransform at = AffineTransform.getScaleInstance(scale_x, scale_y);
    at.translate(-translationX, -translationY);

    Shape shape = at.createTransformedShape(thePolygon);
    return shape;
}

From source file:fr.fg.server.core.TerritoryManager.java

private static BufferedImage createTerritoryMap(int idSector) {
    List<Area> areas = new ArrayList<Area>(DataAccess.getAreasBySector(idSector));

    float[][] points = new float[areas.size()][2];
    int[] dominatingAllies = new int[areas.size()];
    int i = 0;/* ww  w. j a v  a 2 s. com*/
    for (Area area : areas) {
        points[i][0] = area.getX() * MAP_SCALE;
        points[i][1] = area.getY() * MAP_SCALE;
        dominatingAllies[i] = area.getIdDominatingAlly();
        i++;
    }

    Hull hull = new Hull(points);
    MPolygon hullPolygon = hull.getRegion();
    float[][] newPoints = new float[points.length + hullPolygon.count()][2];
    System.arraycopy(points, 0, newPoints, 0, points.length);

    float[][] hullCoords = hullPolygon.getCoords();

    for (i = 0; i < hullPolygon.count(); i++) {
        double angle = Math.atan2(hullCoords[i][1], hullCoords[i][0]);
        double length = Math.sqrt(hullCoords[i][0] * hullCoords[i][0] + hullCoords[i][1] * hullCoords[i][1]);

        newPoints[i + points.length][0] = (float) (Math.cos(angle) * (length + 8 * MAP_SCALE));
        newPoints[i + points.length][1] = (float) (Math.sin(angle) * (length + 8 * MAP_SCALE));
    }

    points = newPoints;

    Voronoi voronoi = new Voronoi(points);
    Delaunay delaunay = new Delaunay(points);

    // Dcoupage en rgions
    MPolygon[] regions = voronoi.getRegions();

    // Calcule le rayon de la galaxie
    int radius = 0;

    for (Area area : areas) {
        radius = Math.max(radius, area.getX() * area.getX() + area.getY() * area.getY());
    }

    radius = (int) Math.floor(Math.sqrt(radius) * MAP_SCALE) + 10 * MAP_SCALE;
    int diameter = 2 * radius + 1;

    // Construit l'image avec les quadrants
    BufferedImage territoriesImage = new BufferedImage(diameter, diameter, BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = (Graphics2D) territoriesImage.getGraphics();

    // Affecte une couleur  chaque alliance
    HashMap<Integer, Color> alliesColors = new HashMap<Integer, Color>();

    for (Area area : areas) {
        int idDominatingAlly = area.getIdDominatingAlly();
        if (idDominatingAlly != 0)
            alliesColors.put(idDominatingAlly,
                    Ally.TERRITORY_COLORS[DataAccess.getAllyById(idDominatingAlly).getColor()]);
    }

    Polygon[] polygons = new Polygon[regions.length];
    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] != 0) {
            polygons[i] = createPolygon(regions[i].getCoords(), radius + 1, 3);
        }
    }

    // Dessine tous les secteurs
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF);

    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] == 0)
            continue;

        Polygon p = polygons[i];

        // Dessine le polygone
        g.setColor(alliesColors.get(dominatingAllies[i]));
        g.fill(p);

        // Rempli les espaces entre les polygones adjacents qui
        // correspondent au territoire d'une mme alliance
        int[] linkedRegions = delaunay.getLinked(i);
        for (int j = 0; j < linkedRegions.length; j++) {
            int linkedRegion = linkedRegions[j];

            if (linkedRegion >= areas.size())
                continue;

            if (dominatingAllies[i] == dominatingAllies[linkedRegion]) {
                if (linkedRegion <= i)
                    continue;

                float[][] coords1 = regions[i].getCoords();
                float[][] coords2 = regions[linkedRegion].getCoords();

                int junctionIndex = 0;
                int[][] junctions = new int[2][2];

                search: for (int k = 0; k < coords1.length; k++) {
                    for (int l = 0; l < coords2.length; l++) {
                        if (coords1[k][0] == coords2[l][0] && coords1[k][1] == coords2[l][1]) {
                            junctions[junctionIndex][0] = k;
                            junctions[junctionIndex][1] = l;

                            junctionIndex++;

                            if (junctionIndex == 2) {
                                int[] xpts = new int[] { polygons[i].xpoints[junctions[0][0]],
                                        polygons[linkedRegion].xpoints[junctions[0][1]],
                                        polygons[linkedRegion].xpoints[junctions[1][1]],
                                        polygons[i].xpoints[junctions[1][0]], };
                                int[] ypts = new int[] { polygons[i].ypoints[junctions[0][0]],
                                        polygons[linkedRegion].ypoints[junctions[0][1]],
                                        polygons[linkedRegion].ypoints[junctions[1][1]],
                                        polygons[i].ypoints[junctions[1][0]], };

                                Polygon border = new Polygon(xpts, ypts, 4);
                                g.setStroke(new BasicStroke(2, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
                                g.fill(border);
                                g.draw(border);
                                break search;
                            }
                            break;
                        }
                    }
                }
            }
        }
    }

    // Dessine des lignes de contours des territoires
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] == 0)
            continue;

        g.setStroke(new BasicStroke(1.5f));
        g.setColor(alliesColors.get(dominatingAllies[i]).brighter().brighter());

        float[][] coords1 = regions[i].getCoords();

        lines: for (int j = 0; j < coords1.length; j++) {
            int[] linkedRegions = delaunay.getLinked(i);
            for (int k = 0; k < linkedRegions.length; k++) {
                int linkedRegion = linkedRegions[k];

                if (linkedRegion >= areas.size())
                    continue;

                if (dominatingAllies[i] == dominatingAllies[linkedRegion]) {
                    float[][] coords2 = regions[linkedRegion].getCoords();

                    for (int m = 0; m < coords2.length; m++) {
                        if (coords1[j][0] == coords2[m][0] && coords1[j][1] == coords2[m][1]
                                && ((coords1[(j + 1) % coords1.length][0] == coords2[(m + 1)
                                        % coords2.length][0]
                                        && coords1[(j + 1) % coords1.length][1] == coords2[(m + 1)
                                                % coords2.length][1])
                                        || (coords1[(j + 1)
                                                % coords1.length][0] == coords2[(m - 1 + coords2.length)
                                                        % coords2.length][0]
                                                && coords1[(j + 1)
                                                        % coords1.length][1] == coords2[(m - 1 + coords2.length)
                                                                % coords2.length][1]))) {
                            continue lines;
                        }
                    }
                }
            }

            g.drawLine(Math.round(polygons[i].xpoints[j]), Math.round(polygons[i].ypoints[j]),
                    Math.round(polygons[i].xpoints[(j + 1) % coords1.length]),
                    Math.round(polygons[i].ypoints[(j + 1) % coords1.length]));
        }

        for (int j = 0; j < coords1.length; j++) {
            int neighbours = 0;
            int lastNeighbourRegion = -1;
            int neighbourCoordsIndex = -1;

            int[] linkedRegions = delaunay.getLinked(i);
            for (int k = 0; k < linkedRegions.length; k++) {
                int linkedRegion = linkedRegions[k];

                if (linkedRegion >= areas.size())
                    continue;

                if (dominatingAllies[i] == dominatingAllies[linkedRegion]) {
                    float[][] coords2 = regions[linkedRegion].getCoords();

                    for (int m = 0; m < coords2.length; m++) {
                        if (coords1[j][0] == coords2[m][0] && coords1[j][1] == coords2[m][1]) {
                            neighbours++;
                            lastNeighbourRegion = linkedRegion;
                            neighbourCoordsIndex = m;
                            break;
                        }
                    }
                }
            }

            if (neighbours == 1) {
                g.drawLine(Math.round(polygons[i].xpoints[j]), Math.round(polygons[i].ypoints[j]),
                        Math.round(polygons[lastNeighbourRegion].xpoints[neighbourCoordsIndex]),
                        Math.round(polygons[lastNeighbourRegion].ypoints[neighbourCoordsIndex]));
            }
        }
    }

    BufferedImage finalImage = new BufferedImage(diameter, diameter, BufferedImage.TYPE_INT_ARGB);

    g = (Graphics2D) finalImage.getGraphics();

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, .15f));
    g.drawImage(territoriesImage, 0, 0, null);
    g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC, .5f));

    // Charge la police pour afficher le nom des alliances
    try {
        Font textFont = Font.createFont(Font.TRUETYPE_FONT,
                Action.class.getClassLoader().getResourceAsStream("fr/fg/server/resources/TinDog.ttf"));
        textFont = textFont.deriveFont(12f).deriveFont(Font.BOLD);
        g.setFont(textFont);
    } catch (Exception e) {
        LoggingSystem.getServerLogger().warn("Could not load quadrant map font.", e);
    }
    FontMetrics fm = g.getFontMetrics();

    ArrayList<Integer> closedRegions = new ArrayList<Integer>();

    for (i = 0; i < areas.size(); i++) {
        if (dominatingAllies[i] == 0 || closedRegions.contains(i))
            continue;

        ArrayList<Integer> allyRegions = new ArrayList<Integer>();
        ArrayList<Integer> openRegions = new ArrayList<Integer>();

        openRegions.add(i);

        while (openRegions.size() > 0) {
            int currentRegion = openRegions.remove(0);
            allyRegions.add(currentRegion);
            closedRegions.add(currentRegion);

            int[] linkedRegions = delaunay.getLinked(currentRegion);

            for (int k = 0; k < linkedRegions.length; k++) {
                int linkedRegion = linkedRegions[k];

                if (linkedRegion >= areas.size() || openRegions.contains(linkedRegion)
                        || allyRegions.contains(linkedRegion))
                    continue;

                if (dominatingAllies[i] == dominatingAllies[linkedRegion])
                    openRegions.add(linkedRegion);
            }
        }

        Area area = areas.get(i);
        long xsum = 0;
        long ysum = 0;

        for (int k = 0; k < allyRegions.size(); k++) {
            int allyRegion = allyRegions.get(k);
            area = areas.get(allyRegion);

            xsum += area.getX();
            ysum += area.getY();
        }

        int x = (int) (xsum / allyRegions.size()) * MAP_SCALE + radius + 1;
        int y = (int) (-ysum / allyRegions.size()) * MAP_SCALE + radius + 1;
        ;

        Point point = new Point(x, y);
        boolean validLocation = false;
        for (int k = 0; k < allyRegions.size(); k++) {
            int allyRegion = allyRegions.get(k);

            if (polygons[allyRegion].contains(point)) {
                validLocation = true;
                break;
            }
        }

        if (validLocation) {
            if (allyRegions.size() == 1)
                y -= 14;
        } else {
            int xmid = (int) (xsum / allyRegions.size());
            int ymid = (int) (ysum / allyRegions.size());

            area = areas.get(i);
            int dx = area.getX() - xmid;
            int dy = area.getY() - ymid;
            int distance = dx * dx + dy * dy;

            int nearestAreaIndex = i;
            int nearestDistance = distance;

            for (int k = 0; k < allyRegions.size(); k++) {
                int allyRegion = allyRegions.get(k);

                area = areas.get(allyRegion);
                dx = area.getX() - xmid;
                dy = area.getY() - ymid;
                distance = dx * dx + dy * dy;

                if (distance < nearestDistance) {
                    nearestAreaIndex = allyRegion;
                    nearestDistance = distance;
                }
            }

            area = areas.get(nearestAreaIndex);
            x = area.getX() * MAP_SCALE + radius + 1;
            y = -area.getY() * MAP_SCALE + radius - 13;
        }

        // Dessine le tag de l'alliance
        String allyTag = "[ " + DataAccess.getAllyById(dominatingAllies[i]).getTag() + " ]";
        g.setColor(Color.BLACK);
        g.drawString(allyTag, x - fm.stringWidth(allyTag) / 2 + 1, y);
        g.setColor(alliesColors.get(dominatingAllies[i]));
        g.drawString(allyTag, x - fm.stringWidth(allyTag) / 2, y);
    }

    return finalImage;
}

From source file:com.alvermont.terraj.planet.project.StereographicProjection.java

/**
 * Carry out the projection//from  w  ww .  j  a  v  a2  s  .c om
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    double x;
    double y;
    double z;
    double x1;
    double y1;
    double z1;
    double ymin;
    double ymax;
    double theta1;
    double theta2;
    double zz;
    int i;
    int j;

    ymin = 2.0;
    ymax = -2.0;

    final double sla = Math.sin(lat);
    final double cla = Math.cos(lat);
    final double slo = Math.sin(lon);
    final double clo = Math.cos(lon);

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        for (i = 0; i < width; ++i) {
            x = ((2.0 * i) - width) / height / scale;
            y = ((2.0 * j) - height) / height / scale;
            z = (x * x) + (y * y);
            zz = 0.25 * (4.0 + z);

            x = x / zz;
            y = y / zz;
            z = (1.0 - (0.25 * z)) / zz;

            x1 = (clo * x) + (slo * sla * y) + (slo * cla * z);
            y1 = (cla * y) - (sla * z);
            z1 = (-slo * x) + (clo * sla * y) + (clo * cla * z);

            if (y1 < ymin) {
                ymin = y1;
            }

            if (y1 > ymax) {
                ymax = y1;
            }

            colours[i][j] = (short) planet0(x1, y1, z1);

            if (doShade) {
                shades[i][j] = shade;
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = Math.sin(Math.toRadians(theta1));

            if ((ymin <= y) && (y <= ymax)) {
                zz = Math.sqrt(1 - (y * y));

                for (theta2 = -Math.PI; theta2 < Math.PI; theta2 += (0.5 / width / scale)) {
                    x = Math.sin(theta2) * zz;
                    z = Math.cos(theta2) * zz;
                    x1 = (clo * x) + (slo * z);
                    y1 = ((slo * sla * x) + (cla * y)) - (clo * sla * z);
                    z1 = (-slo * cla * x) + (sla * y) + (clo * cla * z);

                    if (Math.abs(z1) < 1.0) {
                        i = (int) (0.5
                                * (((height * scale * 2.0 * x1 * (1 + z1)) / (1.0 - (z1 * z1))) + width));
                        j = (int) (0.5
                                * (((height * scale * 2.0 * y1 * (1 + z1)) / (1.0 - (z1 * z1))) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta2 = -Math.PI; theta2 < Math.PI; theta2 += (0.5 / width / scale)) {
            y = Math.sin(theta2);

            if ((ymin <= y) && (y <= ymax)) {
                for (theta1 = 0.0; theta1 < 360.0; theta1 += vgrid) {
                    x = Math.sin(Math.toRadians(theta1)) * Math.cos(theta2);
                    z = Math.cos(Math.toRadians(theta1)) * Math.cos(theta2);

                    x1 = (clo * x) + (slo * z);
                    y1 = ((slo * sla * x) + (cla * y)) - (clo * sla * z);
                    z1 = (-slo * cla * x) + (sla * y) + (clo * cla * z);

                    if (Math.abs(z1) < 1.0) {
                        i = (int) (0.5 * (((height * scale * 2.0 * x1 * (1 + z1)) / (1 - (z1 * z1))) + width));
                        j = (int) (0.5 * (((height * scale * 2.0 * y1 * (1 + z1)) / (1 - (z1 * z1))) + height));

                        if ((0 <= i) && (i < width) && (0 <= j) && (j < height)) {
                            colours[i][j] = BLACK;
                        }
                    }
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}

From source file:edu.snu.leader.hidden.builder.AbstractIndividualBuilder.java

/**
 * Create a valid location for an individual
 *
 * @param index The index of the individual
 * @return The valid location// w  w w . ja va 2 s .  com
 */
protected Vector2D createValidLocation(int index) {
    Vector2D location = null;

    // If we have a location, use it
    if (index < _locations.size()) {
        location = _locations.get(index);
    }
    // Otherwise, generate it
    else {
        // Generate a radius
        float radius = _simState.getRandom().nextFloat() * _maxRadius;

        // Generate an angle
        double angle = (_simState.getRandom().nextDouble() * Math.PI * 2.0) - Math.PI;

        // Convert to cartesian
        float x = radius * (float) Math.cos(angle);
        float y = radius * (float) Math.sin(angle);

        location = new Vector2D(x, y);
    }

    return location;
}

From source file:jat.core.cm.TwoBodyAPL.java

public void propagate(double t0, double tf, Printable pr, boolean print_switch, double steps) {
    double[] temp = new double[6];
    // double ta_save = this.ta;
    this.steps = steps;

    // Determine step size
    double n = this.meanMotion();
    double period = this.period();
    double dt = period / steps;
    if ((t0 + dt) > tf) // check to see if we're going past tf
    {//from w  ww  . ja v  a 2  s  . co  m
        dt = tf - t0;
    }

    // determine initial E and M
    double sqrome2 = Math.sqrt(1.0 - this.e * this.e);
    double cta = Math.cos(this.ta);
    double sta = Math.sin(this.ta);
    double sine0 = (sqrome2 * sta) / (1.0 + this.e * cta);
    double cose0 = (this.e + cta) / (1.0 + this.e * cta);
    double e0 = Math.atan2(sine0, cose0);

    double ma = e0 - this.e * Math.sin(e0);

    // initialize t

    double t = t0;

    if (print_switch) {
        temp = this.randv();
        pr.print(t, temp);
    }

    while (t < tf) {
        ma = ma + n * dt;
        double ea = solveKepler(ma, this.e);

        double sinE = Math.sin(ea);
        double cosE = Math.cos(ea);
        double den = 1.0 - this.e * cosE;

        double sinv = (sqrome2 * sinE) / den;
        double cosv = (cosE - this.e) / den;

        this.ta = Math.atan2(sinv, cosv);
        if (this.ta < 0.0) {
            this.ta = this.ta + 2.0 * Constants.pi;
        }

        t = t + dt;

        temp = this.randv();
        this.rv = new VectorN(temp);

        if (print_switch) {
            pr.print(t, temp);
        }

        if ((t + dt) > tf) {
            dt = tf - t;
        }

    }
    // Reset everything to before
    this.ta = initial_ta;

}

From source file:com.alvermont.terraj.planet.project.SquareProjection.java

/**
 * Carry out the projection//from  ww w.j ava 2 s .  c  o m
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    depth = (3 * ((int) (log2(scale * height)))) + 6;

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    double y;
    double scale1;
    double theta1;
    double cos2;
    int k;
    int i;
    int j;

    k = (int) ((lat * width * scale) / Math.PI);

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        y = ((2.0 * (j - k)) - height) / width / scale * Math.PI;

        if (Math.abs(y) >= (0.5 * Math.PI)) {
            for (i = 0; i < width; ++i) {
                colours[i][j] = backgroundColour;

                if (doShade) {
                    shades[i][j] = 255;
                }
            }
        } else {
            cos2 = Math.cos(y);

            if (cos2 > 0.0) {
                scale1 = (scale * width) / height / cos2 / Math.PI;
                depth = (3 * ((int) (log2(scale1 * height)))) + 3;

                for (i = 0; i < width; ++i) {
                    theta1 = lon - (0.5 * Math.PI) + ((Math.PI * ((2.0 * i) - width)) / width / scale);

                    colours[i][j] = (short) planet0(Math.cos(theta1) * cos2, Math.sin(y),
                            -Math.sin(theta1) * cos2);

                    if (doShade) {
                        shades[i][j] = shade;
                    }
                }
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = Math.toRadians(theta1);

            j = (height / 2) + (int) ((0.5 * y * width * scale) / Math.PI) + k;

            if ((j >= 0) && (j < height)) {
                for (i = 0; i < width; ++i)
                    colours[i][j] = BLACK;
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta1 = 0.0; theta1 > -360.0; theta1 -= vgrid)
            ;

        for (theta1 = theta1; theta1 < 360.0; theta1 += vgrid) {
            i = (int) (0.5 * width * (1.0 + ((scale * (Math.toRadians(theta1) - lon)) / Math.PI)));

            if ((i >= 0) && (i < width)) {
                for (j = Math.max(0,
                        (height / 2) - (int) ((0.25 * Math.PI * width * scale) / Math.PI) + k); j < Math.min(
                                height,
                                (height / 2) + (int) ((0.25 * Math.PI * width * scale) / Math.PI) + k); ++j) {
                    colours[i][j] = BLACK;
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}

From source file:fr.amap.lidar.amapvox.commons.LeafAngleDistribution.java

/**
 * Get the density probability from angle
 * @param angle must be in radians from in [0,2pi]
 * @return pdf function//from   w ww . j  a v a2  s .  c o m
 */
public double getDensityProbability(double angle) {

    double density = 0;

    double tmp = Math.PI / 2.0;
    if (angle == tmp) {
        angle = tmp - 0.000001;
    }

    //angle = Math.PI/2.0 - angle; ??inversion des coefficients

    switch (type) {

    //warning : in wang paper there is an inversion between planophile, and erectophile
    case PLANOPHILE:
        density = (2.0 / Math.PI) * (1 + Math.cos(2 * angle));
        break;
    case ERECTOPHILE:
        density = (2.0 / Math.PI) * (1 - Math.cos(2 * angle));
        break;
    case PLAGIOPHILE:
        density = (2.0 / Math.PI) * (1 - Math.cos(4 * angle));
        break;
    case EXTREMOPHILE:
        density = (2.0 / Math.PI) * (1 + Math.cos(4 * angle));
        break;
    case SPHERIC:
        density = Math.sin(angle);
        break;
    case UNIFORM:
        density = 2.0 / Math.PI;
        break;
    case HORIZONTAL:
        break;
    case VERTICAL:
        break;
    case ELLIPTICAL:
        break;
    case ELLIPSOIDAL:

        double res;

        if (x == 1) {
            res = Math.sin(angle);
        } else {

            double eps, lambda = 0;

            if (x < 1) {
                eps = Math.sqrt(1 - (x * x));
                lambda = x + (Math.asin(eps) / eps);
            }
            if (x > 1) {
                eps = Math.sqrt(1 - Math.pow(x, -2));
                lambda = x + Math.log((1 + eps) / (1 + eps)) / (2 * eps * x);
            }

            res = (2 * Math.pow(x, 3) * Math.sin(angle)) / (lambda
                    * Math.pow((Math.pow(Math.cos(angle), 2)) + (x * x * Math.pow(Math.sin(angle), 2)), 2));
        }

        return res;

    case TWO_PARAMETER_BETA:

        //angle = Math.PI/2.0 - angle;
        double te = 2 * angle / Math.PI;
        te = Double.max(te, 1E-09);
        te = Double.min(te, 1 - 1E-09);

        density = distribution.density(te) / (Math.PI / 2.0);

        break;
    }

    return density;
}

From source file:com.grayfox.server.service.PoiService.java

private double distanceBetween(Location location1, Location location2) {
    double fi1 = Math.toRadians(location1.getLatitude());
    double fi2 = Math.toRadians(location2.getLatitude());
    double deltaLambda = Math.toRadians(location2.getLongitude() - location1.getLongitude());
    return Math.acos(Math.sin(fi1) * Math.sin(fi2) + Math.cos(fi1) * Math.cos(fi2) * Math.cos(deltaLambda))
            * Constants.Ints.EARTH_RADIUS;
}

From source file:de.dakror.villagedefense.game.entity.Entity.java

public void drawEntity(Graphics2D g) {
    if (alpha == 0)
        return;/*from  w  w  w.ja v a  2 s.  c o m*/

    drawBump(g, false);
    Composite c = g.getComposite();
    if (this instanceof Struct)
        g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
    draw(g);
    g.setComposite(c);

    if (isHungry()) {
        g.drawImage(Game.getImage("icon/hunger.png"), (int) x,
                (int) (y - Tile.SIZE - Math.cos(tick / 10f) * Tile.SIZE / 4), 32, 32, Game.w);
    } else if (this instanceof Struct && !((Struct) this).isWorking()) {
        g.drawImage(Game.getImage("icon/sleep.png"), (int) (x + width * 0.75f),
                (int) (y - Tile.SIZE - Math.cos(tick / 10f) * Tile.SIZE / 4), 32, 32, Game.w);
    }

    drawBump(g, true);
}

From source file:com.alvermont.terraj.planet.project.SinusoidProjection.java

/**
 * Carry out the projection//  w  w  w .  j  ava2  s.co  m
 */
public void project() {
    setcolours();

    final int width = getParameters().getProjectionParameters().getWidth();
    final int height = getParameters().getProjectionParameters().getHeight();

    final double lat = getParameters().getProjectionParameters().getLatitudeRadians();
    final double lon = getParameters().getProjectionParameters().getLongitudeRadians();

    final double scale = getParameters().getProjectionParameters().getScale();

    final double hgrid = getParameters().getProjectionParameters().getHgrid();
    final double vgrid = getParameters().getProjectionParameters().getVgrid();

    final boolean doShade = getParameters().getProjectionParameters().isDoShade();

    cacheParameters();

    colours = new short[width][height];
    shades = new short[width][height];

    double y;
    double theta1;
    double theta2;
    double cos2;
    double l1;
    double i1;
    double scale1;
    int k;
    int i;
    int j;
    int l;
    int c;

    k = (int) ((lat * width * scale) / Math.PI);

    progress.progressStart(height, "Generating Terrain");

    for (j = 0; j < height; ++j) {
        progress.progressStep(j);

        y = ((2.0 * (j - k)) - height) / width / scale * Math.PI;

        if (Math.abs(y) >= (0.5 * Math.PI)) {
            for (i = 0; i < width; ++i) {
                colours[i][j] = backgroundColour;

                if (doShade) {
                    shades[i][j] = 255;
                }
            }
        } else {
            cos2 = Math.cos(y);

            if (cos2 > 0.0) {
                scale1 = (scale * width) / height / cos2 / Math.PI;

                depth = (3 * ((int) (log2(scale1 * height)))) + 3;

                for (i = 0; i < width; ++i) {
                    l = (i * 12) / width;
                    l1 = (l * width) / 12.0;
                    i1 = i - l1;

                    theta2 = lon - (0.5 * Math.PI) + ((Math.PI * ((2.0 * l1) - width)) / width / scale);
                    theta1 = ((Math.PI * ((2.0 * i1) - (width / 12))) / width / scale) / cos2;

                    if (Math.abs(theta1) > (Math.PI / 12.0)) {
                        colours[i][j] = backgroundColour;

                        if (doShade) {
                            shades[i][j] = 255;
                        }
                    } else {
                        colours[i][j] = (short) planet0(Math.cos(theta1 + theta2) * cos2, Math.sin(y),
                                -Math.sin(theta1 + theta2) * cos2);

                        if (doShade) {
                            shades[i][j] = shade;
                        }
                    }
                }
            }
        }
    }

    progress.progressComplete("Terrain Generated");

    if (hgrid != 0.0) {
        /* draw horizontal gridlines */
        for (theta1 = 0.0; theta1 > -90.0; theta1 -= hgrid)
            ;

        for (theta1 = theta1; theta1 < 90.0; theta1 += hgrid) {
            y = Math.toRadians(theta1);

            cos2 = Math.cos(y);

            j = (height / 2) + (int) ((0.5 * y * width * scale) / Math.PI) + k;

            if ((j >= 0) && (j < height)) {
                for (i = 0; i < width; ++i) {
                    l = (i * 12) / width;
                    l1 = (l * width) / 12.0;
                    i1 = i - l1;

                    theta2 = ((Math.PI * ((2.0 * i1) - (width / 12))) / width / scale) / cos2;

                    if (Math.abs(theta2) <= (Math.PI / 12.0)) {
                        colours[i][j] = BLACK;
                    }
                }
            }
        }
    }

    if (vgrid != 0.0) {
        /* draw vertical gridlines */
        for (theta1 = 0.0; theta1 > -360.0; theta1 -= vgrid)
            ;

        for (theta1 = theta1; theta1 < 360.0; theta1 += vgrid) {
            i = (int) (0.5 * width * (1.0 + ((scale * (Math.toRadians(theta1) - lon)) / Math.PI)));

            if ((i >= 0) && (i < width)) {
                for (j = Math.max(0,
                        (height / 2) - (int) ((0.25 * Math.PI * width * scale) / Math.PI) + k); j < Math.min(
                                height,
                                (height / 2) + (int) ((0.25 * Math.PI * width * scale) / Math.PI) + k); ++j) {
                    y = ((2.0 * (j - k)) - height) / width / scale * Math.PI;
                    cos2 = Math.cos(y);

                    l = (i * 12) / width;
                    l1 = ((l * width) / 12.0) + (width / 24.0);
                    i1 = i - l1;
                    c = (int) (l1 + (i1 * cos2));

                    if ((c >= 0) && (c < width)) {
                        colours[c][j] = BLACK;
                    }
                }
            }
        }
    }

    if (doShade) {
        smoothshades();
    }

    doOutlining();
}