finding angles 0-360

前端 未结 10 1771
清酒与你
清酒与你 2020-12-25 15:16

Need help with a math issue: i need to get the true angle from 0 degrees using x and y cordinates im using this at the moment:

Math.atan((x2-x1)/(y1-y2))/(Ma         


        
相关标签:
10条回答
  • 2020-12-25 16:08

    For 0=up,90=right,180=down,270=left etc (x=x2-x1,y=y2-y1)

    you can use the formula:

    f(x,y)=180-90*(1+sign(y))* (1-sign(x^2))-45*(2+sign(y))*sign(x)
    
    -(180/pi())*sign(x*y)*atan((abs(y)-abs(x))/(abs(y)+abs(x)))
    
    0 讨论(0)
  • 2020-12-25 16:09

    Also note:

    if (y1==y2) {
        if (x1>x2)
            angle = 90;
        else if (x1<x2)
            angle = 270;
        else
            angle = 0;
    }
    
    0 讨论(0)
  • 2020-12-25 16:11

    Here's two solutions, one with Math.atan (which takes a FRACTION opposite/adjacent) and one with Math.atan2 (which takes TWO ARGUMENTS)

    solutions are written in ES6 (ES2015+) syntax, ironically, because the question predates this javascript.

    note that 'to the right' is 0° (=0 Radians); up is 90° (= PI/2); left is 180° (PI), and down is 270° (PI*1.5)

    angleGivenCoords(coord1,coord2) {
        // given two coords {x,y}, calculate the angle in radians with
        // right being 0° (0 radians)
    
        if (coord1.x === coord2.x) return (coord1.y > coord2.y ? Math.PI * 0.5 : Math.PI * 1.5)
        if (coord1.y === coord2.y) return (coord1.x > coord2.x ? Math.PI : 0 )
    
        let opposite = coord2.x - coord1.x
        let adjacent = coord1.y - coord2.y
        let adjustor = ((coord2.x < coord1.x && coord2.y < coord1.y) || (coord2.x < coord1.x && coord2.y > coord1.y)) ?  Math.PI : 0
    
        let res = Math.atan(opposite/adjacent) + adjustor
    
        if (res < 0) { res = res + Math.PI*2  }
        return res ;
      }
    

    now with Math.atan2. notice that with this solution the guard clauses at the top (coord1.x === coord2.x, (coord1.y === coord2.y)) are unneeded

      angleGivenCoords(coord1,coord2) {
        // given two coords {x,y}, calculate the angle in radians with
        // left being 0° (0 radians)
        let opposite = coord2.x - coord1.x
        let adjacent = coord1.y - coord2.y
        let res = Math.atan2(adjacent, opposite)
        if (res < 0) { res = res + Math.PI*2  }
        return res ;
      }
    

    (I tried to keep the 'opposite' and 'adjacent' variable names in deference to the Trigonometry)

    please note that here is my test suite written in Jest. Also note my function above returns radians and my code (not shown here) has a simple Trig.degreesToRadians() as you would expect

     it('for right 0°', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 600, y: 500}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(0))
      })
    
    
      it('for up-right 45°', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 600, y: 400}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(45))
      })
    
      it('for 90° up', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 500, y: 400}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(90))
      })
    
    
      it('for 135° up to left', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 400, y: 400}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(135))
      })
    
      it('for 180° to left', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 400, y: 500}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(180))
      })
    
    
      it('for 225° to to bottom left', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 400, y: 600}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(225))
      })
    
      it('for 270° to the bottom', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 500, y: 600}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(270))
      })
    
      it('for 315° to the bottom', () => {
        let coord1 = {x: 500, y: 500},
          coord2 = {x: 600, y: 600}
        expect(Trig.angleGivenCoords(coord1,coord2)).toEqual(Trig.degreesToRadians(315))
      })
    
    0 讨论(0)
  • 2020-12-25 16:12

    This should do the trick:

    1. If y2
    2. If < 0, add 360.

    Examples:

    (x1,y1) = 0

    (x2,y2) = (-1,1), atan() = -45, [add 360], 270
    (x2,y2) = (1,1), atan() = 45
    (x2,y2) = (1,-1), atan() = -45, [add 180], 135
    (x2 ,y2) = (-1,-1), atan() = 45, [add 180], 225
    
    0 讨论(0)
提交回复
热议问题