圆线交点

2023-11-26

public static ArrayList<IntPoint> getCircleLineIntersectionPoint(IntPoint pointA, IntPoint pointB, IntPoint center, int radius) {
    // returns a list of intersection points between a line which passes through given points,
    // pointA and pointB, and a circle described by given radius and center coordinate

    double disc, A, B, C, slope, c;
    double x1, x2, y1, y2;
    IntPoint point1, point2;
    ArrayList<IntPoint> intersections = new ArrayList<IntPoint>();  
    try{
        slope = Util.calculateSlope(pointA, pointB);
    }catch (UndefinedSlopeException e){         
        C =  Math.pow(center.y, 2) + Math.pow(pointB.x, 2) - 2 * pointB.x * center.x + Math.pow(center.x, 2) - Math.pow(radius, 2);
        B = -2 * center.y;
        A = 1;
        disc = Math.pow(B, 2) - 4 * 1 * C;
        if (disc < 0){
            return intersections;
        }
        else{
            y1 = (-B + Math.sqrt(disc)) / (2 * A);
            y2 = (-B - Math.sqrt(disc)) / (2 * A);

            x1 = pointB.x;
            x2 = pointB.x;
        }
        point1 = new IntPoint((int)x1, (int)y1);
        point2 = new IntPoint((int)x2, (int)y2);
        if (Util.euclideanDistance(pointA,  point2) > Util.euclideanDistance(pointA, point1)){
            intersections.add(point1);
        }
        else{
            intersections.add(point2);
        }
        return intersections;
    }
    if (slope == 0){
        C =  Math.pow(center.x, 2)  + Math.pow(center.y, 2) + Math.pow(pointB.y, 2) - 2 * pointB.y * center.y  - Math.pow(radius, 2);
        B = -2 * center.x;
        A = 1;
        disc = Math.pow(B, 2) - 4 * 1 * C;
        if (disc < 0){
            return intersections;
        }
        else{
            x1 = (-B + Math.sqrt(disc)) / (2*A);
            x2 = (-B - Math.sqrt(disc)) / (2*A);
            y1 = pointB.y;
            y2 = pointB.y;
        }
    }
    else{
        c = slope * pointA.x + pointA.y;
        B = (2 * center.x + 2 * center.y * slope  + 2 * c * slope);
        A = 1 + Math.pow(slope, 2);
        C = (Math.pow(center.x, 2) + Math.pow(c, 2) + 2 * center.y * c + Math.pow(center.y, 2) - Math.pow(radius, 2));
        disc = Math.pow(B, 2) - (4 * A * C);

        if (disc < 0){
            return intersections;
        }
        else{
            x1 = (-B + Math.sqrt(disc)) / (2 * A);
            x2 = (-B - Math.sqrt(disc)) / (2 * A);

            y1 = slope * x1 - c;
            y2 = slope * x2 - c;
        }
    }

    point1 = new IntPoint((int)x1, (int)y1);
    point2 = new IntPoint((int)x2, (int)y2);
    if (Util.euclideanDistance(pointA,  point2) > Util.euclideanDistance(pointA, point1)){
        //if (Util.angleBetween(pointA, pointB, point1) < Math.PI/2){
            intersections.add(point1);
        //}
    }
    else{
        //if (Util.angleBetween(pointA, pointB, point1) < Math.PI/2){
            intersections.add(point2);
        //}
    }       
    return intersections;
}

我正在使用上述算法来测试圆和直线之间的交点。它有时工作正常,但有时却失败。该代码表示​​从圆方程和直线方程同时求解 x 得出的方程(x-a)^+(y-b)^2=r^2 and y = mx - mx1 + y1。有谁知道我在数学或其他方面出了问题吗?


您的计算似乎很长,而且我没有看到您测试的不同案例的用途。 不管怎样,因为我发现这个问题很有趣,所以我尝试自己解决它并提出了以下建议。随意更换double radius by int radius并使用IntPoints,但请注意,每次转换时,如评论中所述,不是精确整数交点的结果都会出错。

执行计算的背景是这样的:从 A 点,矢量 AB 的缩放版本指向圆上的一点。该点具有距中心的距离半径。因此,|AC + 缩放因子 * 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)));
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

圆线交点 的相关文章

  • JNA - EnumProcessModules() 未返回所有 DLL?

    我试图从游戏中读取坐标 当我在通过 OpenProcess 接收的 HANDLE 上使用 ReadProcessMemory 以及我在 CheatEngine 中找到的内存时 效果非常好 例如 如果我知道正在运行的进程中的浮点值是0x5AB
  • Java 字符串哈希码缓存

    字符串不变性的优点之一是哈希码缓存以实现更快的访问 在这种情况下 如何处理具有相同哈希码的字符串的缓存 在这种情况下它真的能提高性能吗 在这种情况下 如何处理具有相同哈希码的字符串的缓存 被缓存的是字符串的哈希码 它被缓存在私有的int字符
  • jvm中本机代码如何转换为机器代码[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我读过一些文章说 jvm将字节码转换为机器码 jvm将字节码转换为本机代码 jvm 将字节码转换为系统调用 系统调用又由操作系统与硬件
  • 使用正则表达式验证输入字符串是否为 0-255 之间的数字

    我在将输入字符串与正则表达式匹配时遇到问题 我想验证输入数字在 0 255 之间并且长度最多应为 3 个字符 代码工作正常 但当我输入 000000 至任意长度时 显示 true 而不是 false 这是我的代码 String IP 000
  • 如何为java注释处理器编写自动化单元测试?

    我正在尝试使用 java 注释处理器 我可以使用 JavaCompiler 编写集成测试 事实上我现在正在使用 hickory 我可以运行编译过程并分析输出 问题 即使我的注释处理器中没有任何代码 单个测试也会运行大约半秒 对于以 TDD
  • 使用 Hibernate Dialect 设置表字符集/排序规则?

    我使用 Hibernate MySQLInnoDB Dialect 来生成 DDL hibernate cfg xml
  • AffineTransform.rotate() - 如何同时缩放、旋转和缩放?

    我有以下代码 它可以完成我想要绘制一个上面有一些棋子的棋盘的 第一部分 Image pieceImage getImage currentPiece int pieceHeight pieceImage getHeight null dou
  • Android 游戏偶尔出现延迟

    我正在用 Java 制作一个简单的 Android 游戏 我注意到每 20 40 秒就会出现一些烦人的延迟 首先 我认为它们是由垃圾收集器引起的 但当我检查 LogCat 时 我发现游戏滞后时没有垃圾收集 每当游戏开始滞后时 我都会标记日志
  • 我需要一个字数统计程序[关闭]

    这个问题不太可能对任何未来的访客有帮助 它只与一个较小的地理区域 一个特定的时间点或一个非常狭窄的情况相关 通常不适用于全世界的互联网受众 为了帮助使这个问题更广泛地适用 访问帮助中心 help reopen questions 我需要弄清
  • spring mvc 跟踪引用页面

    在基于注释的弹簧控制器中 如果用户正在url com first page并点击一个链接或提交一份表格指出url com second page 如何制作second page知道url of first page所以这样second pa
  • 为什么我要使用责任链而不是 switch 语句

    考虑一下您已经获得了多次验证 仅当要检查的对象属于某种类型时 这些验证才应生效 为什么我要使用责任链而不是 switch 语句 责任链示例 public class Executor Inject private ValidatorFact
  • java中日期转换dd-MMM-yyyy到dd-MM-yyyy

    在Java中将23 Mar 2011转换为23 03 2011的最简单方法是什么 感谢大家 这似乎解决了这个问题 try Calendar cal Calendar getInstance cal setTime new SimpleDat
  • 使用 Box2d(适用于 Android)进行碰撞检测?

    有人可以解释一下使用 box2d for android 进行碰撞检测的工作原理吗 我无法理解 BBContactListener 以什么方式工作 BBContactListener listener new BBContactListen
  • Java字符串查找和替换的最佳方法?

    我正在寻找 Java 中字符串查找和替换的最佳方法 这是一句话 我的名字叫米兰 人们都知道我叫米兰瓦西奇 我想用 Milan Vasic 替换 Milan 弦 但在我已经有 Milan Vasic 的地方 情况不应该是这样 搜索 替换后的结
  • 春季MVC。方法参数字段的默认值

    我有一个带有方法测试的简单控制器 RequestMapping produces application json ResponseBody public HttpEntity
  • 如何在速度模板中检索哈希图值

    如何从速度模板中的以下哈希图中检索值 请帮忙 LinkedHashMap
  • Lucene/Hibernate 搜索锁定异常

    我使用 Hibernate Search 在 Web 应用程序上索引和全文搜索项目 没有问题 来自我的 pom xml
  • 使用正则表达式匹配阿拉伯文文本

    我试图使用正则表达式仅匹配阿拉伯语文本 但出现异常 这是我的代码 txt matches P Arabic 这是例外情况 线程 main 中的异常 java util regex PatternSyntaxException 索引 9 附近
  • Retrofit 2.0:预期为 BEGIN_OBJECT,但在第 1 行第 1 列路径 $ [重复] 处为 STRING

    这个问题在这里已经有答案了 我在邮递员上传递了更新用户请求并获得了成功的响应 参见图片 现在当我尝试使用 Retrofit 2 在我的应用程序中执行相同操作时 出现错误 com google gson JsonSyntaxException
  • 获取Java中ResultSet返回的行数

    我用过一个ResultSet返回一定数量的行 我的代码是这样的 ResultSet res getData if res next System out println No Data Found while res next code t

随机推荐

  • Objective-C 中的异步调用

    我正在尝试从网站 xml 获取数据 一切正常 但是 UIButton 一直处于按下状态 直到返回 xml 数据 因此如果互联网服务出现问题 则无法纠正 并且应用程序实际上无法使用 以下是通话内容 AppDelegate appDelegat
  • Android studio 1.5.1:找不到属性“vectorDrawables”

    我正在使用 Android Studio 1 5 1 Gradle 2 8 和我的项目最小 sdk 版本 14 目标 sdk 版本 23 所以 当我通过 Google 文档将 vectorDrawables 添加到配置时 添加了 Vecto
  • 如何在字符串集合中高效查找指定长度的相同子串?

    我有一个收藏S 通常包含 10 50 个长字符串 出于说明目的 假设每个字符串的长度范围在 1000 到 10000 个字符之间 我想找到指定长度的字符串k 通常在 5 到 20 的范围内 是每个字符串的子串S 显然 这可以使用一种简单的方
  • UIButton改变位置

    我在 IB 中设置了一个按钮 我设置了 IBOutlet 并链接到它的屏幕对象 有没有办法以编程方式更改按钮位置和 或大小 我知道您可以更改标题和某些内容 但我不知道如何更改其位置或大小 现在我想相应地改变它的位置 是否可以 如果是 请告诉
  • Javascript:找到字符串中最长的单词

    function longestWord string var str string split var longest 0 var word null for var i 0 i lt str length 1 i if longest
  • 如何从剔除绑定中获取 DOM 元素?

    我已经使用淘汰赛将 DOM 元素绑定到 viewModel 当我更改底层模型上的属性时 它会更改 DOM 一切正常 但是 有没有办法获取绑定的 DOM 元素 以便当底层模型从外部更新时我可以向其中添加一个类 我使用了自定义绑定 它使我可以访
  • 什么时候可以完全优化掉 volatile 变量?

    考虑这个代码示例 int main void volatile int a static volatile int b volatile int c c 20 static volatile int d d 30 volatile int
  • 无法在 android studio 中创建 AVD

    我是安卓新手 我刚刚安装了 Android Studio 并创建了一个默认项目 现在我正在尝试创建 AVD 但无法创建 确定按钮未启用 请帮忙 附截图 问题是你没有安装系统镜像 请检查下图 要启动 SDK 管理器 请单击 Android S
  • 如何整合 Flask 和 Scrapy?

    我正在使用 scrapy 来获取数据 我想使用 Flask Web 框架在网页中显示结果 但我不知道如何调用烧瓶应用程序中的蜘蛛 我尝试过使用CrawlerProcess呼叫我的蜘蛛 但我收到这样的错误 ValueError ValueEr
  • 我无法查看 Android 版 Firebase Crashlytics 的崩溃详细信息,并卡在“构建并运行您的应用程序”上

    我在完成 Firebase 设置后尝试设置 Firebase Crashlytics 崩溃已成功发送 我可以在 firebase 仪表板中看到它们 但是当我进入 crashlytics 查看崩溃详细信息时 我陷入了第 3 步 构建并运行您的
  • iTerm 2 不支持 .tmux.conf 中声明的键绑定

    我正在使用最新稳定版本的 iTerm2 2 0 和最新的 Homebrew 版本的 tmux 1 9a 不幸的是 我的键绑定声明于 tmux conf不能在 集成模式 下使用 iTerm2 tmux 组合 即当 iTerm2 接管 tmux
  • 使用 Apache POI 进行低内存写入/读取

    我正在尝试编写一个相当大的 XLSX 文件 4M 单元 但遇到一些内存问题 我无法使用 SXSSF 因为我还需要读取模板中的现有单元格 我可以做些什么来减少内存占用吗 也许将流读和流写结合起来 为了用低内存处理大数据 最好的也是我认为唯一的
  • Linux 上的哪个库中有系统调用,该库如何链接到包含系统调用的可执行目标文件?

    我知道系统调用不在C 标准库中 是否有系统调用所在的库 某种系统库 如果有这样的库 这个库如何链接到可执行程序 A 系统调用可以以几种不同的方式工作 具体取决于目标架构 但无论如何 它是not图书馆的电话 它是正在运行的用户空间程序调用内核
  • 从静态库创建共享库时保留所有导出的符号

    我正在从静态库创建一个共享库 但我没有源代码 许多 Stack Overflow 问题都提供了answers关于如何做到这一点 gcc shared o libxxx so Wl whole archive libxxx a Wl no w
  • AngularJS 页面之间传递数据 + 页面刷新

    我试图在应用程序的结账过程中在页面之间传递数据 但它没有按应有的方式工作 我已经阅读了一些内容 大多数人建议使用服务 但唯一的问题是 当刷新页面时 用户单击刷新或稍后返回 服务中的所有数据都会消失 这是有道理的 因为服务中的数据并不意味着是
  • Java / 将 ISO-8601 (2010-12-16T13:33:50.513852Z) 转换为 Date 对象

    如何解析一个字符串ISO 8601格式与祖鲁时间 javax xml bind DatatypeConverter parseDateTime 2010 12 16T13 33 50 513852Z returns IllegalArgum
  • LibGDX 中 music.class 的 TweenAccessor

    我一直想知道 是否可以使用 LibGDX 中的通用 Tween 引擎来 例如 更改歌曲的音量 我用类似于我的 SpriteAccessor 的代码编写了自己的 MusicAccessor 它实际上适用于 Sprite class 但当涉及到
  • 在 PyCharm 中运行时,Tkinter 窗口显示为黑色

    无论我如何指定背景颜色 Tkinter 背景在运行脚本上都显示为黑色 我在 macOS 12 2 1 上使用 PyCharm CE 2021 3 2 Python解释器 Python 3 8 有5个包 如下 枕头9 0 1 未来0 18 2
  • sprintf() 如何防止 SQL 注入?

    我听说过sprintf 防止 SQL 注入 这是真的吗 如果是这样 怎么办 为什么人们建议这样编写查询 sql sprintf SELECT FROM TABLE WHERE COL1 s AND COL2 s col1 col2 spri
  • 圆线交点

    public static ArrayList