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,3
2:
-3.5,4 1,1
1,3.5 -2.5,-1
3:
-4,4 -0.5,2
0.5,1 3.5,3
Expected Ouputs
1:
1.0
2:
8.75
3:
0.0
Bonus
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.8
Bonus Expected Output
2.4
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
View all comments
Show parent comment
1
u/skeeto -9 8 Dec 30 '16
Alright, I'm impressed with the way Rust auto-vectorizes in the loop. :-) Clang does it too (suggesting LLVM is responsible), but I can't get GCC to do it. Actually benchmarking this particular example shouldn't be meaningful, though, since I expect the time to be dominated by
scanf/scan_fmt. I'm just comparing codegen.An example of the codegen I've been looking at is this (neither of these written by me, but by a friend learning Rust):
The Rust output does a provably unnecessary bounds check, and it sets up a base pointer for no reason. I suspect it's aligning the stack for a potential panic, even though that should be considered an unlikely (and slow) path. The output of GCC is basically what I'd code myself if I didn't have a compiler.