Bug in Rect?

Type this info a console app (or open event and replace print by system.debuglog or whatever) and run it.

Var OrigRect As New Rect(0, 0, 200, 200)
Var smallerRect As New Rect(0, 0, 100, 100)
smallerRect.Center = OrigRect.Center
print smallerRect.Left.ToString + "/" + smallerRect.top.ToString

Would you agree that the result according to the docs should be 50/50?
They stay at 0/0 with 2021r1.1. A known bug?

No, I don’t think it’s a bug since the Rects are position-relative to themselves. They have no (apparent) parent window/canvas/etc so Left and Top are remaining relative to the Rect itself. The line

smallerRect.Center = OrigRect.Center

merely positions them but does not define any other relationships between them.

Do you have older einhugur plugins? they had a Rect class, which at our side overtook the native xojo class.

Thanks, Dale. But then, why do the docs say

?

1 Like

No, all up to date. Thanks for the hint anyway!

1 Like

Because that’s exactly what it does. You can think of it as a 100 cm square piece of paper on top of a 200 cm square piece of paper. Positioning the smaller one so its center is aligned with the center of the larger one doesn’t change the fact that its Left edge has no relation to the Left edge of the larger one (except maybe visually).

If you are needing the relative positions, then you need to define something they are both relative to. For example, drawing them into a Canvas will give then both positional relevance to the Canvas and using the Rect.Center to align them will allow you to get the relationship of their Left edges relative to the Canvas. But without a common reference, they are just two objects floating in cyberspace that happen to be the same shape.

Thanks for your answer, Dale, but I don’t get your point.
The docs say “Changing the center value will move the rect”.
How does a rect move? By changing its origin.
When I take your example: When I align a smaller piece’s of paper center point to that of a bigger ones, of course I expect the smaller one to move. Otherwise Center would just be a read-only property. From the documentation it is not, so why can it be set without changing the origin?

EDIT: Of course I do agree you have to have a reference system, so it would rather make sense to have a method like Rect.Center(toRect as Rect). But basically that reference system is derived from setting the center coordinates.

Now, to continue, if you want them relative to each other then you need to have something like

print Str((OrigRect.width - smallerRect.width) /2) + "/" +Str((OrigRect.Height - smallerRect.Height) /2) 

to establish the relationships between the two rectangles.

Hi Ulrich,

Have you tried the following:

smallerRect.Center = OrigRect.Center * 2

I don’t know why, but when trying to match up the center of Rect objects, that works for me. See Opening Modal Windows on Second Screen

Edit: Better answer below

Thanks, Scott! How is Point * 2 supposed to be read? x *2 and y * 2?

I’m sorry Ulrich, I was thinking one thing and writing another.

Please try:

smallerRect.VerticalCenter = OrigRect.VerticalCenter * 2
smallerRect.HorizontalCenter = OrigRect.HorizontalCenter * 2

Should’ve read my own post more thoroughly.

Hope that helps.