C#中Math.Atan()与Math.Atan2()的区别

本文对比了Math.Atan与Math.Atan2函数的使用方式与角度范围的不同,详细介绍了四个象限中两者返回的角度值差异,并通过C#代码示例展示了如何根据坐标位置正确使用这两个函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Math.Atan()的范围为Θ=(-π/2,π/2),Math.Atan2()的范围为α=(-π,π)
第一象限x>0,y>0 Θ>0,α>0 Θ=α;
第二象限x<0,y>0 Θ<0,α>0 -Θ+α=180;
第三象限x<0,y<0 Θ>0,α<0 Θ-α=180;
第四象限x>0,y<0 Θ<0,α<0 Θ=α;

using System;

namespace CheckAtan2
{
    class Program
    {
        static void Main(string[] args)
        {
            //第一象限
            double ang1 = Math.Atan(0.5)*180/Math.PI;
            double ang2 = Math.Atan2(1,2)*180/Math.PI;
            Console.WriteLine("ang1:{0}", ang1);
            Console.WriteLine("ang2:{0}", ang2);
            //第二象限
            double ang3 = Math.Atan(-0.5) * 180 / Math.PI;
            double ang4 = Math.Atan2(1,-2) * 180 / Math.PI;
            Console.WriteLine("ang3:{0}", ang3);
            Console.WriteLine("ang4:{0}", ang4);
            //第三象限
            double ang5 = Math.Atan(0.5) * 180 / Math.PI;
            double ang6 = Math.Atan2(-1,-2) * 180 / Math.PI;
            Console.WriteLine("ang5:{0}", ang5);
            Console.WriteLine("ang6:{0}", ang6);
            //第四象限
            double ang7 = Math.Atan(-0.5) * 180 / Math.PI;
            double ang8 = Math.Atan2(-1,2) * 180 / Math.PI;
            Console.WriteLine("ang7:{0}", ang7);
            Console.WriteLine("ang8:{0}", ang8);
        }
    }
}

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值