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
3
u/leonardo_m Dec 29 '16
This little program is too much simple to see much of Rust at work. This site ( http://rosettacode.org/wiki/Rosetta_Code ) is based on your idea and it helps to learn a language.
Often it's good enough, sometimes it's not, it's not uniformly mediocre. In many cases where it's not, you can find ways to improve the situation.
Still, type-level values as C++ template arguments sometimes offer optimization solutions that are nearly impossible in Rust. But they will probably come: https://github.com/rust-lang/rfcs/pull/1657
Clang V.3.9.0 (with -Ofast -march=haswell) compiles the loop of your C code in a clean way:
In the Rust code "for line in stdin.lock().lines()" heap-allocates one UTF8 string for each input line, and scan_fmt!() is not simple, so the Rustc asm output can't be clean... It's a kind of mess.
But the run-time is not bad. With an input file of about 32 MB (33.030.144 bytes), the C code compiled with GCC 6.1.0 (a different back-end) runs in about 1.91 seconds and the Rust code in 2.18 seconds. And there are some ways to make that Rust code faster.
You can write a very simple C compiler that performs nearly no optimizations (like: http://bellard.org/tcc/ ) and the binaries will still run fast enough for most purposes. In Rust a dumb compilation produces binaries that are tens of times slower than optimzied builds or more. Rust seems to require an advoptimizer, and (much) longer compile times compares to older simpler languages (C, Turbo Pascal, Oberon, etc). While Rust currently seems a bit too much tied to LLVM, I think someday we'll see a Rust compiler with a GCC back-end, and I think GCC optimizations will be enough.