C#代码:基于两点计算圆并使其靠近点集且保证点集在圆外
以下是C#代码实现,用于计算过给定两个点的圆,并且圆尽量靠近给定的点集合,同时保证点集合在圆外: ```C# using System; using System.Collections.Generic; public class Point { public double X { get; set; } public double Y { get; set; } } public class Circle { public Point Center { get; set; } public double Radius { get; set; } public Circle(Point center, double radius) { Center = center; Radius = radius; } } public static class CircleUtils { public static Circle CalculateCircle(Point point1, Point point2, List<Point> points) { Point midPoint = new Point { X = (point1.X + point2.X) / 2, Y = (point1.Y + point2.Y) / 2 }; double radius = Math.Sqrt(Math.Pow(point1.X - midPoint.X, 2) + Math.Pow(point1.Y - midPoint.Y, 2)); foreach (var point in points) { double distanceToCenter = Math.Sqrt(Math.Pow(point.X - midPoint.X, 2) + Math.Pow(point.Y - midPoint.Y, 2)); if (distanceToCenter <= radius) { double offsetX = midPoint.X - point.X; double offsetY = midPoint.Y - point.Y; double distance = Math.Sqrt(offsetX * offsetX + offsetY * offsetY); radius = (radius + distance) / 2; } } return new Circle(midPoint, radius); } } public class Program { public static void Main(string[] args) { Point point1 = new Point { X = 1, Y = 2 }; Point point2 = new Point { X = 3, Y = 4 }; List<Point> pointSet = new List<Point> { new Point { X = 1, Y = 4 }, new Point { X = 2, Y = 3 }, new Point { X = 5, Y = 6 }, new Point { X = 7, Y = 8 } }; Circle circle = CircleUtils.CalculateCircle(point1, point2, pointSet); Console.WriteLine($"Circle Center: ({circle.Center.X}, {circle.Center.Y})"); Console.WriteLine($"Circle Radius: {circle.Radius}"); } } ``` 在上述代码中,我们首选计算两个给定点的中点,然后计算此中点与其中一个点之间的距离来获得初始半径。接着我们遍历给定的点集合,如果点在当前半径之内,则根据点到中点的距离更新半径。返回一个代表圆的对象,包含中点和半径。 在程序的Main方法中,我提供了一个示例来测试这个方法,包括给定的两个点和一个点集合。输出将打印圆的中心坐标和半径。 ######[AI写代码神器 | 1593点数解答 | 2024-10-30 08:52:24]
- C++ 求解:n 个不爽事连续 m 个刺痛值和的最小值(字节豆包 | 241点数解答 | 2025-11-24 19:52:43)106
- Python 实现:轻松将不同进制数(2 - 16 进制)转换为十进制数 (字节豆包 | 336点数解答 | 2026-01-02 19:45:07)61
- “田忌赛马”新解:如何在固定顺序下赢最多轮次?(DeepSeek | 848点数解答 | 2026-01-11 12:47:40)58
- C++ 解决序列或与和差询问问题:代码实现及优化要点(字节豆包 | 232点数解答 | 2025-01-08 19:10:29)476
- C++ 求解序列数字或与和之差:含代码及注意要点(字节豆包 | 244点数解答 | 2025-01-08 19:10:30)299
- C++求解序列或与运算和差值:避O(nq)复杂度及数据处理要点(字节豆包 | 534点数解答 | 2025-10-17 20:05:07)74
- C++ 解决序列按位运算询问问题:注意数据范围、输入输出效率与运算符优先级(字节豆包 | 410点数解答 | 2025-10-17 20:05:52)64
- C++ 实现:计算一排鹅与最高、最矮鹅的身高差及代码详解(字节豆包 | 549点数解答 | 2025-11-16 20:19:06)105
- C++实现:根据三个整数关系输出对应结果(plus/minus/illegal)(DeepSeek | 98点数解答 | 2025-12-15 21:37:25)81
- Python 帮小杨算出 N 个储蓄罐 D 天后的存钱金额!(字节豆包 | 162点数解答 | 2026-01-02 19:41:59)42
- 与田忌赛马:如何排兵布阵赢最多轮次?代码揭秘!(字节豆包 | 430点数解答 | 2026-01-10 20:35:40)40
- 班级分组难题:最少修改多少同学 id 实现公平配对?(DeepSeek | 1604点数解答 | 2026-01-11 13:28:28)69