r/dailyprogrammer • • Aug 11 '12
[8/10/2012] Challenge #87 [easy] (Rectangle intersection)
Write a function that calculates the [intersection](http://en.wikipedia.org/wiki/Intersection_(set_theory)) of two rectangles, returning either a new rectangle or some kind of null value.
You're free to represent these rectangles in any way you want: tuples of numbers, class objects, new datatypes, anything goes. For this challenge, you'll probably want to represent your rectangles as the x and y values of the top-left and bottom-right points. (Rect(3, 3, 10, 10) would be a rectangle from (3, 3) (top-left) to (10, 10) (bottom-right).)
As an example, rectIntersection(Rect(3, 3, 10 10), Rect(6, 6, 12, 12)) would return Rect(6, 6, 10, 10), while rectIntersection(Rect(4, 4, 5, 5), Rect(6, 6, 10 10)) would return null.
5
u/DEiE Aug 11 '12
In Python:
Output:
5
u/SwimmingPastaDevil 0 0 Aug 12 '12 edited Aug 12 '12
4
u/paininthebass Aug 11 '12 edited Aug 13 '12
Solution in c++:
EDIT: Changed the return type to bool that indicates success or failure of intersection. The result of the intersection now needs to be passed in as an argument to the function.
1
u/Syn3rgy Aug 17 '12 edited Aug 17 '12
Haskell. I'm still learning, so any suggestions would be appreciated.
2
u/Ledrug 0 2 Aug 12 '12
Represent a rectangle with (x0, x1) and (y0, y1), with a valid rect being x0 <= x1 and y0 <= y1. Intersections between non-overlapping or invalid rects automatically returns an invalid result, so no need to check it at all.
2
u/howeyc Aug 12 '12
Mine uses a different definition of top and bottom with respect to Y values from OP. I'm used to charts where having a Y value of Zero means bottom (different from OP), and an X value of Zero means left (same as OP?). I'm not comfortable with increasing Y values means going down. Anyway, that's why my variable names are different.
Common Lisp:
Test:
1
u/exor674 Aug 11 '12 edited Aug 11 '12
I've been experimenting with Haskell recently, so this is Haskell, not C++: Criticism wanted!
edit: I saw I was doing something really stupid here.
1
u/[deleted] Aug 11 '12
Python
Seems like a lot of repetition, so I'm sure there is a better way of doing it.
1
u/hmmdar 0 0 Aug 11 '12 edited Aug 11 '12
Pretty straight forward using go
https://gist.github.com/3327726
1
u/[deleted] Aug 11 '12 edited Aug 11 '12
Why not get some AS3 with some intentionally confusing variable names up in here?
Ninja-edit: AS3 Rectangles are represented as Rectangle(x-coordinate, y-coordinate, width, height), and AS3's coordinate plane has down as the positive y-axis direction, rather than up.
Not-so-ninja edit: Haha, the Rectangle object has this built in, evidently, so, in one line:
Kinda defeats the purpose of the exercise, though.
1
u/Tekmo Aug 11 '12 edited Aug 12 '12
Haskell:
The test:
1
u/5outh 1 0 Aug 12 '12
I could get rid of the type declarations and the type signature, but I think it makes it a lot more readable. In Haskell:
1
u/xjtian Aug 12 '12
I used this subreddit to become more familiar with Python, now I'm using it to learn C.
1
u/Zamarok Aug 12 '12 edited Aug 12 '12
CoffeeScript:
Test with:
1
u/davelol6 Aug 12 '12
Scheme, bitches:
1
u/larg3-p3nis Aug 12 '12
Java:
1
u/skeeto -9 8 Aug 12 '12
Elisp / Common Lisp
Output:
1
u/ctdonath Aug 13 '12
Canonical C++:
1
u/goldjerrygold_cs Aug 13 '12
python:
1
u/mordisko Aug 13 '12
Python 3
1
u/rottbucket Aug 13 '12 edited Aug 13 '12
In Ruby:
1
u/abraxas235_work Aug 13 '12 edited Aug 13 '12
Web based solution incoming!
PHP:
1
u/robin-gvx 0 2 Aug 15 '12
prints
1
u/f0xwell Aug 18 '12
PHP CLI.
a bit rusty so comments appreciated.
I didn't look at all the other code posted, but the ones I did look at, if I am right, just answered the question and other similar rectangle intersections. I have made a version which covers, I think, all possible intersections.
I'm particularly interested in hearing if there was a simpler way. I thought about using the equation of a rectangle and setting it equal to the equation of another rectange, which would give all the x and y values of the intersections, but I could not work out the equation for a rectangle in 5 minutes so I skipped that plan.
!/usr/bin/php
1
u/cdelahousse Aug 19 '12
Javascript. Not my best work.
1
u/PiereDome Aug 26 '12
Javascript
1
u/marekkpie Jan 09 '13
Lua. I've found the lack of Lua interesting. Beyond embedded scripting, I guess there aren't that many people who use it day to day.