Output an Animated Trigonometry Circle

5

1

I want you to write a program that outputs a trigonometry circle like the one below. I also want you to animate this circle, having the sine line move along the circumference with the other lines move around it. The minimum number of unique frames is 5. Any lower and your entry will not be counted.

Trig circle

The lines you will need are:

  • Sine (Both)
  • Cosine (Both)
  • Secant
  • Cosecant
  • Tangent
  • Cotangent
  • OA (on diagram)

Edit: No longer a limit

This is a code golf challenge, but I want you to get your code to below my code which is written in BBC BASIC.

Note: I will remove this requirement if the challenge is too hard.

My code: BBC BASIC - 185 bytes, 232 characters (Thanks Steve)

a=0:REPEAT:CLS:a+=0.005:CIRCLE600,500,300:s=300*SIN(a)+500:c=300*COS(a)+600:sc=300*1/COS(a)+600:cs=300*1/SIN(a)+500:MOVE600,500:DRAWc,s:DRAWc,500:DRAW600,500:DRAWsc,500:DRAWc,s:MOVE600,500:DRAW600,cs:DRAWc,s:DRAW600,s:WAIT 1:UNTIL0

Trig Circle

Beta Decay

Posted 2014-08-19T15:55:22.467

Reputation: 21 478

Dead link, perhaps? – sergiol – 2017-11-11T12:06:41.930

@sergiol I've updated it now :) – Beta Decay – 2017-11-11T15:04:32.483

1An image or gif of what your code produces would be very helpful. – Calvin's Hobbies – 2014-08-19T15:56:36.700

@Calvin'sHobbies Added – Beta Decay – 2014-08-19T16:00:09.897

1I count ASCII 232 characters there, though with tokenisation it probably is 185 bytes. Watch out for that byte counter on BBC Basic for Windows. I usually post as an ASCII count. Your code is probabably golfable down below 185 ASCII characters with VDU's though. – Level River St – 2014-08-19T16:19:44.457

1And when the tangent or cotangent are infinite, what should be drawn? Also, what does "output" mean? Can a program write a gif or video file, or should it output in real time on a canvas? – Peter Taylor – 2014-08-19T16:21:40.720

@PeterTaylor When tan and cot are infinite, you could draw it to a very long length. Regarding the format of the output, it can be anything. – Beta Decay – 2014-08-19T16:26:51.263

Answers

15

Mathematica, 142 bytes

Graphics[{Circle[],Line/@{{{c=Cos@t,0},{c,s=Sin@t},{0,s}},{{0,0},{1/c,0},{0,1/s},{0,0},{c,s}}},},PlotRange->{r={-2,2},r}]~Animate~{t,0.01,2Pi}

I'm plotting for angles between 0.01 and which conveniently skips any angles where one of the lines would become actually infinite. You can make a GIF from it by replacing Animate with Table, adding a step width of 0.1 say and then using Export on the result:

enter image description here

So I got a bit bored and thought I'd give the full-blown thing a go (with all lines and colours). I omitted the labels, because they are really annoying to position and just clutter everything up.

Animate[
  c = Cos@t;
  s = Sin@t;
  h = Sign@c;
  v = Sign@s;
  i = .1 h;
  j = .1 v;
  Graphics[{
    Thick,
    Circle[],
    Line /@ {{{c, -s}, {0, 0}, {c, s}}, {{-i, 0}, {-i, j}, {0, 
        j}}, {{-i + c, 0}, {-i + c, j}, {c, j}}, {{c - .1 s, 
        s + .1 c}, {1.1 c - .1 s, 1.1 s + .1 c}, 1.1 {c, s}}},
    Circle[{0, 0}, 0.1, {t, Round[t, Pi]}],
    Blue,
    Line /@ {{{0, 0}, {c, 0}}, {{0, s}, {c, s}}},
    Red,
    Line /@ {{{0, 0}, {0, s}}, {{c, 0}, {c, s}}},
    Darker@Green,
    Line[{{c, 0}, {h, 0}}],
    Cyan,
    Line[{{0, s}, {0, v}}],
    Lighter@Lighter@Magenta,
    Line[{{h, 0}, {1/c, 0}}],
    Darker@Darker@Green,
    Line[{{0, v}, {0, 1/s}}],
    Orange,
    Line[{{0, 1/s}, {c, s}}],
    Lighter@Brown,
    Line[{{1/c, 0}, {c, s}}],
    Gray,
    Line[{{c, 0}, {c, -s}}],
    Lighter@Gray,
    Line[{{c, s}, {h, 0}}],
    Lighter@Pink,
    Line /@ {{{-.45 h, 0}, {-.55 h, 0}}, {{-.45 h, 1/s}, {-.55 h, 
        1/s}}},
    Arrowheads[{-.05, .05}],
    Arrow[{{-.5 h, 0}, {-.5 h, 1/s}}],
    Darker@Cyan,
    Line /@ {{{0, -.45 v}, {0, -.55 v}}, {{1/c, -.45 v}, {1/
         c, -.55 v}}},
    Arrow[{{0, -.5 v}, {1/c, -.5 v}}],
    Dashed,
    Black,
    Line[{{c, s}, 1.3 {c, s}}],
    Lighter@Pink,
    Line /@ {{{0, 0}, {-.5 h, 0}}, {{0, 1/s}, {-.5 h, 1/s}}},
    Darker@Cyan,
    Line /@ {{{0, 0}, {0, -.5 v}}, {{1/c, 0}, {1/c, -.5 v}}}
    }, PlotRange -> {r = {-2, 2}, r}],
  {t, 0.01, 2 Pi}
  ];

Enjoy!

enter image description here

And because this is still a code-golf question I golfed this down as well, to see how short I could get it. Currently, 719 bytes:

Graphics[{Thick,Arrowheads@{-.05,.05},(z=Circle)[],(n=Line)/@{{p={c=Cos@t,s=Sin@t},o={0,0},{c,-s}},{-{i=.1(h=Sign@c),0},{-i,j=.1(v=Sign@s)},{0,j}},{{-i+c,0},{-i+c,j},{c,j}},{p+(q={-.1s,.1c}),1.1p+q,1.1p}},z[o,0.1,{t,Round[t,Pi]}],{e=Dashed,n[{p,1.3p}]},Blue,n/@{{o,a={c,0}},{b={0,s},p}},Red,n/@{{o,b},{a,p}},g=(d=Darker)@Green,n@{a,{h,0}},y=Cyan,n@{b,{0,v}},(l=Lighter)@l@Magenta,n@{{h,0},f={1/c,0}},d@g,n@{{0,v},u={0,1/s}},Orange,n@{u,p},l@Brown,n@{f,p},r=Gray,n@{a,{c,-s}},l@r,n@{p,{h,0}},l@Pink,n/@{q={{-.45h,0},{-.55h,0}},{u,u}+q},Arrow@{q={-.5h,0},u+q},{e,n/@{{o,q},{u,u+q}}},d@y,n/@{q={{0,-.45v},{0,-.55v}},{f,f}+q},Arrow@{q={0,-.5v},f+q},{e,n/@{{o,q},{f,f+q}}}},PlotRange->{r={-2,2},r}]~Animate~{t,0.01,2Pi,0.05}

Martin Ender

Posted 2014-08-19T15:55:22.467

Reputation: 184 808

1Nice, the colours are great! – Beta Decay – 2014-08-19T18:33:50.640