C# 检查某个点是否存在于圆扇区内(Check whether a point exists in circle sector or not)

我们有一个以原点 (0, 0) 为中心的圆。作为输入,我们给出了圆扇区的起始角度和圆扇区的大小(以百分比表示)。

例子:

输入:半径 = 8

起始角 = 0

百分比 = 12

x = 3 y = 4

输出:点 (3, 4) 位于圆

扇区内

输入:半径 = 12

起始角 = 45

百分比 = 25

x = 3 y = 4

输出:点 (3, 4) 不位于

圆扇区内

在此图像中,起始角度为 0 度,半径为 r,假设彩色区域百分比为 12%,则我们计算结束角度为360/百分比 + 起始角度。

为了确定点 (x, y) 是否存在于圆扇区(以原点为中心)内,我们需要找到该点的极坐标,然后执行以下步骤:

1、使用这个将 x, y 转换为极坐标角度 = atan(y/x); 半径 = sqrt(x * x + y * y);

2、那么角度必须介于 StartingAngle(起始角) 和 EndingAngle(终止角) 之间,并且半径必须介于 0 和您的半径之间。

示例代码:

// C# program to check if a point lies

// inside a circle sector.

using System.IO;

using System;

class GFG {

static void checkPoint(int radius, int x, int y,

float percent, float startAngle)

{

// calculate endAngle

float endAngle = 360 / percent + startAngle;

// Calculate polar co-ordinates

float polarradius =

(float)Math.Sqrt(x * x + y * y);

float Angle = (float)Math.Atan(y / x);

// Check whether polarradius is less then

// radius of circle or not and Angle is

// between startAngle and endAngle or not

if (Angle >= startAngle && Angle <= endAngle

&& polarradius < radius)

Console.Write("Point ({0}, {1}) exist in "

  • "the circle sector", x, y);

else

Console.Write("Point ({0}, {1}) does not "

  • "exist in the circle sector", x, y);

}

// Driver code

public static void Main()

{

int radius = 8, x = 3, y = 4;

float percent = 12, startAngle = 0;

checkPoint(radius, x, y, percent, startAngle);

}

}

// This code is contributed by Smitha Dinesh Semwal

输出 :

点(3,4)位于圆扇区内

时间复杂度: O(1)

辅助空间: O(1)

如果您喜欢此文章,请收藏、点赞、评论,谢谢,祝您快乐每一天。

相关推荐
晚枫歌F3 分钟前
最小堆定时器
数据结构·算法
xiaogutou112124 分钟前
2026年历史课件PPT模板选购指南:教师备课效率与精度的平衡方案
开发语言·c#
Lumos_77737 分钟前
Linux -- 线程
java·jvm·算法
七颗糖很甜1 小时前
“十五五”气象发展规划:聚焦五大核心任务
大数据·python·算法
科研前沿1 小时前
镜像视界浙江科技有限公司的关键技术突破有哪些?
大数据·人工智能·科技·算法·音视频·空间计算
嫩萝卜头儿1 小时前
2 - 复杂度收尾 + 链表经典OJ
数据结构·算法·链表·复杂度
星马梦缘2 小时前
算法设计与分析 作业二 答案与解析
算法·图论·dfs·bfs·floyd-warshall·bellman_ford·多源最短路
玛丽莲茼蒿2 小时前
Leetcode hot100 每日温度【中等】
算法·leetcode·职场和发展
cjp5602 小时前
009.UG二次开发,任务环境草图优化3(高级功能生成直线)
算法
样例过了就是过了2 小时前
LeetCode热题100 分割等和子集
数据结构·c++·算法·leetcode·动态规划