How to calculate the center of a rectangle in Node.js

1 Answer

0 votes
class Point {
  constructor(x, y) {
    this.x = x;
    this.y = y;
  }
}

class Rectangle {
  constructor(topLeft, bottomRight) {
    this.topLeft = topLeft;
    this.bottomRight = bottomRight;
  }
}

function getCenter(rect) {
  const centerX = (rect.topLeft.x + rect.bottomRight.x) / 2.0;
  const centerY = (rect.topLeft.y + rect.bottomRight.y) / 2.0;
  return new Point(centerX, centerY);
}

function main() {
  const rect = new Rectangle(new Point(10.0, 20.0), new Point(120.0, 80.0));
  const center = getCenter(rect);

  console.log(`Center of the rectangle: (${center.x.toFixed(2)}, ${center.y.toFixed(2)})`);
}

main();


     
/*
run:
     
Center of the rectangle: (65.00, 50.00)
     
*/

 



answered Jun 23, 2025 by avibootz

Related questions

1 answer 105 views
1 answer 118 views
1 answer 113 views
1 answer 122 views
1 answer 89 views
...