algorithms – How can we find the number of subsets of points that can be enclosed by a rectangle?

I recently was wondering that if we were given a coordinate grid with N points on them (each at distinct positions), how could we calculate the number of subsets of those N points that can be enclosed within or on a rectangle?

The current algorithm I thought of is O(N^4), as it looks to choose the left and right x-bounds of the rectangle and then the lower and upper y-bounds of the rectangle. However, I think that there is probably an algorithm that is more efficient than this one. Can somebody please point me in the right direction?

javascript – Draw rectangle with aCoords tl, br corners instead of top, left in fabric.js?

So I wanted to draw rectangles on canvas based on coordinates received from an external system, which uses (x1,y1) (top-left) and (x2,y2) (bottom-right) coordinates to draw rectangle whereas in Fabric.js it’s a bit different such as using the top and left properties, I tried to do it using aCoords and wrote this code:

  addRect() {
    let square = new fabric.Rect({
      stroke: 'yellow',
      noScaleCache: false,
      strokeWidth: 10,
      strokeUniform: true,
      fill: 'rgba(0,0,0,0.2)',
      flipX: true,

    square.set("aCoords", { tl: new fabric.Point(100, 2), br: new fabric.Point(1, 2), tr: new fabric.Point(1, 2), bl: new fabric.Point(1, 2) });


This doesn’t seem to work, what am I missing ?

oc.optimization and control – how to draw a diagram of a rectangle using optimization

the question is
A rectangle has its two lower corners on the x-axis and its two upper corners on the curve y=10e^-(x^2)/18.

A) draw the diagram

I am not being lazy and asking someone to just solve it. I genuinely need to learn how to solve and would like to compare the correct answer with mine

geometry – How can the area of a rectangle having base $x$ inscribed in a fixed circle of radius $a$ be expressed as a function of $x$?

We are given a circle of radius $a$, and a rectangle of base $x$ is inscribed in that circle.

How to express the area of that rectangle as a function of $x$?

My Attempt:

The centroid of the rectangle (i.e. the point of intersection of the two diagonals) is the center of the circle. So each one of the two diagonals has length $2a$, which implies that the height of the rectangle is $sqrt{ 4a^2 – x^2}$, and therefore the area is given by
A(x) = x sqrt{ 4a^2 – x^2}.

Is this formula correct? If so, have I applied correct logic in reaching this formula? Or, have I made a mistake somewhere?

semidefinite programming – Non-negative bivariate polynomials in a rectangle

I have been working on non-negative univariate polynomials and I found the following equivalent relationship to check if a polynomial is non-negative or not:

The polynomial $g(x) = sum_{r=0}^k y_rx^r$ satisfies $g(x) geq 0$ for all $x in (a, b)$ if and
only if there exists a positive semidefinite matrix $X = (x_{ij})_{i,j=0,…,k}$, such that
sum_{substack{i,j=0,ldots,k \ i+j=2q-1}}x_{ij}=0, qquad q=1,ldots, k

sum_{substack{i,j=0,ldots,k \ i+j=2q}}x_{ij}=sum_{m=0}^qsum_{r=m}^{k+m-q}y_r {rchoose m} {k-rchoose q-m}a^{r-m}b^m, qquad q=0,ldots, k.

This result is found in
Bertsimas, D., & Popescu, I. (2005). Optimal inequalities in probability theory: A convex optimization approach. SIAM Journal on Optimization, 15(3), 780-804.

I was wondering if this result (or any other, preferably using semidefinite programming) have been extended to bivariate polynomials, that is to say, if someone can find conditions for a bivariate polynomial $g(x,y)=sum_{r=0}^ksum_{s=0}^l a_{r,s}x^ry^l$ to be non-negative over some rectangle $(a,b)times (c,d)$.

Thank you in advance.

sdl2 – How do I calculate the x,y of the rectangle after rotating?

I’m using SDL2. I want to draw a textured rectangle and I’d like it rotated so I’m using SDL_RenderCopyEx. However SDL2 doesn’t give me the option of repeating texture so I need to manually do it myself. The problem is I have no idea how to do the math.

My texture us 20×20, so I make the rects 20×20, I chosen a fixed angle of 45, my rotation point is 10,5 which appears to make the left middle of the rect the point it rotates from. I can see the two rects aren’t touching if the second rect is (firstX+15,firstY+15). However if I change one of them to 14 they touch and it looks solid

I don’t understand how to find the numbers. I’m finding the angle but doing

double angle = atan2(mouseX - x, mouseY - y) * 180 / PI;

Making the second rect relative to the first by +15,+14 only works if it’s a 45 deg angle but is complete wrong for other angles.

How do I figure out the x,y the subsequent rect based on the angle?

programming challenge – Largest rectangle areas after drawing boundaries

I was asked the following question while trying to get a Hackerrank certificate. I couldn’t solve on time but I now completed the solution. Can you please check if my solution seems good? Suggestions on improvement are appreciated.

Q: You have a rectangle of width w and height h. You will start to draw boundaries. The number of boundaries is given by the length of the given array, distance. isVertical(i) represents whether or not i-th boundary is vertical or not. 1 means vertical and 0 means horizontal. distance(i) tells the distance between your boundary and the rectangle. In case of a horizontal boundary, it represents the distance from the bottom of your rectangle, and in case of a vertical boundary, it represents the distance from the left of your rectangle.

You need to return the largest areas of your rectangle as an array after drawing each of your boundaries.

ex) w = 4, h = 4, isVertical = (0,1), distance = (3,1)
After drawing the first boundary, the largest possible rectangle area is 12.
After drawing the second boundary, the largest possible rectangle area is 9.
You are not given a new rectangle for each boundary. You draw boundaries on the same rectangle.

You should return (12, 9).

My solution:

  • I created one grid using a 2d-array and computed the largest possible rectangle area after each boundary is drawn by choosing the larger between the area taken up by the boundary and the current largest rectangle area minus the boundary area. I used the same, one grid to do computation for every boundary.
function largestArea(w, h, isVertical, distance) {
    if (isVertical.length !== distance.length) return null;
    if (w == 0 || h == 0) return 0;
    let grid = new Array(h).fill(0).map(() => new Array(w).fill()); // -1 represents areas of the original rectangle
    let largestArea = w*h;
    let areas = ();
    for (let i = 0; i < isVertical.length; i++) {
        let boundaryArea = 0;
        if (isVertical(i) == 1) {
            boundaryArea = drawVerticalBoundary(distance(i), grid); // distance(i) for vertical boundary is distance from the left
        } else {
            boundaryArea = drawHorizontalBoundary(h-distance(i), grid); // distance(i) for horizontal boundary is distance from the bottom
        largestArea = Math.max(largestArea-boundaryArea, boundaryArea);
    return areas;

function drawVerticalBoundary(maxCol, grid) {
    let area = 0;
    for (let row = 0; row < grid.length; row++) {
        for (let col = 0; col < maxCol; col++) { // area already taken by another boundary is not part of current boundary
            if (!grid(row)(col)) {
                grid(row)(col) = 1;
    return area;

function drawHorizontalBoundary(maxRow, grid) {
    let area = 0;
    for (let row = 0; row < maxRow; row++) {
        for (let col = 0; col < grid(0).length; col++) {
            if (!grid(row)(col)) { // area already taken by another boundary is not part of current boundary
                grid(row)(col) = 1;
    return area;