Problem Statement
A triangle is "inscribed" in a circle if all 3 points of the triangle are on the edge of the circle. For this problem, our circle will be centered at the origin and have a radius of 5. Our goal is to find the largest triangle (by area) we can inscribe in this circle. Normally, this would be any equilateral triangle, but in this case we have the added restriction that each point of our triangle must be within one (or more) of the valid ranges of degrees. The degree ranges are given in thousandths of degrees in corresponding elements of angleFrom and angleTo. For each range, angleFrom will be less than or equal to angleTo and each will be between 0 and 360000. All ranges are inclusive; see the examples for clarification. Return the area of the largest inscribed triangle that can be made while following these restrictions. If no triangle can be made, return 0.
Definition
- Class:
- InscribedTriangles
- Method:
- findLargest
- Parameters:
- int[], int[]
- Returns:
- double
- Method signature:
- double findLargest(int[] angleFrom, int[] angleTo)
- (be sure your method is public)
Notes
- The ranges may overlap.
- Your return value must have an absolute or relative error less than 1e-9.
Constraints
- angleFrom and angleTo will each contain between 1 and 30 elements, inclusive.
- angleFrom and angleTo will contain the same number of elements.
- Each element of angleFrom and angleTo will be between 0 and 360000, inclusive.
- Each element of angleFrom will be less than or equal to the corresponding element of angleTo.
Examples
{0,89000,180000}
{0,91000,180000}
Returns: 25.0
double constrained test. Easy to hand check this one - (radius+radius)*radius/2 = 10*5/2 = 25
{0}
{240000}
Returns: 32.47595264191645
equilateral test
{0}
{360}
Returns: 3.87577502193079E-7
note thousandths of degrees
{29829}
{29829}
Returns: 0.0
zero test
{0,30000,60000}
{0,180000,60000}
Returns: 25.0
{0}
{238000}
Returns: 32.46609388044022
{0,1000,2000,3000,4000,5000,6000,7000,8000,9000, 10000,11000,12000,13000,14000,15000,16000,17000,18000,19000, 20000,21000,22000,23000,24000,24999,26000,27000,28000,29000}
{1000,2000,3000,4000,5000,6000,7000,8000,9000, 10000,11000,12000,13000,14000,15000,16000,17000,18000,19000, 20000,21000,22000,23000,24000,24999,26000,27000,28000,29000,29999}
Returns: 0.22045433337735376
Best triangle can use .0005 increments (whether or not that makes a significant difference in end area)...
{260001,89000}
{280000,91000}
Returns: 8.616036371577957
{15050,140055,259900}
{60045,140900,260000}
Returns: 32.47594515984052
Very, very close to an equilateral.
{0}
{360000}
Returns: 32.47595264191645
We can use any point we want on the circle - so we can draw an equilateral triangle (which will be the biggest we can ever draw).
{15000,25000,100000,265000,330000}
{15000,25000,100000,265000,330000}
Returns: 27.433829549752186
In this case, each of our ranges are single points. The biggest triangle can be made by using the points at 15°, 100°, and 265°.
{245900,246500,249900}
{245915,246611,252901}
Returns: 0.002789909594714814
We only have 3 small ranges, all near to each other - so our best triangle is still really small.
{42}
{42}
Returns: 0.0
It's hard to draw a triangle with one point.
{357050,180050,85000}
{359050,183050,95000}
Returns: 26.272344159710006
{29838,35000,353722}
{50002,67002,360000}
Returns: 2.9481034394618586
{0}
{180000}
Returns: 25.0
Here we have just the top half of the circle to work with. The best triangle is at 0°, 90°, 180°.
{0,270000}
{180000,270000}
Returns: 32.47595264191645
{89000,211154,319387,89000,211167,319378,89000,211002,319487,89000,211255,319487,89000,211491,319032,89000,211437,319437,89000,211315,319185,89000,211097,319436,89000,211237,319056,89000,211473,319406}
{90007,211955,320088,90002,211968,320079,90008,211803,320188,90006,212056,320188,90003,212292,319733,90001,212238,320138,90000,212116,319886,90003,211898,320137,90005,212038,319757,90001,212274,320107}
Returns: 32.146162619223226
{89117,209337,329205,89314,209183,329242,89185,209147,329276,89413,209204,329095,89288,209369,329423,89101,209212,329041,89227,209147,329390,89025,209481,329005,89269,209331,329275,89486,209147,329134}
{89633,209547,329592,89794,209250,329677,89191,209158,329591,89685,209636,329548,89439,209927,329611,89609,209717,329078,89808,209687,329827,89490,209484,329474,89712,209636,329503,89563,209501,329565}
Returns: 32.47595264191645
{89000,190180,290031,89000,190402,290273,89000,190546,290021,89000,190534,290443,89000,190169,290461,89000,190013,290393,89000,190004,290351,89000,190027,290588,89000,190040,290197,89000,190343,290153}
{90000,190218,290444,90000,190991,290423,90000,190986,290177,90000,190548,290851,90000,190251,290892,90000,190365,290753,90000,190603,290696,90000,190177,290700,90000,190561,290670,90000,190741,290340}
Returns: 29.205879798138202
{127810,122418,23013,267998,126963,287095,215811,339465,351723,318957,333732,236254,302352,335353,12935,233802,327782,281682,182002,169344,28119,108889,44436,329409,207139,305965,96162,207326,209792,302449}
{303690,284236,250009,288745,189203,346252,322811,357974,359054,327362,352355,291192,321761,344459,64616,237047,333294,302581,309236,268884,117195,312676,245636,329767,258518,326023,155880,249512,271361,317795}
Returns: 32.47595264191645
0-360 degrees
{42661,145219,87097,76231,55982,109200,24711,20792,41394,82149,164445,102096,176399,181589,83997,34423,109514,19182,173332,182443,162774,88230,62177,173717,129227,6179,7528,101197,105439,87112}
{138019,168222,176528,171293,121916,160479,113283,89752,123301,135625,183555,175807,180437,183986,123684,96872,140320,22910,174716,183934,177533,183010,137224,184258,134431,55763,43517,139773,155558,118707}
Returns: 24.577468560856083
0-185 degrees
{3630,4439,964,390,4014,3286,4147,1072,3480,3414,4642,4594,1789,2649,2872,4758,3150,3891,4961,1693,3730,2624,239,4355,4144,4405,3737,3032,1964,2373}
{4134,4679,3699,3848,4383,3718,4957,4921,4160,4916,4857,4755,2528,3399,4737,4811,4439,4031,4984,4566,4554,4004,3476,4660,4328,4909,4484,3739,3036,3794}
Returns: 8.871041458287127E-4
Small angles.
{199000,40277,40222,130,114,40236,40295,40294,40187,40245,40189,105,40198,40181,6,40154,77,40,40297,26,40289,83,122,40249,40175,91,40194,41,40207,109}
{201000,40361,40275,215,202,40312,40327,40385,40230,40264,40193,181,40298,40267,24,40154,153,72,40368,53,40348,128,177,40329,40232,187,40199,132,40256,197}
Returns: 16.72616437390915
This is built to catch ones that don't treat double-constrained case right.
{89999,280283,280235,260051,280179,260132,280216,280230,260115,260076,280195,280203,280195,260096,260008,260012,280186,280179,260136,260075,280270,280167,260050,260143,280295,280205,280167,260046,280230,280294}
{90501,280374,280313,260082,280234,260225,280268,280284,260147,260140,280201,280274,280261,260168,260060,260104,280227,280261,260195,260123,280295,280168,260057,260180,280313,280239,280196,260127,280234,280324}
Returns: 8.770015291003363
A similar double constraint
{167800,190000,356000}
{172000,190100,360000}
Returns: 9.577657806760245
{10000,340000,170000}
{11000,350000,190000}
Returns: 13.118935338332117
{70000,70000,70000,70000,70000,110000,110000,110000,110000,70000,110000}
{70000,70000,70000,70000,70000,110000,110000,110000,110000,70000,110000}
Returns: 0.0
{0, 360000 }
{0, 360000 }
Returns: 0.0
{0, 80000, 200000 }
{0, 130000, 250000 }
Returns: 32.47595264191645
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
{360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000, 360000 }
Returns: 32.47595264191645
{0 }
{89999 }
Returns: 5.177515264125743
{99967, 73399, 342872 }
{166166, 172032, 347768 }
Returns: 26.910070281680067
{0, 60000, 180000 }
{0, 120000, 180000 }
Returns: 25.0
{0 }
{360000 }
Returns: 32.47595264191645
{0, 10000 }
{180000, 190000 }
Returns: 27.075469673130268
{0, 120001, 230000 }
{0, 120001, 250000 }
Returns: 32.47595263944327
{200000, 300001, 68000 }
{200001, 300003, 73000 }
Returns: 31.461515018229893
{0, 100000 }
{0, 250000 }
Returns: 32.47595264191645
{0 }
{239000 }
Returns: 32.473483657273896
{45000, 91111, 265000 }
{48000, 93000, 287123 }
Returns: 19.06381920154999
{3000, 265000 }
{3000, 360000 }
Returns: 6.4893886462996715
{245900, 246500, 249900 }
{245915, 246611, 282901 }
Returns: 0.409961278457731
{0, 180000, 80000 }
{0, 180000, 100000 }
Returns: 25.0