Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSql Rectangle DataType for storing Left, Top, Width and Height

I am trying to store a rectangle inside a table.

At the moment i have 4 seperate columns:

  • top
  • left
  • width
  • height

I noticed that PostgreSql offers a datatype called box.

However this datatype automatically reorders the values based on:

Any two opposite corners can be supplied on input, but the values will be reordered as needed to store the upper right and lower left corners, in that order.

I tried using the box datatype for rectangles but as stated it sometimes mixes upperright and bottomleft, so it is not suitable for storing top, left, width and height.

I am writing a C# application that handles those values. Therefore it would be nice if i have a datatype that is equivalent to the C# Rectangle type.

I am wondering how to implement a rectangle / rect datatype. Maybe even write an own datatype based on four integers (array[4])?

like image 430
Thomas Avatar asked Feb 14 '26 10:02

Thomas


1 Answers

It doesn't mix the points, it stores them with the top right values first. You will just have to read out these and work out the other 2 points yourself if you need them.

That's not exactly complicated stuff - at the least you can read the top/right point and take the top value, and the bottom/left point and take the left point to obtain the top/left point you want. Similarly, reading width is just right minus left.

like image 101
gbjbaanb Avatar answered Feb 17 '26 01:02

gbjbaanb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!