Find 4 points which form a square with the biggest area I have set of points on the coordinate plane. I need to find the 4 points which form a square with the biggest area.

I'm new in JavaScript so I'd like to get any suggestions about code style, patterns, code idioms, etc. Thank you in advance!

"use strict";

/*
 * Point
 */

(function() {
    function numbersEqual(a, b) {
        return Math.abs(a - b) &lt; Number.EPSILON;
    }

    function Point(x, y) {
        this.x = x;
        this.y = y;
    }

    Point.distance = function(a, b) {
        return Math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2);
    }

    Point.equal = function(a, b) {
        return numbersEqual(a.x, b.x) &amp;&amp; numbersEqual(a.y, b.y);
    }

    Point.pointsFormsQuadrangle = function(a, b, c, d) {
        if (Point.equal(a, b) || Point.equal(a, c) || Point.equal(a, d))
            return false;

        if (Point.equal(b, c) || Point.equal(b, d))
            return false;

        if (Point.equal(c, d))
            return false;

        const center = new Point((a.x + b.x + c.x + d.x) / 4, (a.y + b.y + c.y + d.y) / 4);

        const ac = Point.distance(a, center);
        const bc = Point.distance(b, center);
        const cc = Point.distance(c, center);
        const dc = Point.distance(d, center);

        return numbersEqual(ac, bc) &amp;&amp; numbersEqual(bc, cc) &amp;&amp; numbersEqual(cc, dc);
    }

    Point.pointsFormsSquare = function(a, b, c, d) {
        if (!Point.pointsFormsQuadrangle(a, b, c, d))
            return false;   

        const ab = Point.distance(a, b)
        const ac = Point.distance(a, c);
        const ad = Point.distance(a, d);
        const triangle = [ab, ac, ad].sort((a, b) =&gt; a - b);

        return numbersEqual(triangle[0], triangle[1]);
    }

    window.Point = Point;
})();

/*
 * Square
 */

(function () {
    function Square(a, b, c, d) {
        this.a = a;
        this.b = b;
        this.c = c;
        this.d = d;
    }

    Square.prototype.area = function() {
        const findSquareDiagonalLength = (a, b, c, d) =&gt; {
            const ab = Point.distance(a, b);
            const ac = Point.distance(a, c);
            const ad = Point.distance(a, d);

            return Math.max(ab, ac, ad);
        };

        const d = findSquareDiagonalLength(this.a, this.b, this.c, this.d);
        return d * d / 2;
    }

    window.Square = Square;
})();

/*
 * Solution
 */

function formSquaresFromPoints(points) {
    let squares = [];

    for (let a = 0; a &lt; points.length; a++)
        for (let b = a + 1; b &lt; points.length; b++)
            for (let c = b + 1; c &lt; points.length; c++)
                for (let d = c + 1; d &lt; points.length; d++)
                    if (Point.pointsFormsSquare(points[a], points[b], points[c], points[d]))
                        squares.push(new Square(points[a], points[b], points[c], points[d]));

    return squares;
}

function sortSquaresByArea(squares) {
    squares.sort((a, b) =&gt; {
        if (a.area() &gt; b.area())
            return 1;
        else if (a.area() &lt; b.area())
            return -1;
        else
            return 0;
    });

    return squares;
}

const POINTS = [
    new Point(1, 1),
    new Point(1, 3),
    new Point(3, 1),
    new Point(3, 3),
    new Point(1, 6),
    new Point(6, 1),
    new Point(6, 6),
    new Point(1, 9),
    new Point(9, 1),
    new Point(9, 9),
    new Point(4, 5),
    new Point(4, 8),
    new Point(7, 8),
    new Point(7, 5),
    new Point(6, 3),
    new Point(5, 3),
    new Point(5, 1)
];

const sortedSquares = sortSquaresByArea(formSquaresFromPoints(POINTS));
const squareWithBiggestArea = sortedSquares[sortedSquares.length - 1];

console.log(squareWithBiggestArea); // Gives: {(1, 1), (1, 9), (9, 1), (9, 9)}
You're coding javascript the "old" way. With es6 you have a lot of new features available. Take a look at them.



Don't use self-invoking anonymous functions i.e. IIFEs

/// bad
(function(){
  // code here
})();


Use blocks

/// good
{
   // code here
}




Give meaningful names to your variables and methods. Functions should not have more than 2 parameters. If they do, you need to put them in an object.

/// bad
... pointsFormsQuadrangle = function(a, b, c, d){}

/// good
static FormsQuadrangle([point1, point2, point3, point4]){

}




Use the class syntax instead of function/prototype.

class Utils {
  static NumbersEqual(a, b){
    return Math.abs(a - b) &lt; Number.EPSILON;
  }
}

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

  distance(point) {
    return Math.sqrt((this.x - point.x) ** 2 + (this.y - point.y) ** 2);
  }

  equal(point) {
    return Utils.NumbersEqual(this.x, point.x) &amp;&amp; Utils.NumbersEqual(this.y, point.y);
  }

  static FormsQuadrangle(a, b, c, d) {
    if (a.equal(b) || a.equal(c) || a.equal(d)) return false;

    if (b.equal(c) || b.equal(d)) return false;

    if (c.equal(d)) return false;

    const center = new Point((a.x + b.x + c.x + d.x) / 4, (a.y + b.y + c.y + d.y) / 4);

    const ac = a.distance(center);
    const bc = b.distance(center);
    const cc = c.distance(center);
    const dc = d.distance(center);

    return Utils.NumbersEqual(ac, bc) &amp;&amp; Utils.NumbersEqual(bc, cc) &amp;&amp; Utils.NumbersEqual(cc, dc);
  }

  static FormsSquare(a, b, c, d) {
    if (!Point.FormsQuadrangle(a, b, c, d))
      return false;

    const ab = a.distance(b)
    const ac = a.distance(c);
    const ad = a.distance(d);
    const [tr1, tr2] = [ab, ac, ad].sort((a, b) =&gt; a - b);

    return Utils.NumbersEqual(tr1, tr2);
  }
}