r/dailyprogrammer • u/fvandepitte 0 0 • Dec 22 '16
[2016-12-22] Challenge #296 [Intermediate] Intersecting Area Of Overlapping Rectangles
Description
You need to find the area that two rectangles overlap. The section you need to output the area of would be the blue lined section here: http://i.imgur.com/brZjYe5.png
If the two rectangles do not overlap, the resultant area should be 0.
Input
There will be two lines of input. On each line are the x and y positions (separated by a comma) of each opposing corner (each corner co-ordinate separated by a space). The co-ordinates can have decimals, and can be negative.
Output
The area of the overlapping section of the two rectangles, including any decimal part.
Challenge Inputs
1:
0,0 2,2
1,1 3,32:
-3.5,4 1,1
1,3.5 -2.5,-13:
-4,4 -0.5,2
0.5,1 3.5,3Expected Ouputs
1:
1.02:
8.753:
0.0Bonus
Make this work with any number of rectangles, calculating the area of where all input rectangles overlap. The input will define a rectangle on each line the same way, but there can be any amount of lines of input now.
Bonus Input
-3,0 1.8,4
1,1 -2.5,3.6
-4.1,5.75 0.5,2
-1.0,4.6 -2.9,-0.8Bonus Expected Output
2.4Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
View all comments
Show parent comment
1
u/skeeto -9 8 Dec 29 '16
I tell you what, the most lavish way to learn a new language is to have a knowledgeable programmer faithfully translate your own code. It's a straight mapping of concepts.
There's an interesting difference between
DBL_MINin C andf64::MINin Rust. In the former, that constant is0.0, which I find to be counterintuitive. I initially usedDBL_MINwhile writing my code and wasn't until I was debugging the incorrect answers that I realized my mistake. Hence the-DBL_MAX, which may technically be incorrect for non-IEEE floats.I'm very interested in Rust's code generation. Unfortunately the Rust Compiler Explorer doesn't provide
scan_fmt, so I can't build it there. But, so far, what I've seen of Rust's generated code quality is that it's mediocre. It's still a ways behind C and C++ in terms of performance even where it really should be on the same footing (i.e. in "unsafe" code, or when bounds checking can be proven unnecessary). That's not a failing, it just needs more time to mature.I personally think they're leaning too much on LLVM optimizations, but their inputs to LLVM are over-constrained. They aim to fix this with MIR.