Page 1 of 1

procedure TGraphConstraints.ConfineRect(var Rect: TRect);

PostPosted: December 16th, 2005, 12:09 pm
by elias
Although two points (TopLeft and BottomRight) are enough to confine a Rectangle inside the bounds of SimpleGraph, it is necessary the points to keep the same relation of distance at x and y coordinates to keep the size of the rectangle; not only confining the two points separately:


Code: Select all
procedure TGraphConstraints.ConfineRect(var Rect: TRect);  //(It doesnt keep the size of the rect)
begin
  ConfinePt(Rect.TopLeft);     //confining point (TopLeft)
  ConfinePt(Rect.BottomRight);    //confining point (BottomRight)
end;


The alternative I found to this code is the following:

Code: Select all
procedure TGraphConstraints.ConfineRect(var Rect: TRect); //(Keeping the size of the Rect)
var DistanceBetweenYs,DistanceBetweenXs:integer;
begin

DistanceBetweenYs:=(Rect.Right-Rect.Left);   //   Right:Top
DistanceBetweenXs:=(Rect.Top-Rect.Bottom);   //   Left:Bottom

  if Rect.Left < BoundsRect.Left then
    Rect.Left := BoundsRect.Left;
  if Rect.Right - DistanceBetweenYs < BoundsRect.Left then
    Rect.Right := BoundsRect.Left+ DistanceBetweenYs;

  if Rect.Right > BoundsRect.Right then
    Rect.Right := BoundsRect.Right;
  if Rect.Left + DistanceBetweenYs > BoundsRect.Right then
    Rect.Left := BoundsRect.Right - DistanceBetweenYs;

  if Rect.Bottom > BoundsRect.Bottom then
    Rect.Bottom := BoundsRect.Bottom;
  if Rect.Top - DistanceBetweenXs > BoundsRect.Bottom then
    Rect.Top := BoundsRect.Bottom + DistanceBetweenXs;

  if Rect.Top < BoundsRect.Top then
    Rect.Top := BoundsRect.Top;
  if Rect.Bottom + DistanceBetweenXs < BoundsRect.Top then
    Rect.Bottom := BoundsRect.Top - DistanceBetweenXs;

end;


...yes...

PostPosted: December 17th, 2005, 10:39 am
by Kambiz
Thank you elias!

I was missed this important issue.