In my last challenge, you were asked to find all rectangles given a m x n
grid of them. However, it turned out to be very trivial as there actually was a mathematical formula I did not even know about to solve the problem! So now, for a little bit more of a challenge, how about calculating the number of unique rectangles, i.e. find the number rectangles that are all of different dimensions?
For example, consider 4 horizontal, or y
lines at [-250,-50,50,250]
and 4 vertical, or x
lines at [-250,-70,70,250]
. Graphing these on a coordinate plane with infinite dimensions results in the following 500 x 500
pixel closed grid, in which the length of each segment in pixels and the lines corresponding to their respected values from the arrays are shown:
which contains the 16
unique rectangles shown in this animated GIF:
However, if the topmost line (y = 250
) were to be removed, there would only be 12
unique rectangles, as the top 3 rectangles would be factored out since they each won't be fully closed without the y = 250
line.
Task
So, as shown above, the task is counting the number of rectangles rectangles with different dimensions. In other words, given an input of 2 arrays, with the first one containing all equations of all x
lines, and the latter containing those of all y
lines, output the total number of rectangles of different dimensions created when the lines corresponding to those equations are graphed on a coordinate plane.
Rules
The use of any built-ins that directly solve this problem is explicitly disallowed.
If either of the arrays have less than
2
elements, the output should be0
, since if there are less than4
lines on the plane, there are no closed,4
sided figures.The input arrays are not guaranteed to be sorted.
You can assume that there are not any repeated values in either of the the input arrays.
A
n x m
rectangle is the same as am x n
rectangle. For example, a300 x 200
rectangle is the same as a200 x 300
one.Standard loopholes are prohibited.
Test Cases
Given in the format Comma Separated Arrays Input -> Integer output
:
[],[] -> 0
[-250,-50,50,250],[-250,-70,70,250] -> 16 (Visualized above)
[-250,-50,50,250],[-250,-70,70] -> 12
[-40, 40],[-80, 50] -> 1
[10],[10] -> 0
[60, -210, -60, 180, 400, -400], [250, -150] -> 12
[0,300,500],[0,300,500] -> 6
[0,81,90],[0,90,100] -> 9
Remember, this is code-golf, so shortest code wins!
[[0,300,500],[0,300,500]]
should probably be a test case \$\endgroup\$6
. I forgot to count the entire square itself! It's updated now. \$\endgroup\$