2012-03-08 23 views
32

मैं एक रेखा के कोण की गणना करने के लिए एक सरल समाधान प्राप्त करना चाहता हूं (घड़ी के सूचक की तरह)।अंक से कोण की गणना कैसे करें?

cX, cY - the center of the line. 
eX, eY - the end of the line. 

The result is angle (0 <= a < 360). 

कौन सा समारोह इस मूल्य प्रदान करने में सक्षम है:

मैं 2 अंक है?

+0

आपको कम से कम तीन अंक की जरूरत है एक कोण की गणना करने के लिए। आप तीसरे कौन हैं? एक्स-अक्ष या वाई-अक्ष? – Sirko

+2

एक्स अक्ष –

+0

2 आयामी रेखा मानते हैं। – durumdara

उत्तर

72

आप arctangent हैं:

dy = ey - cy 
dx = ex - cx 
theta = arctan(dy/dx) 
theta *= 180/pi // rads to degs 

एर्म, ध्यान दें कि ऊपर स्पष्ट रूप से संकलन नहीं कर रहा है जावास्क्रिप्ट कोड। आपको arctangent फ़ंक्शन के लिए दस्तावेज़ों को देखना होगा।

संपादित करें:Math.atan2(y,x) का उपयोग करते हुए आप के लिए विशेष मामलों और अतिरिक्त तर्क के सभी संभाल लेंगे:

function angle(cx, cy, ex, ey) { 
    var dy = ey - cy; 
    var dx = ex - cx; 
    var theta = Math.atan2(dy, dx); // range (-PI, PI] 
    theta *= 180/Math.PI; // rads to degs, range (-180, 180] 
    //if (theta < 0) theta = 360 + theta; // range [0, 360) 
    return theta; 
} 
+5

मैन, मुझे त्रिकोणमित्री पसंद आया। – Purag

+5

ध्यान दें कि 0 से विभाजन से बचने के लिए, आपको परीक्षण करना चाहिए कि 'dx == 0' पहले; यदि ऐसा है, तो 'dy <0' अगर' dy> 0' और 270 डिग्री हो तो 90 डिग्री लौटाएं। –

+0

@EtiennePerot: मुझे एक और उपयोगी फ़ंक्शन याद आया और इसके बजाय मेरे उत्तर को अपडेट करने के लिए अपडेट किया गया। –

8

Christian's answer की Runnable संस्करण।

function angle(cx, cy, ex, ey) { 
 
    var dy = ey - cy; 
 
    var dx = ex - cx; 
 
    var theta = Math.atan2(dy, dx); // range (-PI, PI] 
 
    theta *= 180/Math.PI; // rads to degs, range (-180, 180] 
 
    return theta; 
 
} 
 
function angle360(cx, cy, ex, ey) { 
 
    var theta = angle(cx, cy, ex, ey); // range (-180, 180] 
 
    if (theta < 0) theta = 360 + theta; // range [0, 360) 
 
    return theta; 
 
} 
 

 
show("right", 0, 0, 1, 0); 
 
show("top right", 0, 0, 1, 1); 
 
show("top", 0, 0, 0, 1); 
 
show("top left", 0, 0, -1, 1); 
 
show("left", 0, 0, -1, 0); 
 
show("bottom left", 0, 0, -1, -1); 
 
show("bottom", 0, 0, 0, -1); 
 
show("bottom right", 0, 0, 1, -1); 
 

 
// IGNORE BELOW HERE (all presentational stuff)
table { 
 
    border-collapse: collapse; 
 
} 
 
table, th, td { 
 
    border: 1px solid black; 
 
    padding: 2px 4px; 
 
} 
 
tr > td:not(:first-child) { 
 
    text-align: center; 
 
} 
 
tfoot { 
 
    font-style: italic; 
 
}
<table> 
 
    <thead> 
 
    <tr><th>Direction*</th><th>Start</th><th>End</th><th>Angle</th><th>Angle 360</th></tr> 
 
    </thead> 
 
    <tfoot> 
 
    <tr><td colspan="5">* Cartesian coordinate system<br>positive x pointing right, and positive y pointing up.</td> 
 
    </tfoot> 
 
    <tbody id="angles"> 
 
    </tbody> 
 
</table> 
 
<script> 
 
function show(label, cx, cy, ex, ey) { 
 
    var row = "<tr>"; 
 
    row += "<td>" + label + "</td>"; 
 
    row += "<td>" + [cx, cy] + "</td>"; 
 
    row += "<td>" + [ex, ey] + "</td>"; 
 
    row += "<td>" + angle(cx, cy, ex, ey) + "</td>"; 
 
    row += "<td>" + angle360(cx, cy, ex, ey) + "</td>"; 
 
    row += "</tr>"; 
 
    document.getElementById("angles").innerHTML += row; 
 
} 
 
</script>

0

दो अंक या किसी भी कोण के बीच के कोण हो रही के साथ इस मुद्दे से एक संदर्भ आप का उपयोग है।

गणित में हम सर्कल के दाईं ओर उत्पत्ति के साथ एक त्रिभुज चक्र का उपयोग करते हैं (x = त्रिज्या, y = 0 में एक बिंदु) और 0 से 2 पीआई तक कोण काउंटर घड़ी की गणना करें।

भूगोल में मूल 0 डिग्री पर उत्तर है और हम 360 डिग्री से घड़ी की दिशा में जाते हैं।

नीचे कोड (सी # में) तो रेडियन में कोण हो जाता है एक भौगोलिक कोण में धर्मान्तरित:

public double GetAngle() 
    { 
     var a = Math.Atan2(YEnd - YStart, XEnd - XStart); 
     if (a < 0) a += 2*Math.PI; //angle is now in radians 

     a -= (Math.PI/2); //shift by 90deg 
     //restore value in range 0-2pi instead of -pi/2-3pi/2 
     if (a < 0) a += 2*Math.PI; 
     if (a < 0) a += 2*Math.PI; 
     a = Math.Abs((Math.PI*2) - a); //invert rotation 
     a = a*180/Math.PI; //convert to deg 

     return a; 
    } 
0

आप कैनवास उपयोग कर रहे हैं, तो आप देखेंगे (आप पहले से ही नहीं है) कि कैनवास घड़ी के घूर्णन का उपयोग करता है (MDN) और y अक्ष फ़्लिप हो जाती है। लगातार परिणाम प्राप्त करने के लिए, आपको अपने angle फ़ंक्शन को ट्विक करने की आवश्यकता है।

समय-समय पर, मुझे यह फ़ंक्शन लिखना होगा और हर बार मुझे इसे देखने की ज़रूरत है, क्योंकि मैं गणना के नीचे कभी नहीं पहुंचता।

सुझाए गए समाधान काम करते समय, वे कैनवास समन्वय प्रणाली को ध्यान में नहीं लेते हैं। की जांच निम्नलिखित डेमो:

Calculate angle from points - JSFiddle

function angle(originX, originY, targetX, targetY) { 
    var dx = originX - targetX; 
    var dy = originY - targetY; 

    // var theta = Math.atan2(dy, dx); // [0, Ⲡ] then [-Ⲡ, 0]; clockwise; 0° = west 
    // theta *= 180/Math.PI;   // [0, 180] then [-180, 0]; clockwise; 0° = west 
    // if (theta < 0) theta += 360;  // [0, 360]; clockwise; 0° = west 

    // var theta = Math.atan2(-dy, dx); // [0, Ⲡ] then [-Ⲡ, 0]; anticlockwise; 0° = west 
    // theta *= 180/Math.PI;   // [0, 180] then [-180, 0]; anticlockwise; 0° = west 
    // if (theta < 0) theta += 360;  // [0, 360]; anticlockwise; 0° = west 

    // var theta = Math.atan2(dy, -dx); // [0, Ⲡ] then [-Ⲡ, 0]; anticlockwise; 0° = east 
    // theta *= 180/Math.PI;   // [0, 180] then [-180, 0]; anticlockwise; 0° = east 
    // if (theta < 0) theta += 360;  // [0, 360]; anticlockwise; 0° = east 

    var theta = Math.atan2(-dy, -dx); // [0, Ⲡ] then [-Ⲡ, 0]; clockwise; 0° = east 
    theta *= 180/Math.PI;   // [0, 180] then [-180, 0]; clockwise; 0° = east 
    if (theta < 0) theta += 360;  // [0, 360]; clockwise; 0° = east 

    return theta; 
} 
संबंधित मुद्दे