栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

圆线相交点

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

圆线相交点

您的计算似乎很长,我看不到您测试的不同案例的使用。无论如何,由于我发现了有趣的问题,所以我尝试自己解决该问题,并提出了以下解决方案。随意更换

doubleradius
int radius
,并使用
IntPoint
S,但是要知道,你每次投,如评论,一点效果都没有准确的整数交点将成为讨论错误的时间。

进行计算的背景是这样的:从点A开始,矢量AB的缩放版本指向圆上的点。该点具有距中心的距离半径。因此,| AC + scaleFactor * AB | =
r。

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)));    }


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/454449.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号