Find the reference angle

17

This is a fairly simple question. According to this random website I found — Web Archive, a reference angle is the acute angle formed by the terminal side of the given angle and the x-axis. You have to write a program to find these.

I don't remember anything from algebra 2, what does this mean?

Angles are usually given in standard form, which is measured by placing one side of the angle and measuring to the other side, counterclockwise, like so:

Standard form

This will be your input. Your output will be the reference angle of this. You can think of this as basically the smallest distance from the terminal side to the x-axis. Except it's an angle, not a distance. Here are some examples:

Quad 2 ref Quad 1 ref

Quad 3 ref Quad 4 ref

Clarifications

  • All the defaults on inputs and submissions.
  • Angles is in degrees.
  • Negative angles, non-integer angles, and angles > 360 deg are allowed.
  • Output in form 60 deg.
  • This is , so shortest code in bytes wins!

Another helpful link.

Test Cases

70      ->  70 deg
135     ->  45 deg
210     ->  30 deg
-60     ->  60 deg
91      ->  89 deg
610     ->  70 deg
-1000   ->  80 deg

Maltysen

Posted 2015-07-25T05:41:17.360

Reputation: 25 023

1What do we do on input of 90? – lirtosiast – 2015-07-25T05:51:37.747

2@ThomasKwa it would be 90 from either direction. – Maltysen – 2015-07-25T05:53:21.217

1A short solution is abs(arcsin(sin(input)))+" deg", though I don't know which language would win (it would need to support degrees and string operations). – lirtosiast – 2015-07-25T06:02:40.943

Is the input always an integer? – lirtosiast – 2015-07-25T06:08:26.847

@ThomasKwa good point, no. – Maltysen – 2015-07-25T06:12:07.527

6Since non-integer angles are allowed, you should include one in the test cases. – Dennis – 2015-07-25T13:32:24.070

Answers

7

Pyth, 17 bytes

+hS%R180,Q_Q" deg

Coincidentally, this is equivalent to @xnor's answer.

Try it online.

isaacg

Posted 2015-07-25T05:41:17.360

Reputation: 39 268

6

Pyth, 18 bytes

+.a-%+QK90yKK" deg

Try it online.

grovesNL

Posted 2015-07-25T05:41:17.360

Reputation: 6 736

got it down to 19 using your algo but with inline assign, feel free to use: https://pyth.herokuapp.com/?code=%2B.a-%25%2BvwK90yKK%22+deg&debug=0

– Maltysen – 2015-07-25T06:44:12.433

@Maltysen: Thanks, found another improvement as well – grovesNL – 2015-07-25T06:47:46.607

5

Python 2, 34 bytes

lambda x:`90-abs(x%180-90)`+' deg'

Using "%d deg" string formatting would be longer because of the needed parentheses.

Sp3000

Posted 2015-07-25T05:41:17.360

Reputation: 58 729

5

CJam (21 bytes)

180qd1$%z_@@-e<" deg"

Online demo

Peter Taylor

Posted 2015-07-25T05:41:17.360

Reputation: 41 901

4

dc, 25

90?90+d*v180%-d*vn[ deg]p

Note dc uses _ as a -ve sign instead of -.

Test output:

$ for test in 70 135 210 _60 91 610 _1000; do dc -e'90?90+d*v180%-d*vn[ deg]p' <<< $test ; done
70 deg
45 deg
30 deg
60 deg
89 deg
70 deg
80 deg
$

Digital Trauma

Posted 2015-07-25T05:41:17.360

Reputation: 64 644

3

Python 2, 35

lambda x:`min(x%180,-x%180)`+' deg'

The smaller of the angle and its negative modulo 180. Effectively converts the angle to the range from 0 to 180 and takes the distance to the closer of the endpoints. Note that -x%180 groups as (-x)%180.

Same length with string formatting for Python 3:

lambda x:'%d deg'%min(x%180,-x%180)

xnor

Posted 2015-07-25T05:41:17.360

Reputation: 115 687

3

Mathematica, 22 31

ToString@Abs@Mod[#,180,-90]deg&

alephalpha

Posted 2015-07-25T05:41:17.360

Reputation: 23 988

1@LegionMammal978 If I just multiply it by deg, 0 deg will become 0. – alephalpha – 2015-07-26T04:18:09.193

1Since you don't care about the output type, you can save 2 bytes like this: Row@{Abs@Mod[#,180,-90],deg}& – LegionMammal978 – 2015-07-26T10:05:36.370

3

Pyth 25 30 bytes

Painfully uncreative solution, using trig functions. Permalink

+.R.t.a.t.t.tQ7Z3 6?%Q1Q0" deg

Any suggestions welcome.

Winny

Posted 2015-07-25T05:41:17.360

Reputation: 1 120

1Got it down to 28 by taking the .t calls and putting them in a fold: +.R.t.a.tF[Q7Z3)6?%Q1Q0" deg – Maltysen – 2015-07-25T08:13:11.437

Neat. I noticed it doesn't appear to work with a floating point input: 90.5 => 90.0 deg, though. – Winny – 2015-07-25T18:08:44.057

That's because yours doesn't either: https://pyth.herokuapp.com/?code=%2B.R.t.a.t.t.tQ7Z3+6%3F%25Q1Q0%22+deg&input=90.5&debug=0

– Maltysen – 2015-07-25T18:11:18.397

Interesting catch. With input -60.5 => 60.5 deg but with 60.5 => 60.0 deg. Looks like an issue with Pyth -- Example using pyth/macros.py

– Winny – 2015-07-25T18:23:50.377

1

JavaScript (ES6), 43 bytes

Similar to Sp3000's answer, though the modulo is quite lengthy due to the behaviour in JavaScript.

f=x=>90-Math.abs((x%180+180)%180-90)+' deg'

Demo

Code is rewritten in ES5 for browser compatibility:

function f(x){return 90-Math.abs((x%180+180)%180-90)+' deg'}

// Snippet stuff
console.log = function(x){document.body.innerHTML += x + '<br>'};
[70,135,210,-60,91,610,-1000].map(function(v){console.log(f(v))});

CoffeeScript, 45 bytes

f=(x)->90-Math.abs((x%180+180)%180-90)+' deg'

rink.attendant.6

Posted 2015-07-25T05:41:17.360

Reputation: 2 776

5I assume it was a design decision by a PDP hardware engineer which is responsible for the fact that several decades later we still have very few languages which implement % sensibly. – Peter Taylor – 2015-07-25T06:38:18.053

Replacing the first 180 with (t=180) then replacing the following 180s with t would probably save a few bytes – Downgoat – 2015-07-25T16:10:21.250

@vihan1086 It's the same – rink.attendant.6 – 2015-07-25T20:33:47.643

1

J, 26 bytes

m=:180&|
' deg',~m@-":@<.m

Same as xnor's method.

Legendre

Posted 2015-07-25T05:41:17.360

Reputation: 201

1

Matlab, 44

Using an anonymous function:

f=@(x)[num2str(min(mod([x -x],180))) ' deg']

Example:

>> f=@(x)[num2str(min(mod([x -x],180))) ' deg']
f = 
    @(x)[num2str(min(mod([x,-x],180))),' deg']

>> f(70)
ans =
70 deg

>> f(210)
ans =
30 deg

>> f(-1000)
ans =
80 deg

Luis Mendo

Posted 2015-07-25T05:41:17.360

Reputation: 87 464

4This doesn't append the ' deg' at the end though. (Did not downvote.) – Legendre – 2015-07-25T16:16:39.927

@Legendre Oops. Thanks! Corrected – Luis Mendo – 2015-07-26T02:24:50.350

1

Scala, 40 35 Characters

(a:Int)⇒s"${90-(a%180-90).abs} deg"

gilad hoch

Posted 2015-07-25T05:41:17.360

Reputation: 717

0

Tcl, 83 bytes

proc R a {puts [expr [set a $a%360]>90?$a>180?$a>270?360-$a:$a-180:180-$a:$a]\ deg}

Try it online!

sergiol

Posted 2015-07-25T05:41:17.360

Reputation: 3 055

-1

PHP, 44 bytes

Takes one command line parameter. Again PHP suffers from the same issue as JavaScript.

<?=90-abs(($argv[1]%180+180)%180-90)+' deg';

Using the canonical solution requires 53 bytes (the degrees and radians conversion takes a lot of characters):

<?=rad2deg(abs(asin(sin(deg2rad($argv[1])))))+' deg';

rink.attendant.6

Posted 2015-07-25T05:41:17.360

Reputation: 2 776

1Doesn't PHP use . for concatenation? – Downgoat – 2015-07-26T03:57:25.250

@vihan1086 Yes, PHP uses .. Downvoted until it is fixed – Ismael Miguel – 2015-07-26T11:33:38.293