在没有if条件的情况下找到图上点的象限 [英] find quadrant of a point on graph without if condition

查看:229
本文介绍了在没有if条件的情况下找到图上点的象限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在不使用任何条件的情况下(如果,……,……等)找到图上点的位置(象限或轴).用户将提供该点的坐标(x,y).
Language = C或CPP

How can i find the position (quadrant or axis) of a point on a graph without using any conditon (if, while, for...etc). The user will provide coordinates(x,y) of the point.
Language=C or CPP

推荐答案

// requires <math.h>

int quadrant(double x, double y)
{
  static int q[]={3, 4, 2, 1};
  int sx = ((int)(x/fabs(x)) + 1)/2;
  int sy = ((int)(y/fabs(y)) + 1); 
  return q[sx+sy];
}



您也可以使用atan2代替fabs:



you may use, as well, atan2 instead of fabs:

int quadrant(double x, double y)
{
  static int q[]={3, 4, 2, 1};
  int i = (int)((atan2(y,x)/atan2(1.,0.))+2.0); 
  return q[i];
}





请注意,该函数对于x=0.0或/和y=0.0输入值给出错误的结果(这些点位于象限的边界上).
:)





Please note, the function gives wrong result for x=0.0 or/and y=0.0 input values (such points are on the boundary of the quadrants).
:)


这篇关于在没有if条件的情况下找到图上点的象限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆