From d7263eac746e87141ea9938f33c98e182f0b5264 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Thu, 10 Nov 2022 22:08:41 +0100 Subject: [PATCH] Square and rectangle convenience ctor for GSI Box and DBox --- src/db/db/gsiDeclDbBox.cc | 24 ++++++++++++++++++++++++ testdata/ruby/dbBoxTest.rb | 18 ++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/db/db/gsiDeclDbBox.cc b/src/db/db/gsiDeclDbBox.cc index 42a6789a2..f71a54d0e 100644 --- a/src/db/db/gsiDeclDbBox.cc +++ b/src/db/db/gsiDeclDbBox.cc @@ -59,6 +59,16 @@ struct box_defs return new C (); } + static C *new_sq (coord_type s) + { + return new C (-s / 2, -s / 2, s / 2, s / 2); + } + + static C *new_wh (coord_type w, coord_type h) + { + return new C (-w / 2, -h / 2, w / 2, h / 2); + } + static C *new_lbrt (coord_type l, coord_type b, coord_type r, coord_type t) { return new C (l, b, r, t); @@ -121,6 +131,20 @@ struct box_defs "box is also an empty box. The width, height, p1 and p2 attributes of an empty box are undefined. " "Use \\empty? to get a value indicating whether the box is empty.\n" ) + + constructor ("new", &new_sq, + "@brief Creates a square with the given dimensions centered around the origin\n" + "\n" + "Note that for integer-unit boxes, the dimension has to be an even number to avoid rounding.\n" + "\n" + "This convenience constructor has been introduced in version 0.28." + ) + + constructor ("new", &new_wh, + "@brief Creates a rectangle with given width and height, centered around the origin\n" + "\n" + "Note that for integer-unit boxes, the dimensions have to be an even number to avoid rounding.\n" + "\n" + "This convenience constructor has been introduced in version 0.28." + ) + constructor ("new", &new_lbrt, gsi::arg ("left"), gsi::arg ("bottom"), gsi::arg ("right"), gsi::arg ("top"), "@brief Creates a box with four coordinates\n" "\n" diff --git a/testdata/ruby/dbBoxTest.rb b/testdata/ruby/dbBoxTest.rb index 4f6c1de10..05dd03daa 100644 --- a/testdata/ruby/dbBoxTest.rb +++ b/testdata/ruby/dbBoxTest.rb @@ -29,6 +29,15 @@ class DBBox_TestClass < TestBase # DBox basics def test_1_DBox + a = RBA::DBox::new( 20 ) + assert_equal( a.to_s, "(-10,-10;10,10)" ) + + a = RBA::DBox::new( 21 ) + assert_equal( a.to_s, "(-10.5,-10.5;10.5,10.5)" ) + + a = RBA::DBox::new( 20, 40 ) + assert_equal( a.to_s, "(-10,-20;10,20)" ) + a = RBA::DBox::new( -10, 21, 11, 17 ) assert_equal( a.to_s, "(-10,17;11,21)" ) assert_equal( RBA::DBox::from_s(a.to_s).to_s, a.to_s ) @@ -233,6 +242,15 @@ class DBBox_TestClass < TestBase # Box basics def test_1_Box + a = RBA::Box::new( 20 ) + assert_equal( a.to_s, "(-10,-10;10,10)" ) + + a = RBA::Box::new( 21 ) + assert_equal( a.to_s, "(-10,-10;10,10)" ) + + a = RBA::Box::new( 20, 40 ) + assert_equal( a.to_s, "(-10,-20;10,20)" ) + a = RBA::Box::new( -10, 21, 11, 17 ) assert_equal( a.to_s, "(-10,17;11,21)" ) assert_equal( RBA::Box::from_s(a.to_s).to_s, a.to_s )