How to calculate the center of a rectangle in Pascal

2 Answers

0 votes
program RectangleCenter;
 
type
  Point = record
    x, y: Real;
  end;
 
  Rectangle = record
    topLeft: Point;
    bottomRight: Point;
  end;
 
// Function to calculate the center point of a rectangle
function GetCenter(rect: Rectangle): Point;
begin
  GetCenter.x := (rect.topLeft.x + rect.bottomRight.x) / 2.0;
  GetCenter.y := (rect.topLeft.y + rect.bottomRight.y) / 2.0;
end;
 
var
  rect: Rectangle;
  center: Point;
begin
  // Initialize the rectangle with top-left and bottom-right corners
  rect.topLeft.x := 10.0;
  rect.topLeft.y := 20.0;
  rect.bottomRight.x := 110.0;
  rect.bottomRight.y := 70.0;
 
  // Compute the center
  center := GetCenter(rect);
 
  WriteLn('Center of the rectangle: (', center.x:0:2, ', ', center.y:0:2, ')');
end.
 
 
 
 
(*
run:
 
Center of the rectangle: (60.00, 45.00)
 
*)

 



answered Jun 22, 2025 by avibootz
edited Jun 22, 2025 by avibootz
0 votes
program RectangleCenter;

uses
  Types;

var
  Rectt: TRect;
  Center: TPoint;

begin
  // Define the rectangle using its corners (x1, y1, x2, y2)
  Rectt := Rect(10, 20, 110, 70); 

  // Calculate the center point of the rectangle
  Center := CenterPoint(Rectt);

  WriteLn('Center of the rectangle: (', Center.X, ', ', Center.Y, ')');
end.



(*
run:

Center of the rectangle: (60, 45)

*)

 



answered Jun 22, 2025 by avibootz
edited Jun 22, 2025 by avibootz

Related questions

1 answer 127 views
1 answer 98 views
1 answer 72 views
1 answer 88 views
2 answers 120 views
...