translate Point To Circle Border - Java java.lang

Java examples for java.lang:Math Geometry Shape

Description

translate Point To Circle Border

Demo Code


//package com.java2s;
import java.awt.Point;

public class Main {
    public static void translatePointToCircleBorder(Point point,
            Point refPoint, int radius) {

        if (refPoint.x == point.x) {
            if (refPoint.y < point.y) {
                point.y -= radius;//from  ww  w  .jav  a2 s. c om
            } else {
                point.y += radius;
            }
        } else {
            int dx = point.x - refPoint.x;
            int dy = point.y - refPoint.y;
            int sgnx = (dx < 0 ? -1 : (dx > 0 ? 1 : 0));
            int sgny = (dy < 0 ? -1 : (dy > 0 ? 1 : 0));
            float tg = ((float) dy) / dx;
            int mx = (int) Math.round(((float) radius)
                    / Math.sqrt(1 + tg * tg));
            int my = (int) Math.round(((float) radius)
                    / Math.sqrt(1 + (1 / (tg * tg))));
            point.x -= mx * sgnx;
            point.y -= my * sgny;
        }

    }
}

Related Tutorials