求直线与X轴的角度

wqfwuliao 2014-05-19 10:19:38

已知A、B两个点的坐标,求直线AB与X轴的角度
用JS,或者jquery都可以,哪位大神帮忙下
...全文
921 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
曼珠沙华糯米 2014-05-19
  • 打赏
  • 举报
回复
顶楼上的
xuefeng0707 2014-05-19
  • 打赏
  • 举报
回复
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>TEST</title>
	</head>
	<body>
		<label>hello  world!</label>
		
		<script type="text/javascript">
		// 这个可以用$.isNumeric代替
		function isNumeric(v) {
			return typeof v === 'number';
		}
		function angle(x1, y1, x2, y2) {
			if(!isNumeric(x1) || !isNumeric(y1) || !isNumeric(x2) || !isNumeric(y2)) {
				throw '必须输入数字';
			}
			var height = y1 - y2,
				width = x1 - x2;
			if(width == 0) {
				// 如果和y轴平行,角度为90或270
				return y1 >= y2 ? 90 : 270;
			} else {
				var tan = Math.atan(height / width),
					angle = tan * 180 / Math.PI;
				
				return tan > 0 ? (x1 > x2 ? angle : angle + 180) : (x1 > x2 ? angle + 360 : angle + 180);
			}
		}
		
		// 测试
		function assert(condition, msg) {
			if(!condition) {
				throw msg || 'error in assert';
			}
		}
		
		function angleTest() {
			assert(90 == angle(2,2,2,-1));
			assert(270 == angle(2,-1,2,2));
			assert(45 == angle(2,2,-1,-1));
			assert(225 == angle(-1,-1,2,2));
			assert(135 == angle(-2,2,1,-1));
			assert(315 == angle(2,-2,-1,1));
		}
		angleTest();
		</script>
	</body>
</html>
shine333 2014-05-19
  • 打赏
  • 举报
回复
我只想问,LZ的中学是怎么上的?

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧