The blockchain is going to store an array of structs: x1,y1,x2,y2 (uint) to represent the upper left and lower right corners of a rectangle.
When a new rectangle is added, I need to validate that it does not overlap with any other rectangles in the blockchain.
This is what I'm thinking of doing. Create another struct for the points, containing: x, y, index to the main rectangle array.
I will have two arrays, one sorted by X, and another sorted by Y. Both corners that define each rectangle will have entries in both arrays.
For the validation of the new rectangle, I search for whether any entry in the X array exists in between the 2 X's. The same goes for Y. If any of the returned entries for X and Y have the same "main array" index, then there's an overlap. I also have to validate whether the new rectangle is totally within another rectangle.
I'm still getting myself started with Solidity to try this out. It seems though that this is a very expensive process, and may not be scalable. I have to scan through 2 arrays multiple times to validate every new rectangle that I add.
Is there a more efficient way of validating my new rectangle without having to scan through 2 arrays?
The other option is to keep to just 1 array, and validate each rectangle against the new one. Again, this sounds quite expensive.