Java Angle Calculate angleToPoint(Rectangle r, double angle)

Here you can find the source of angleToPoint(Rectangle r, double angle)

Description

angle To Point

License

Open Source License

Declaration

public static Point angleToPoint(Rectangle r, double angle) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.awt.*;

import java.awt.geom.Rectangle2D;

public class Main {
    public static Point angleToPoint(Rectangle r, double angle) {
        double si = Math.sin(angle);
        double co = Math.cos(angle);
        double e = 1.0E-4D;
        int x = 0;
        int y = 0;
        if (Math.abs(si) > e) {
            x = (int) ((1.0D + co / Math.abs(si)) / 2.0D * (double) r.width);
            x = range(0, r.width, x);// w  w  w  .j a  v  a 2  s.c o m
        } else if (co >= 0.0D) {
            x = r.width;
        }

        if (Math.abs(co) > e) {
            y = (int) ((1.0D + si / Math.abs(co)) / 2.0D * (double) r.height);
            y = range(0, r.height, y);
        } else if (si >= 0.0D) {
            y = r.height;
        }

        return new Point(r.x + x, r.y + y);
    }

    public static java.awt.geom.Point2D.Double angleToPoint(Rectangle2D.Double r, double angle) {
        double si = Math.sin(angle);
        double co = Math.cos(angle);
        double e = 1.0E-4D;
        double x = 0.0D;
        double y = 0.0D;
        if (Math.abs(si) > e) {
            x = (1.0D + co / Math.abs(si)) / 2.0D * r.width;
            x = range(0.0D, r.width, x);
        } else if (co >= 0.0D) {
            x = r.width;
        }

        if (Math.abs(co) > e) {
            y = (1.0D + si / Math.abs(co)) / 2.0D * r.height;
            y = range(0.0D, r.height, y);
        } else if (si >= 0.0D) {
            y = r.height;
        }

        return new java.awt.geom.Point2D.Double(r.x + x, r.y + y);
    }

    public static int range(int min, int max, int value) {
        if (value < min) {
            value = min;
        }

        if (value > max) {
            value = max;
        }

        return value;
    }

    public static double range(double min, double max, double value) {
        if (value < min) {
            value = min;
        }

        if (value > max) {
            value = max;
        }

        return value;
    }
}

Related

  1. angle(Point2D.Double vec)
  2. angle2D(Point p1, Point p2)
  3. angleBetween(Point2D.Double vec1, Point2D.Double vec2)
  4. angleOf(Point2D a, Point2D b)
  5. anglePI(Point2D top, Point2D corner1, Point2D corner2)
  6. calculateAngle(double dx, double dy)
  7. calculateAngle(float x, float y, float x1, float y1)
  8. calculateAngle(int a1, int b1, int a2, int b2)