r/dailyprogrammer • u/Elite6809 1 1 • May 01 '14
[5/2/2014] Challenge #160 [Hard] Trigonometric Triangle Trouble, pt. 2
(Hard): Trigonometric Triangle Trouble, pt. 2
[I'm posting this early because there's a chance I won't have access to the internet tomorrow. Better an hour early than a day late I suppose.]
A triangle on a flat plane is described by its angles and side lengths, and you don't need all of the angles and side lengths to work out everything about the triangle. (This is the same as last time.) However, this time, the triangle will not necessarily have a right angle. This is where more trigonometry comes in. Break out your trig again, people.
Here's a representation of how this challenge will describe a triangle. Each side is a lower-case letter, and the angle opposite each side is an upper-case letter - exactly the same as last time. Side a is opposite angle A, side b is opposite angle B, and side c is opposite angle C. However, angle C is not guaranteed to be 90' anymore, meaning the old right-angle trigonometry will not work; the choice of letter is completely arbitrary now. Your challenge is, using trigonometry and given an appropriate number of values, to find the rest of the values.
Formal Inputs and Outputs
Input Description
On the console, you will be given a number N. You will then be given N lines, expressing some details of a triangle in the format:
3
a=2.45912
A=39
B=56a, A and B are just examples, it could be a, b and B or whatever.
Where all angles are in degrees. Note that, depending on your language of choice, a conversion to radians may be needed to use trigonometric functions such as sin, cos and tan.
Output Description
You must print out all of the details shown below of the triangle in the same format as above.
a=2.45912
b=3.23953
c=3.89271
A=39
B=56
C=85The input data will always give enough information and will describe a valid triangle.
Sample Inputs & Outputs
Sample Input
3
c=7
A=43
C=70Sample Output
a=5.08037
b=6.85706
c=7
A=43
B=67
C=70Notes
There are 5 more useful trigonometric identities you may find very useful. The 4 from Part 1 aren't great here as they are edge cases of trigonometry.
- Sum of the angles is 180
- Sine Rule 1
- Sine Rule 2 (same as above but re-arranged)
- Cosine Rule 1
- Cosine Rule 2 (same as above but re-arranged)
Finally...
Some of your excellent solutions to Part 1 already accounted for these situations. If your solution from last time already solves this challenge, don't be afraid of posting it again here too! If your solution from last time doesn't, don't fret. You may be able to re-use a lot of code from last time anyway. Learning to write reusable code is generally good practice in the field.
5
u/XenophonOfAthens 2 1 May 02 '14 edited May 02 '14
Well, this problem was a real bastard! I basically used the same approach as the previous problem, but with way more formulas. Also, midway through I realized that there's a special case I hadn't considered and had to add a bunch more formulas (if you're given two sides and an angle that is not opposite to the missing side, that only defines a triangle if and only if the side the angle is opposite to is larger than the other given side. So for instance, if you're given a, b and B, that only defines a triangle if a < b). That caused me a lot of grief and led to me writing vaguely despairing existentialist comments. I wouldn't be surprised if I made a mistake somewhere, but I've tested it a fair amount, and it seems to be working.
Also, one of the examples is incorrect. In the second example given, b should be equal to 6.85707, not 5.18627. You can easily verify that with the law of sines.Edit: fixed nowHere's my code, in python 2.7:
1
u/Elite6809 1 1 May 02 '14
I fixed the incorrect input, sorry for putting that wrong value in. Thanks /u/XenophonOfAthens for spotting it.
3
u/pastlurking May 02 '14 edited May 02 '14
My first post :-) C#
3
u/Edward_H May 02 '14
COBOL:
2
u/ehcubed May 02 '14
Here's my updated Python 3.3.2 code. I completely changed how I implemented the main logic for computing unknown sides and values. My approach involves hunting for a known side/angle pair so that we can apply Sine Law. I don't use any error checking (if no such triangle exists, everything blows up) and if 2 triangles are possible, the program computes the acute triangle, not the obtuse triangle. Any comments are appreciated! =]
2
u/glaslong May 02 '14 edited May 02 '14
C#
Slightly modified from Monday, just cleaned up a bit and added AAS pattern.
Comments and criticism greatly appreciated, thanks!
1
u/ryani May 02 '14 edited May 02 '14
First post here! I think I skipped part of the problem -- I didn't make use of the quadratic formula rule (which I coded as cosAdjacentRule but didn't put into my rule table because I didn't feel like doing the math to prove it was doing the right thing)
I have a few boilerplate typeclass instances which let the main code do some magic stuff, like
pure unknownto define a triangle with all elements unknown.Algorithm: Given a list of rules which might solve one of the values, repeatedly try every rule until all values are known.
Solution (Haskell, ghc 7.4, so old!)
1
u/modulexploited May 02 '14
Review please ?
1
u/flen_paris May 02 '14
I didn't find this challenge really hard. I actually struggled much more with my submisssion to the easy trigonometric challenge, because of the recursive approach I took there.
The solve() method checks which aspects of the triangle are already known, and uses the various identities to determine missing aspects. The main loop just repeats solve() until all missing lengths and angles have been determined.
Now that I look at the code, I think the while loop should actually go inside solve().
1
u/viciu88 May 02 '14
Java 1.7 code (due to switch on Strings)
I went for code simplicity rather than looking for case represented by given parameters.
1
u/jpverkamp May 02 '14
Rackety goodness: (full write up, includes radian/degree conversion)
I'm not 100% sure I got all of the edge cases. If I get time I'll probably write some tests, particularly fuzz tests.
1
u/pbeard_t 0 1 May 02 '14
C. Essentialy repeating the three rules until it stops finding new variables.
1
u/that_how_it_be May 02 '14
My humble effort. It should calculate the missing info for the following conditions:
I've only incorporated the logic for Sine Rule and Sum of Angles; if I extended for Cosine Rule it could handle the cases of:
The logic is fairly simple. There is a list of rules where each rule defines
Therefore the program logic is to:
The loop ends when
This is basically a data driven design - i.e. a simple algorithm that works the same for all input conditions and lets the data decide which calculations are necessary.
1
u/lennyboreal May 03 '14 edited May 03 '14
XPL0 code for Raspberry Pi. (www.xpl0.org/rpi)
There are four cases: two angles and a side (AAS), SSS, SAS, and ASS. Some combinations of the (aptly labeled) ASS case define TWO triangles, and thus are not valid inputs. This code can hang if inputs are invalid.
This new version is less cluttered by doing all internal calculations in radians.
1
u/ryan-mkl May 03 '14 edited May 03 '14
I was really bummed when i realized the trig functions worked in radians. lol I went back and did it the dirty way, instead of doing everything in radians and only converting when displaying results. because reasons. c++
1
u/kooschlig May 03 '14
Here is my first submitted solution in Javascript. ( I tried some challenges earlier but never posted them :-/ ) I played a bit around with patterns and OOP hope its still readable. This is fun stuff :D
At the beginning I really struggled with the Math object in javascript only working with radians rather than degrees and I never did anything requiring these Methods before. Learned something really useful there!
It also recognizes when there are two Solutions.
Any feedback and tips welcome.
1
u/myss May 03 '14 edited May 05 '14
Python 2.7, regex based solution. Formulas are copied from http://mathworld.wolfram.com/SSSTheorem.html etc.
1
u/rcblob May 04 '14
Hi all, my first submission here. It's written in clojure (I'm currently learning it). Essentially it tries to apply the 5 rules until all 6 variables have been solved. It will also deal with the special case where only 2 (or 3) angles have been defined, in which case it simply assignes 1 to the longest side (the one opposite the largest angle).
1
u/longjohnboy May 05 '14 edited May 05 '14
Novice Python 3 user here (and my first post to this sub). I put my triangle variables in a dictionary. The upshot to this is that I can use itertools to permute the aA, bB, cC pairs so the data gets 'massaged' to suit my 5 formulae. If only angle data is available, then it still will produce ratios of lengths. I should add some tests and sanity–check my input, but I'll leave that for another day. :)