ios中常用函数及使用atan2 判断象限

来源:互联网 发布:手机端口转发 编辑:程序博客网 时间:2024/06/06 05:52


介绍一下Objective-c常用的函数,常数变量

算术函数

【算术函数】

函数名

说明

int rand()

随机数生成。

(例)

srand(time(nil)); //随机数初期化

int val = rand()P; //0~49之间的随机数

int abs(int a)

整数的绝对值

(例)int val = abs(-8);

 →8

※浮点数的时候用fabs。

double fabs(double a)

浮点数的绝对值

(例)double val = fabs(-12.345);

 →12.345

※整数的时候用abs。

double floor(double a)

返回浮点数整数部分(舍弃小数点)

(例)double val = floor(12.345);

 →12.000

double ceil(double a);

返回浮点数整数部分(舍弃小数点部分,往个位数进1)

(例)double val = ceil(12.345);

 →13.000

double pow(double a, double b)

a的b次方

(例)double val = pow(2, 3);

 →8

double sqrt(double a)

a的平方根

(例)double val = sqrt(2);

 →1.41421356

三角函数

【三角函数】

函数名

说明

double cos(double a)

余弦函数 (a:弧度)

double sin(double a)

正弦函数 (a:弧度)

double tan(double a)

正切函数 (a:弧度)

double asin(double a)

反正弦值 (a:弧度)

double acos(double a)

反余弦函数(a:弧度)

double atan(double a)

反正切函数

double atan2(double a, double b)

返回给定的 a 及 b 坐标值的反正切值

指数函数

【指数函数】

函数名

说明

double log(double a)

以e 为底的对数值

double log10(double a)

对数函数log

常数

常数

常数名

说明

M_PI

圆周率(=π)

M_PI_2

圆周率的1/2(=π/2)

M_PI_4

圆周率的1/4(=π/4)

M_1_PI

=1/π

M_2_PI

=2/π

M_E

=e

M_LOG2E

log_2(e)

M_LOG10E

log_10(e)



1、 三角函数 

  double sin (double);正弦 
  double cos (double);余弦 
  double tan (double);正切 
  2 、反三角函数 
  double asin (double); 结果介于[-PI/2, PI/2] 
  double acos (double); 结果介于[0, PI] 
  double atan (double); 反正切(主值), 结果介于[-PI/2, PI/2] 
  double atan2 (double, double); 反正切(整圆值), 结果介于[-PI, PI] 
  3 、双曲三角函数 
  double sinh (double); 
  double cosh (double); 
  double tanh (double); 
  4 、指数与对数 
  double exp (double);求取自然数e的幂 
  double sqrt (double);开平方 
  double log (double); 以e为底的对数 
  double log10 (double);以10为底的对数 
  double pow(double x, double y);计算以x为底数的y次幂 
  float powf(float x, float y); 功能与pow一致,只是输入与输出皆为浮点数 
  5 、取整 
  double ceil (double); 取上整 
  double floor (double); 取下整 
  6 、绝对值 
  double fabs (double);求绝对值 
  double cabs(struct complex znum) ;求复数的绝对值 
  7 、标准化浮点数 
  double frexp (double f, int *p); 标准化浮点数, f = x * 2^p, 已知f求x, p ( x介于[0.5, 1] ) 
  double ldexp (double x, int p); 与frexp相反, 已知x, p求f 
  8 、取整与取余 
  double modf (double, double*); 将参数的整数部分通过指针回传, 返回小数部分 
  double fmod (double, double); 返回两参数相除的余数 
  9 、其他 
  double hypot(double x, double y);已知直角三角形两个直角边长度,求斜边长度 
  double ldexp(double x, int exponent);计算x*(2的exponent次幂) 
  double poly(double x, int degree, double coeffs [] );计算多项式 
  nt matherr(struct exception *e);数学错误计算处理程序
转自http://blog.csdn.net/lwq421336220/article/details/8141150 



atan2(y,x) 所表达的意思是坐标原点为起点,指向(x,y)的射线在坐标平面上与x轴正方向之间的角的角度。

还记得那个十字交叉的y-x轴了吗? 用弧度标示(-PI  ~ PI  ),要想返回角度的话,需要*180/M_PI

判断在第几象限的话可以通过返回来的角度来判断

0-90   第一象限

90-180  第二象限

-180-  -90 第三象限

-90 - 0 第四象限

//返回角度大小-180-180度
- (int)angleFromStartToEndingPoints:(BNRLine*)line                         startPoint:(CGPoint)start                           endPoint:(CGPoint)end{  <span style="color:#ff0000;">  return (((atan2((line.end.x- line.begin.x),(line.end.y-line.begin.y)))*180)/M_PI);</span>}//通过象限给定颜色- (UIColor *)selectLineColorFromAngleValue:(int)angleValue{    UIColor *lc = nil;   <span style="color:#ff0000;"> if (angleValue >= 0 && angleValue <= 90) {        lc = [UIColor redColor];    }    else if (angleValue >= 91 && angleValue <= 180)    {        lc = [UIColor blueColor];    }    else if (angleValue < 0 && angleValue >= -90)    {        lc = [UIColor greenColor];    }    else if (angleValue <-90 && angleValue >= -179)    {        lc = [UIColor yellowColor];    }    else    {        lc = [UIColor blackColor];    }</span>        return lc;}//调用的时候用 self.lineColor = [self selectLineColorFromAngleValue:[self angleFromStartToEndingPoints:line startPoint:line.begin endPoint:line.end]];

你也可以直接通过 某条线的坐标比较来判断

  for (CPELine *line in self.finishedLines)    {      <span style="color:#ff0000;">  if (line.begin.x > line.end.x && line.begin.y > line.end.y) {            [[UIColor greenColor] set];        } else if (line.begin.x > line.end.x && line.begin.y < line.end.y){            [[UIColor redColor] set];        } else if (line.begin.x < line.end.x && line.begin.y > line.end.y){            [[UIColor blueColor] set];        } else [[UIColor orangeColor] set];</span>                [self strokeLine:line];    }


其他代码不太相关,你只需要看懂代码的红色部分,其中line需要解释一下,包括以下属性

@property (nonatomic) CGPoint begin;@property (nonatomic) CGPoint end;
它就是标识一条线。



0 0