Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

circle line intersection java

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class CircleLine {

    public static List<Point> getCircleLineIntersectionPoint(Point pointA,
            Point pointB, Point center, double radius) {
        double baX = pointB.x - pointA.x;
        double baY = pointB.y - pointA.y;
        double caX = center.x - pointA.x;
        double caY = center.y - pointA.y;

        double a = baX * baX + baY * baY;
        double bBy2 = baX * caX + baY * caY;
        double c = caX * caX + caY * caY - radius * radius;

        double pBy2 = bBy2 / a;
        double q = c / a;

        double disc = pBy2 * pBy2 - q;
        if (disc < 0) {
            return Collections.emptyList();
        }
        // if disc == 0 ... dealt with later
        double tmpSqrt = Math.sqrt(disc);
        double abScalingFactor1 = -pBy2 + tmpSqrt;
        double abScalingFactor2 = -pBy2 - tmpSqrt;

        Point p1 = new Point(pointA.x - baX * abScalingFactor1, pointA.y
                - baY * abScalingFactor1);
        if (disc == 0) { // abScalingFactor1 == abScalingFactor2
            return Collections.singletonList(p1);
        }
        Point p2 = new Point(pointA.x - baX * abScalingFactor2, pointA.y
                - baY * abScalingFactor2);
        return Arrays.asList(p1, p2);
    }

    static class Point {
        double x, y;

        public Point(double x, double y) { this.x = x; this.y = y; }

        @Override
        public String toString() {
            return "Point [x=" + x + ", y=" + y + "]";
        }
    }


    public static void main(String[] args) {
        System.out.println(getCircleLineIntersectionPoint(new Point(-3, -3),
                new Point(-3, 3), new Point(0, 0), 5));
        System.out.println(getCircleLineIntersectionPoint(new Point(0, -2),
                new Point(1, -2), new Point(1, 1), 5));
        System.out.println(getCircleLineIntersectionPoint(new Point(1, -1),
                new Point(-1, 0), new Point(-1, 1), 5));
        System.out.println(getCircleLineIntersectionPoint(new Point(-3, -3),
                new Point(-2, -2), new Point(0, 0), Math.sqrt(2)));
    }
Comment

PREVIOUS NEXT
Code Example
Java :: Min value map 
Java :: java event enter key 
Java :: java generic array 
Java :: JAVA_HOME is not defined correctly. 
Java :: java loop backwards through array 
Java :: how to get contacts permission in android 
Java :: load a file from classpath spring boot 
Java :: leetcode patterns 
Java :: how to hide label in bottom menu android studio 
Java :: shift elements in array java 
Java :: how to remove components from a JFRame java 
Java :: arraylist string to string array 
Java :: find duplicate value in array java 
Java :: random password generator java 
Java :: testException = com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field 
Java :: java ModelMapper class 
Java :: system.out.println shortcut 
Java :: mongoose collection name 
Java :: javafx object rectangle 
Java :: install java 8 on windows 10 
Java :: startactivityforresult deprecated android 
Java :: java boolean 2d array 
Java :: java get ip address 
Java :: android get text from string xml programmatically 
Java :: What is a point class in java? 
Java :: how to do stuff with a scoreboard minecraft 
Java :: all devisor of a number java 
Java :: java print hex format 
Java :: java code samples 
Java :: key listener java 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =