## opengl – Applying spacing between moving, rotated rectangles

I am trying to implement basic Snake game with movement based on timestep. For now I’ve got something along those lines:

Update function:

``````glm::vec2 headPosition = glm::vec2(m_segments.begin()->m_sprite.get_position());
float angle = m_segments.begin()->m_sprite.get_angle();

m_segments.begin()->move();

for (auto it = m_segments.begin() + 1; it != m_segments.end(); it++)
{
glm::vec2 oldPosition = it->m_sprite.get_position();
float oldAngle = it->m_sprite.get_angle();

it->m_sprite.set_angle(angle);

angle = oldAngle;
}
``````

Move function:

``````glm::vec2 position(m_sprite.get_position());
position.x +=  cosf(glm::radians(90.0f - m_sprite.get_angle())) * VELOCITY_SCALAR;
position.y += -sinf(glm::radians(90.0f - m_sprite.get_angle())) * VELOCITY_SCALAR;

m_sprite.set_position(position);
``````

Basically I set next segment position to the old position of previous segment. As expected spacing between those segments is equal to timestep value. How can I calculate position so I can manipulate spacing between those segments?

## javascript – Indexing and searching an list of rectangles to see if they contain a point

I have a flat list of rectangles, in the following format (id, x co-ord, y co-ord, height and width):

``````( { id: 4206, x: 360, y: 700, width: 60, height: 50 } ,
{ id: 6621, x: 1260, y: 1100, width: 60, height: 50 } )
``````

I want to index this by range of X values, and then use that indexed object to speed up a search for a point inside.

My full code is here:

``````// arbitrary setup, no need to review
const grid = ();
const height = 50;
const width = 60;
let index = 0;
for (let row = 0; row < 300; row++) {
for (let col = 0; col < 300; col++) {
grid(index) = {
id: index,
x: width * col,
y: height * row,
width,
height
};
index++;
}
}
// try and find these points
const points = ({
x: 400,
y: 700
}, {
x: 1300,
y: 1105
})

// body - please review approach, style less important

const indexedGrid = grid.reduce((acc, curr) => {
if (curr.x === undefined) return acc;
if (acc(curr.x)) {
acc(curr.x).shapes.push(curr);
acc(curr.x).endEdge = Math.max(acc(curr.x).endEdge, curr.width + curr.x)
} else acc(curr.x) = {
shapes: (curr),
startEdge: curr.x,
endEdge: curr.width + curr.x
};
return acc;
}, {})
const indexedEdges = Object.values(indexedGrid).map(val => ({
start: val.startEdge,
end: val.endEdge
}));
const shapesContainingPoints = points.map(
(point) => ({
point,
xRange: indexedEdges.filter(
edge => (point.x >= edge.start && point.x < edge.end)
)
})
).map(
pointWithRange => indexedGrid(pointWithRange.xRange(0).start).shapes.filter(
shape => {
return (pointWithRange.point.y >= shape.y && (pointWithRange.point.y < (shape.y + shape.height)))
}
)
);

//footer - purely to show results in the this question, no need to review
console.log(shapesContainingPoints)``````

I’m fully aware this code could be tidied up a lot, but the main thing I was hoping to ascertain, is if this ‘index and search’ pattern suits the following use case.

I’m drawing a load of rectangles in a html `canvas` element, and I want to be able to find which rectangle was clicked on using the `x, y` co-ords of the mouse. So I plan to create a flat list of the squares, and then index them by their X value. And from that X value, look along the row, filtering by Y value.

Currently the data only includes perfectly tiling rectangles, but in the future I may have to handle an alternating offset (every other row is shifted by a small value) or other less standard layouts.

But it feels very clunky, and that there should be a better approach to this problem? If not, is there anyway of improving my current approach?

## What are the minor yellow rectangles in Device Manager with exclamation mark, when "This device is working properly"?

What are the minor yellow rectangles in Device Manager with exclamation mark, when "This device is working properly"?

## machine learning – Axis aligned rectangles: why is A an ERM in the case of infinite domain?

I’m working on a problem 2.3a in Shalev-Shwartz/Ben-David’s Machine learning textbook, which states:

An axis aligned rectangle classifier in the plane is a classifier that assigns 1 to a point if and only if it is inside a certain rectangle. Formally, given real numbers $$a_1leq b_1, a_2leq b_2,$$ define the classifier $$h_{(a_1, b_1, a_2, b_2)}$$ by
$$h_{(a_1, b_1, a_2, b_2)}(x_1, x_2) = begin{cases}1&textrm{if a_1leq x_1leq b_1 and a_2leq x_2leq b_2}\ 0&textrm{otherwise}end{cases}$$
The class of all axis aligned rectangles in the plane is defined as $$mathcal{H}_mathrm{rec}^2 = {h_{(a_1, b_1, a_2, b_2)}:textrm{a_1leq b_1 and a_2leq b_2}}$$…rely on realizability assumption.
Let $$A$$ be an algorithm that returns the smallest rectangle enclosing all positive examples in the training set. Show that $$A$$ is ERM (empirical risk minimizer).

I don’t understand why $$A$$ would be ERM, when the domain/instance space is infinite. Below is the counterexample I considered to this claim.

For simplicity, consider the one-dimensional case, where the rectangles would be intervals. Sample instance $$xsim mathcal{U}_{(0, 1)}$$ from a uniform distribution over $$(0, 1)$$, where the labeling function $$f:(0, 1)to{0, 1}$$ is defined by
$$f(x) = begin{cases} 1 &textrm{x = 0 or x = 1}\ 0 &textrm{otherwise,} end{cases}$$
then the realizability assumption holds as there exists an $$h^*$$ whose rectangle described by the singleton $${1}$$ has an error set $${0}$$ of measure $$0$$. However, in the case of a particular sample
$$S = {0, 1/4, 1/2, 3/4, 1},$$
the smallest rectangle $$A$$ is the interval $$(0, 1)$$, and the consequent classifier $$h_A$$ has an empirical error of $$L_S(h_A) = 3/5$$. In contrast, the classifier $$h^*$$ has empirical error $$L_S(h^*) = 1/5$$, a lower error than the classifier described by $$A$$, even though it doesn’t contain all positive examples. Therefore $$A$$ does not minimize empirical error.

## graphics card – Weird black rectangles and other artefacts when playing certain video games

I have an ASUS Strix Scar 3.

I have the following problem of this weird, black artifacting on my screen:

View post on imgur.com

another example here:

View post on imgur.com

It happens way more when I’m moving and the camera is close to something, and usually is more common when emitting light particles. It mostly happens in WoW, but happens in other games too sometimes.

What I have just learned, is that when using an external monitor, this effect does not happen. So it has to do with my computer’s own monitor. My refresh rate is 240Hz and 3ms. Manually limiting the FPS does not fix anything. It does this at low and high FPS. Also, is there a name for what’s happening here? Is it screen tearing? Artifacting?

Some logs (which will also show my specs in better detail):

MSInfo (had to use a different paste website since it exceeded limit): https://controlc.com/bb5cc262

DxDiag:https://pastebin.com/Ey1j1qqi

## How to do collision resolution with 2d rectangles?

I am trying to create a basic side scroll 2D game. There are lots of documentation on collision detection but I find the easiest to use at the moment is just basic AABB Collision. This is my basic outline

``````Rect old_position = player->rect;
if(key == a) {
player.x += 5; // move 5 px to right
}
if ... // same for up and left

if(key == s) {
player.y += 5; // move 5 px down
}

player.y += 4; // move player down, gravity

if(collision_detection(player->rect, rectangle->rect)) { // rect = {x pos, y pos, w idth h eight}
if(player->rect.y - old_position.y > 0) {
player->rect.y = rectangle->rect.y - player->rect.h; // if player moved down and collided, move the player to the same pos as object + its own height to get directly above the object.
} else if(player->rect.y - old_position.y < 0) {
player->rect.y = rectangle->rect.y + rectangle->rect.h; // move to directly under collided object.
}
...
}
``````

this does not work since if I come from the side, falling down, I get teleported up above the object! There are people who write about things such as roleback / moving back before the collision happened, etc. but there is not straight forward way of doing collision resolution. When I try different methods they might work but I always slide into them if I walk straight into a wall or if I jump and move into a wall in the air while jumping I glitch inside the rectangle, etc, etc.

Does anyone have a blueprint for different 2D collision resolution? I am curious what types there are and what pros and cons there is. The above code is just sample dummy code, so there is no need to critique it. Thanks!

## why i have black rectangles on my photo?

sony a6000
iso 100
3.2
f/5
sigma 60 mm (90mm for aps-c)

## Complete proof of PAC learning of axis-aligned rectangles

I have already read PAC learning of axis-aligned rectangles and understand every other part of the example.

From Foundations of Machine Learning by Mohri, 2nd ed., p. 13 (book) or p. 30 (PDF), I am struggling to understand the following sentence of Example 2.4, which is apparently the result of a contrapositive argument:

… if $$R(text{R}_S) > epsilon$$, then $$text{R}_S$$ must miss at least one of the regions $$r_i$$, $$i in (4)$$.

i.e., $$i = 1, 2, 3, 4$$. Could someone please explain why this is the case?

The way I see it is this: given $$epsilon > 0$$, if $$R(text{R}_S) > epsilon$$, then $$mathbb{P}_{x sim D}(text{R}setminus text{R}_S) > epsilon$$. We also know from this stage of the proof that $$mathbb{P}_{x sim D}(text{R}) > epsilon$$ as well. Beyond this, I’m not sure how the sentence above is reached.

## nearest neighbour – assign points to non-overlapping rectangles

I have N (~100M) points in 2D and M (~10k) non-overlapping rectangles. I’m looking for an algorithm to assign each point to a rectangle it is contained in (or say that it is outside of any rectangle). I’m looking for a solution that works faster than bruteforce.

In 1D, it is achievable in N * log(M) using binary search. is something like that possible in 2D?

## algorithms – Constructing Rectangles With Jigsaw type A and type B

One way to think of it would be elementary blocks, i.e. blocks that cannot be split by cutting them vertically.

• There’s one elementary block of size 1×2 (one vertical B).
• There’s one elementary block of size 2×2 (two horizontal Bs).
• There are two elementary blocks of size 3×2 (the last two shown in your figure, Example I).
• There are two elementary blocks of size 4×2 (they both appear in the final pattern of Example II).
• In general, there are two elementary blocks of size $$n times 2$$, $$n ge 3$$.

Now, an $$n times 2$$ rectangle can be divided into two smaller blocks by cutting the rightmost elementary block. (This includes the case where the elementary block is the entire rectangle).

• A 0x2 rectangle can be tiled in 1 way.
• From a 1×2 rectangle, we can cut the rightmost elementary block (we have 1 of them) and we’re left with a 0x2 rectangle, so it can be tiled in 1 way.
• From a 2×2 rectangle, we can either cut a 2×2 block (1 way) or cut a 1×2 block and be left with a 1×2 rectangle (1 * 1 = 1 way), for a total of 2 ways.
• From a 3×2 rectangle:
• cut a 3×2 block (2 ways)
• cut a 2×2 block (1 way) and be left with a 1×2 rectangle (1 way)
• cut a 1×2 block (1 way) and be left with a 2×2 rectangle (2 ways)
• Total: 2 + 1 * 1 + 1 * 2 = 5 ways.

This way, you can work all the way to 10×2.