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:Main.java

/**
 * Waypoint projection using haversine formula
 * http://en.wikipedia.org/wiki/Haversine_formula See discussion here for
 * further information: http://www.movable-type.co.uk/scripts/latlong.html
 *///w w w.  ja v a 2  s  .c o  m
public static double[] project(double distance, double bearing, double startLat, double startLon) {

    double distanceRad = distance / EARTH_RADIUS_KM;
    double bearingRad = Math.toRadians(bearing);
    double startLatRad = Math.toRadians(startLat);
    double startLonRad = Math.toRadians(startLon);

    double endLat = Math.asin(Math.sin(startLatRad) * Math.cos(distanceRad)
            + Math.cos(startLatRad) * Math.sin(distanceRad) * Math.cos(bearingRad));

    double endLon = startLonRad
            + Math.atan2(Math.sin(bearingRad) * Math.sin(distanceRad) * Math.cos(startLatRad),
                    Math.cos(distanceRad) - Math.sin(startLatRad) * Math.sin(endLat));

    // Adjust projections crossing the 180th meridian:
    double endLonDeg = Math.toDegrees(endLon);

    if (endLonDeg > 180 || endLonDeg < -180) {
        endLonDeg = endLonDeg % 360; // Just in case we circle the earth
                                     // more than once.
        if (endLonDeg > 180) {
            endLonDeg = endLonDeg - 360;
        } else if (endLonDeg < -180) {
            endLonDeg = endLonDeg + 360;
        }
    }

    double[] endCoords = new double[] { Math.toDegrees(endLat), endLonDeg };

    return endCoords;

}

From source file:Main.java

public void paint(Graphics g) {
    super.paintComponent(g);
    int radius = 40;
    int centerX = 50;
    int centerY = 100;

    Polygon p = new Polygon();
    centerX = 150;/*from w w  w. j ava2s  . co m*/
    for (int i = 0; i < 5; i++)
        p.addPoint((int) (centerX + radius * Math.cos(i * 2 * Math.PI / 5)),
                (int) (centerY + radius * Math.sin(i * 2 * Math.PI / 5)));

    g.fillPolygon(p);

}

From source file:DrawPolyPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Polygon p = new Polygon();
    for (int i = 0; i < 5; i++)
        p.addPoint((int) (100 + 50 * Math.cos(i * 2 * Math.PI / 5)),
                (int) (100 + 50 * Math.sin(i * 2 * Math.PI / 5)));

    g.drawPolygon(p);/* w w  w . j  av  a  2 s. c om*/

    Polygon s = new Polygon();
    for (int i = 0; i < 360; i++) {
        double t = i / 360.0;
        s.addPoint((int) (150 + 50 * t * Math.cos(8 * t * Math.PI)),
                (int) (150 + 50 * t * Math.sin(8 * t * Math.PI)));
    }
    g.drawPolygon(s);
}

From source file:MainClass.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;

    int w = getSize().width - 1;
    int h = getSize().height - 1;

    for (int i = 0; i < 12; i++) {
        double angle = Math.PI / 2 - i * Math.PI / 6;
        double x = Math.cos(angle);
        double y = Math.sin(angle);

        Line2D l = new Line2D.Double(100 + 55.0 * x, 100 - 55.0 * y, 100 + 65.0 * x, 100 - 65.0 * y);

        g2.draw(l);//w ww .ja  v  a2  s  .  co m
    }
}

From source file:Main.java

public static double[] wgs84togcj02(double lng, double lat) {
    if (out_of_china(lng, lat)) {
        return new double[] { lng, lat };
    }/* w w w .  j a v a  2s . co m*/
    double dlat = transformlat(lng - 105.0, lat - 35.0);
    double dlng = transformlng(lng - 105.0, lat - 35.0);
    double radlat = lat / 180.0 * pi;
    double magic = Math.sin(radlat);
    magic = 1 - ee * magic * magic;
    double sqrtmagic = Math.sqrt(magic);
    dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * pi);
    dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * pi);
    double mglat = lat + dlat;
    double mglng = lng + dlng;
    return new double[] { mglng, mglat };
}

From source file:Main.java

/**
 * Returns the distance between two given locations in meters.
 *
 * @param loc1 First location object/*from w ww . jav a2  s  .c om*/
 * @param loc2 Second location object
 * @return distance between Loc1 & Loc2 in meters.
 */
public static float getDistance(Location loc1, Location loc2) {
    double lat1 = loc1.getLatitude();
    double lng1 = loc1.getLongitude();
    double lat2 = loc2.getLatitude();
    double lng2 = loc2.getLongitude();

    double earthRad = 6371; //kilometers
    double dLatitude = Math.toRadians(lat2 - lat1);
    double dLongitude = Math.toRadians(lng2 - lng1);
    double a = Math.sin(dLatitude / 2) * Math.sin(dLatitude / 2) + Math.cos(Math.toRadians(lat1))
            * Math.cos(Math.toRadians(lat2)) * Math.sin(dLongitude / 2) * Math.sin(dLongitude / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    float dist = (float) (earthRad * c);

    dist = dist * MILES_TO_METER_CONVERSION;

    return dist;
}

From source file:com.jennifer.ui.util.MathUtil.java

public static double rotateX(double x, double y, double radian) {
    return x * Math.cos(radian) - y * Math.sin(radian);
}

From source file:FillPolyPanel.java

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    int radius = 40;
    int centerX = 50;
    int centerY = 100;
    int angle = 30;

    int dx = (int) (radius * Math.cos(angle * Math.PI / 180));
    int dy = (int) (radius * Math.sin(angle * Math.PI / 180));

    g.fillArc(centerX - radius, centerY - radius, 2 * radius, 2 * radius, angle, 360 - 2 * angle);

    Polygon p = new Polygon();
    centerX = 150;/*from  w  ww  .  j av  a 2s . com*/
    for (int i = 0; i < 5; i++)
        p.addPoint((int) (centerX + radius * Math.cos(i * 2 * Math.PI / 5)),
                (int) (centerY + radius * Math.sin(i * 2 * Math.PI / 5)));

    g.fillPolygon(p);

    p = new Polygon();
    centerX = 250;
    for (int i = 0; i < 360; i++) {
        double t = i / 360.0;
        p.addPoint((int) (centerX + radius * t * Math.cos(8 * t * Math.PI)),
                (int) (centerY + radius * t * Math.sin(8 * t * Math.PI)));
    }
    g.fillPolygon(p);
}

From source file:org.eclipse.swt.snippets.Snippet209.java

static void drawTorus(GL2 gl, float r, float R, int nsides, int rings) {
    float ringDelta = 2.0f * (float) Math.PI / rings;
    float sideDelta = 2.0f * (float) Math.PI / nsides;
    float theta = 0.0f, cosTheta = 1.0f, sinTheta = 0.0f;
    for (int i = rings - 1; i >= 0; i--) {
        float theta1 = theta + ringDelta;
        float cosTheta1 = (float) Math.cos(theta1);
        float sinTheta1 = (float) Math.sin(theta1);
        gl.glBegin(GL2.GL_QUAD_STRIP);/*from   w  ww.ja  v  a 2  s.c o m*/
        float phi = 0.0f;
        for (int j = nsides; j >= 0; j--) {
            phi += sideDelta;
            float cosPhi = (float) Math.cos(phi);
            float sinPhi = (float) Math.sin(phi);
            float dist = R + r * cosPhi;
            gl.glNormal3f(cosTheta1 * cosPhi, -sinTheta1 * cosPhi, sinPhi);
            gl.glVertex3f(cosTheta1 * dist, -sinTheta1 * dist, r * sinPhi);
            gl.glNormal3f(cosTheta * cosPhi, -sinTheta * cosPhi, sinPhi);
            gl.glVertex3f(cosTheta * dist, -sinTheta * dist, r * sinPhi);
        }
        gl.glEnd();
        theta = theta1;
        cosTheta = cosTheta1;
        sinTheta = sinTheta1;
    }
}

From source file:Main.java

public static BufferedImage create(BufferedImage image, double angle, GraphicsConfiguration gc) {
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = image.getWidth(), h = image.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    int transparency = image.getColorModel().getTransparency();
    BufferedImage result = gc.createCompatibleImage(neww, newh, transparency);
    Graphics2D g = result.createGraphics();
    g.translate((neww - w) / 2, (newh - h) / 2);
    g.rotate(angle, w / 2, h / 2);/* w  w w . jav a  2s .  co  m*/
    g.drawRenderedImage(image, null);
    return result;
}