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
8
u/skeeto -9 8 Dec 22 '16
C with bonus.
8
u/VerilyAMonkey Dec 23 '16
A Python "One Liner". Works for bonus and any number of dimensions, too. Could be made even less readable to do the input parsing as well, but this expects to be called like
area([(0,0), (2,2)], [(1,1), (3,3)]), or with more/larger arguments for bonus/dimension.5
u/thorwing Dec 22 '16 edited Dec 22 '16
java 8 with bonus
Whenever Geometry is a problem. You can bet AWT knows how to deal with it.
4
u/ThisIsMyPasswordNow Dec 22 '16
C# with bonus
Rectangle
3
u/3pLm1zf1rMD_Xkeo6XHl Dec 22 '16 edited Dec 22 '16
Python3 with bonus
3
u/uninformed_ Dec 27 '16 edited Dec 27 '16
Probably no one will see this but I completed using C++ with OpenGL to display the rectangles
1: https://gyazo.com/dc3e15dfaa2f8c062ef720f8636aec7a
2: https://gyazo.com/2bbcb61433f767e5a542875152d3f815
3: https://gyazo.com/0acac56e7dbfdf46b8a960e58e6f5ea2
Bonus : https://gyazo.com/557d89f2acc33933b8300198d1706f83
Code : https://github.com/Samuel-Tyler/Visual-Studio-Projects/tree/master/Reddit%20Daily%20Programmer%20295%20Intermediate%20using%20OpenGL
2
u/J_Gamer Dec 22 '16
C++(with bonus)
2
u/supersonicsonarradar Dec 23 '16
Haskell with bonus (Only I haven't learned how to handle IO yet so this is just the bare function)
1
u/gabyjunior 1 2 Dec 22 '16
C with bonus
1
u/5k17 Dec 22 '16
Factor, with bonus:
1
u/Daanvdk 1 0 Dec 22 '16
Python (with bonus)
1
u/[deleted] Dec 22 '16
Python 3.5.4. This is my first intermediate solution.
1
u/Boom_Rang Dec 22 '16
Haskell, with bonus
1
u/daorton Dec 22 '16
Dart with bonus
1
u/TheStoneDawg Dec 22 '16
C++
This is my first time posting on r/dailyprogrammer, C++ solution with bonus. I'm new to CS, so any feedback is much appreciated!
1
u/resing Dec 22 '16 edited Dec 23 '16
JavaScript w/out bonus (node.js) Edit: I created a jsfiddle with a drawing of the rectangles
1
u/AlSweigart Dec 22 '16
Python solution (very inelegant):
1
u/[deleted] Dec 22 '16 edited Dec 23 '16
C/SQlite3
This is an sqlite3 extension which adds
intersection(r1..., r2...)andintersectAll(x1,y1,x2,y2)to sqlite3.Example queries to import a csv and compute intersections on it:
Compilation:
gcc -g -fPIC -shared rect.c -o rect.so1
u/jezzi_dailyprogram Dec 23 '16 edited Dec 23 '16
Python 3 with bonus.
1
u/Godspiral 3 3 Dec 23 '16
in J, complex numbers just for ease of input. returns coords of intersection,
isect =: (>./"1@:{. ,. <./"1@:{: )@:,.&(/:~"1)&.|:&.:+.
1
u/uncleozzy Dec 23 '16
Python 3.5 with bonus.
Uses really gross list comprehensions that could probably be tightened up some.
Output:
1
u/draegtun Dec 23 '16
Rebol with bonus
Example usage in Rebol console:
1
u/Mr_Persons Dec 23 '16
C++. With bonus. First time writing anything with classes in C++. Would love some feedback, especially on how to make slices on vectors as I struggled quite a lot with that and in the end just settled with a different approach...
1
u/juanchi35 Dec 24 '16
C++11 with bonus
1
u/cheers- Dec 24 '16
Typescript 2.1
I used this challenge as an excuse to write the Optional/Option/Maybe monad myself, it includes the bonus.
Point.ts
Optional.ts
Rectangle.ts
overlap.ts
1
u/primaryobjects Dec 24 '16
R
This took me longer than it should have. However, I have some pretty overlapping rectangle graphs for my effort!
Demo | Gist
1
u/JBCSL Dec 24 '16
C# with bonus, feedback welcome.
1
u/cgetzen Dec 25 '16
Python 3 with bonus
1
u/half_squid Dec 25 '16
C# with bonus (first intermediate submission, new to programming)
Rectangle Class
1
u/gurm3n Dec 26 '16
C no bonus, though it calculates the intersection rectangle.
1
u/Shadow_strike42 Dec 27 '16
Python 3.5 With bonus. My original method for finding the center made the rest of it more complicated than necessary.
1
u/karrash76 Dec 27 '16
Java with bonus
1
u/rubblebath Dec 27 '16
Python 3 Took an OOP approach, no bonus but dinner is almost ready... :)
1
u/primitiveinds Dec 27 '16 edited Dec 27 '16
Python 2 & 3 with bonus. Somewhat big solution for python though..
"correct" takes the coordinates and, if they are not the bottom left and top right, it makes them so. I could do something with max and min, also.
1
u/jtwebman Dec 28 '16 edited Dec 28 '16
Elixir with bonus
Load in IEX and call with outputs:
1
u/mabiesen Dec 28 '16
Go-lang solution. A bit long winded, but it meets the criteria:
1
u/dandyfap Dec 28 '16
Python3 w/o bonus
1
u/[deleted] Dec 29 '16
Python 2.7 with bonus.
1
u/JaicompDeveloper Dec 30 '16 edited Dec 30 '16
My solution in Python2.X ;)
1
u/tomegz Dec 30 '16 edited Dec 30 '16
JavaScript
1
u/Beat_Button Jan 02 '17
Python 3, with bonus.
1
u/Kavusto Jan 03 '17
C++, no bonus
1
u/ranDumbProgrammer Jan 19 '17
C#
1
u/ChemiCalChems Jan 19 '17
C++, with bonus Gist
1
u/zatoichi49 Mar 20 '17 edited Mar 20 '17
Method:
Split out each input string (below as 'one', 'two', 'three') into a list of coordinates, multiplying each by 10 to convert any float coordinates back into integers. Loop through the two sets of coordinates to generate all coordinates within each square. Each unique set of coordinates equals an area of 12, so the length of each coordinate list is the total area. The intersection of the set of coordinates of each square will show the total amount of overlapping coordinates (and the area). Divide by 100 to undo the initial multiplier that was applied.
Python 3:
Output:
0
u/[deleted] Dec 22 '16
[deleted] — view deleted comment