简体   繁体   English

如何将x,y坐标转换为角度?

[英]How to convert x,y coordinates to an angle?

Microsoft provide an excellent SVG gradient maker so IE9 can also have "CSS3" gradients (click Custom). Microsoft提供了一个出色的SVG渐变制作器,因此IE9也可以具有“CSS3”渐变(单击自定义)。

I currently utilise their logic for my Fireworks and Dreamweaver extensions to convert gradients to SVG, but I only know how to do it for standard top, bottom, left, right directions. 我目前利用他们的逻辑为我的FireworksDreamweaver扩展将渐变转换为SVG,但我只知道如何为标准的顶部,底部,左,右方向做。 If you enter an angle, I don't do the conversion, because I'm not sure how I would convert x1, x2, y1, y2 to CSS3 angle degrees. 如果输入一个角度,我不进行转换,因为我不确定如何将x1,x2,y1,y2转换为CSS3角度。

The gradient generator provides values like this: x1="0%" y1="0%" x2="56.262833675564686%" y2="68.29999651227678%" 梯度生成器提供如下值:x1 =“0%”y1 =“0%”x2 =“56.262833675564686%”y2 =“68.29999651227678%”

I'm not great with mathematics or trigonometry, so could somebody help me out? 我对数学或三角学不太满意,所以有人可以帮助我吗? I'd also like to use the same math in a Sass mixin to do a similar thing, if possible. 如果可能的话,我也想在Sass mixin中使用相同的数学来做类似的事情。

If you get deltaX and deltaY from your coordinates then Math.atan2 returns the arctangent of the quotient of its arguments. 如果从坐标获得deltaXdeltaY ,则Math.atan2返回其参数的商的反正切值。 The return value is in radians. 返回值以弧度为单位。

var deltaX = x2 - x1;
var deltaY = y2 - y1;
var rad = Math.atan2(deltaY, deltaX); // In radians

Then you can convert it to degrees as easy as: 然后你可以将它转换为度数就像:

var deg = rad * (180 / Math.PI)

在此输入图像描述

Edit 编辑

There was some bugs in my initial answer. 我最初的答案中有一些错误。 I believe in the updated answer all bugs are addressed. 我相信更新的答案会解决所有错误。 Please comment here if you think there is a problem here. 如果您认为此处存在问题,请在此处发表评论。

The currently accepted answer is incorrect. 目前接受的答案不正确。 First of all, Math.tan is totally wrong -- I suspect Mohsen meant Math.atan and this is just a typo. 首先, Math.tan是完全错误的 - 我怀疑Mohsen的意思是Math.atan ,这只是一个错字。

However, as other responses to that answer state, you should really use Math.atan2(y,x) instead. 但是,正如对该答案的其他响应所述,您应该使用Math.atan2(y,x) The regular inverse tangent will only return values between -pi/2 and pi/2 (quadrants 1 and 4) because the input is ambiguous -- the inverse tangent has no way of knowing if the input value belongs in quadrant 1 vs 3, or 2 vs 4. 常规反正切将仅返回-pi / 2和pi / 2(象限1和4)之间的值,因为输入是不明确的 - 反正切无法知道输入值是否属于象限1对3,或者2比4。

Math.atan2 , on the other hand, can use the xy values given to figure out what quadrant you're in and return the appropriate angle for any coordinates in all 4 quadrants. 另一方面, Math.atan2可以使用给定的xy值来确定您所在的象限,并为所有4个象限中的任何坐标返回适当的角度。 Then, as others have noted, you can just multiply by (180/Math.pi) to convert radians to degrees, if you need to. 然后,正如其他人所说,如果需要,你可以乘以(180/Math.pi)将弧度转换为度数。

Instead of using Math.tan function You should use Math.atan2: 而不是使用Math.tan函数您应该使用Math.atan2:

Here is an example of use: 这是一个使用示例:

deltaX = x2 - x1;
deltaY = y2 - y1;
deg = Math.atan2(deltaY, deltaX)*180.0/Math.PI;

and this will return a degree from <-180;180>. 这将返回<-180; 180>的学位。

If you in a Quadrant 如果你在象限

P1=(X0,Y0) P1 =(X0,Y0)

P2=(X1,Y1) P2 =(X1,Y1)

a=(X0-X1) A =(X0-X1)

b=(Y0-Y2) B =(Y0-Y2)

deltaX=((a)**2)**0.5
deltaY=((b)**2)**0.5
rad=math.atan2(deltaY, deltaX)
deg = rad * (360 / math.pi)
print deg

the deg will between 0 ~ 180 deg将在0~180之间

This function takes 2 elements and returns the degree between the middle of the elements. 此函数采用2个元素并返回元素中间的度数。

For example, I used it on a world map, to make the image of plane rotate in the direction of a city. 例如,我在世界地图上使用它,使平面图像在城市方向上旋转。

function degFromTwoElements(el1,el2){
    var x1,x2,y1,y2,cx1,xy1,cx2,cy2,deltaX,deltaY,dx,dy,rad,deg,shortest,number;
    x1 = el1.position().left;
    y1 = el1.position().top;
    x2 = el2.position().left;
    y2 = el2.position().top;
    cx1 = x1 - (el1.width() / 2);
    cy1 = y1 - (el1.height() / 2);
    cx2 = x2 - (el2.width() / 2);
    cy2 = y2 - (el2.height() / 2);

    deltaX = cx2 - cx1;
    deltaY = cy2 - cy1;
    y1 = Math.sqrt((Math.abs(deltaY)*Math.abs(deltaY))+(Math.abs(deltaX)*(Math.abs(deltaX))));
    x1 = 0;
    dy = deltaY-y1;
    dx = deltaX-x1;
    rad = Math.atan2(dy, dx);
    deg = rad * (360 / Math.PI);

    shortest;
    number = Math.abs(deg);
    if ((360 - number ) < number){
        shortest = 360 - number;
        console.log('shorter degree: ' + shortest);
        return shortest;
    } 
    else console.log('Angle is: ' + deg);
    return deg;

}

 var x,x1,x2,y,y1,y2; var cells = 'cell0'; var h,w; var cx,cy; var dx,dy; var derajat; var deg; var ang; var light; var control; function mouse_watch(event){ x = event.clientX; y = event.clientY; cell_data(cells); koordinat(x2,y2); busur(derajat); } function koordinat(x2,y2){ x2 = x-cx; y2 = y-cy; yk = y2; xk = x2; } function busur(derajat){ y1 = Math.sqrt((Math.abs(yk)*Math.abs(yk))+(Math.abs(xk)*(Math.abs(xk)))); x1 = 0; dy = yk-y1; dx = xk-x1; rad = Math.atan2(dy, dx); derajat = rad * (360 / Math.PI); cell = document.getElementById(cells); ang = cell.getElementsByClassName('angle0')[0]; ang.style.transform = 'rotate('+derajat+'deg)'; light = ang.getElementsByClassName('points')[0]; light.style.height = y1+'px'; } function cell_data(cells){ cell = document.getElementById(cells); h = Number(cell.style.height.replace('px','')); w = Number(cell.style.width.replace('px','')); cy = Number(cell.style.top.replace('px',''))+h/2; cx = Number(cell.style.left.replace('px',''))+w/2; } 
  .preview_engine{ position: absolute; top: 0; left: 0; padding: 10px; background-color: #2E8AE6; color: white; } body{ cursor: default; width: 100%; height: 100%; font-family: Arial; font-size: 12px; } .fieldwork{ width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; } .cell{ position: relative; transition : width 2s, height 2s, top 2s, left 2s; background-color: red; } .angle0{ width: 200px; height: 200px; position: absolute; top: -75px; left: -75px; background-color: green; border-radius: 50%; opacity: 0.5; transition : width 2s, height 2s, top 2s, left 2s; } .points{ width: 10px; height: 10px; position: absolute; left: 95px; top: 95px; background-color: red; border-radius: 1em; opacity: none; } 
 <div class="fieldwork" onmousemove="mouse_watch(event)"> <div class='cell' id="cell0" style="width:50px;height:50px;top:200px;left:400px;"> <div class="angle0"> <div class="points"></div> </div> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将英尺坐标(x,y)转换为像素 - How to convert coordinates (x, y) in feet to pixels 如何翻译鼠标 X,Y 坐标以移动容器内的 HTML 元素,其中容器旋转到某个角度 - How to translate Mouse X,Y Coordinates to move HTML element inside a container where container is rotated to some angle 如何计算HTML Canvas弧的结束角的x和y坐标? - How do I compute for the x and y coordinates of the end angle of a HTML Canvas arc? 如何转换 x,y 坐标以使用绝对显示? - How to convert x,y coordinates to work with absolute display? 如何将(x,y)屏幕坐标转换为LatLng(谷歌地图) - How to convert from (x, y) screen coordinates to LatLng (Google Maps) 如何将人力车的整数数组转换为x / y坐标 - How to convert an array of integers into x/y coordinates for rickshaw 如何从一个角度找到(x,y)坐标 - How to find (x,y) coordinate from an angle 如何使用d3.js将纬度和经度转换为(x,y)坐标 - How to convert latitude and longitude into (x,y) coordinates using d3.js 从角度和距离获取X,Y坐标(使用Math.cos()和Math.sin()的值不正确) - Get X,Y coordinates from Angle & Distance ( Incorrect values using Math.cos() & Math.sin() ) D3 + svg变换基于旋转角度混淆计算x / y坐标 - confusion calculating x/y coordinates based on rotation angle with D3 + svg transformation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM