当前位置: 首页 > news >正文

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)

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

http://www.xdnf.cn/news/286795.html

相关文章:

  • AI小智本地前后端部署
  • Web Workers 技术详解与最佳实践
  • Kubernetes(k8s)学习笔记(七)--KubeSphere 最小化安装
  • webpack 的工作流程
  • 备忘录模式(Memento Pattern)
  • 56.[前端开发-前端工程化]Day03-webpack构建工具
  • Windows11 VS code 安装 Cline 调用 Github MCP 配置过程坑点汇总
  • 深入探索 51 单片机:从入门到实践的全面指南
  • ctfshow——web入门361~368
  • 电脑怎么分屏操作?
  • Gradio全解20——Streaming:流式传输的多媒体应用(5)——基于WebRTC的摄像头实时目标检测
  • N-Gram 模型
  • 慢sql处理流程和常见案例
  • Webug4.0靶场通关笔记16- 第20关文件上传(截断上传)
  • 数据结构——算法复杂度
  • 部署GM DC Monitor 一体化监控预警平台
  • Python 整理3种查看神经网络结构的方法
  • 3DGS-slam:splatam公式
  • 开源模型应用落地-qwen模型小试-Qwen3-8B-推理加速-vLLM(一)
  • Git 标签管理
  • 【STM32 学习笔记】GPIO输入与输出
  • Scrapy分布式爬虫实战:高效抓取的进阶之旅
  • 【NLP】30. 深入理解 In-Context Learning 的核心机制与策略
  • PrivKV: Key-Value Data Collection with Local Differential Privacy论文阅读
  • vue+element 导航 实现例子
  • HarmonyOS Device Connector(hdc)
  • linux 中inotify与inode的关系是什么?
  • PandasAI:对话式数据分析新时代
  • 实战设计模式之中介者模式
  • 基于Boost库、Jsoncpp、cppjieba、cpp-httplib等构建Boost搜索引擎