## 3d – Calculate coordinates of boundaries containing group of objects

I would like to understand how to calculate coordinates of boundaries group of objects as a parallelepiped in 3D game?

Let’s say for simplicity we have spheres randomly slowly moving in 3d space some of them cross each other from time to time and forming groups from 2 or more object locating very close to each other. Now I need to create dynamic parallelepiped surrounding this formed group as long as this group still formed because spheres moving constantly and such groups may appear and disappear chaotically.

So how to calculate coordinates of this parallelepiped faster? The naive approach I was thinking is just calculate distance of each object from each other individually to know to which group each sphere belongs which is slow O(n^2) time complexity. Is there more efficient solution to solve this problem?

Posted on Categories Articles

## computational geometry – Algorithm to construct a parabola that hits a given target and avoids given boundaries

I’m working on a video game and I’m struggling with the math behind one of the enemies. The enemy is a grenade launcher mounted on a vertical rail, which can slide up and down, and lob a grenade at any angle with any amount of force. The grenade’s path will be a parabola which must hit the player, but there are line segment boundaries in the way represented by their two endpoints which the parabola must avoid.

Here is a drawing.

What I’d like to do is calculate the equation for the parabola of the grenade which hits a target and misses all of the boundaries, from which I can figure out the position, angle, and force for the launcher to use. The parabola must be subject to these three constraints:

1. The parabola must pass through the target point $$(x_T, y_T)$$
2. The parabola must pass through the line segment $$overline{RS}$$
3. For each boundary $$overline{EF}$$, if the parabola passes through the segment, it must not happen between the line $$overline{RS}$$ and the target point.

Depending on where the target is, there may be no solution, in which case I’d like it to return that information. If there is any solution there will be multiple; I would only need one.

## What I’ve tried so far:

We can represent the parabola as $$y-y_T=A(x-x_T)^2+B(x-x_T)$$, which takes care of the first constraint, and means we need to find values for $$A$$ and $$B$$ that satisfy the other two constraints (we know $$A$$ must be negative because of the direction of gravity). Then for each boundary $$overline{EF}$$ on the map, with endpoints $$(x_E,y_E)$$ and $$(x_F,y_F)$$, we can represent the boundary line as $$(y_E-y_F)x+(x_F-x_E)y+(x_E y_F-x_F y_E)=0$$. From that I can find the points of intersection between the line and the parabola, and make sure that for every boundary, the x-values of the points are either not between $$x_E$$ and $$x_F$$, or they are not between $$x_S$$ and $$x_T$$. This quickly becomes a nasty quadratic equation, which then creates a system of linear inequalities that I don’t know how to solve. Can anyone think of a better way to approach the problem?

Posted on Categories Articles

## pr.probability – Uniqueness of stationary measures for \$(G,mu)\$ boundaries

Let $$G$$ be a countable group acting minimally by homeomorphisms on a compact Hausdorff space $$X$$ and $$mu$$ be a probability measure whose support generates $$G$$ as a semigroup.
Let $$nu$$ is a $$mu$$-stationary measure on $$X$$ such that $$(X,nu)$$ is a $$(G,mu)$$ boundary, ie for $$mu^{mathbb{N}}$$ almost every trajectory $$g_n$$ the pushforwards $$g_1…g_n nu$$ weakly converge to a dirac measure.

Does it follow that $$nu$$ is the unique $$mu$$-stationary Borel probability measure on $$X$$? This is the case in many geometric situations, for instance when the action is on the Gromov boundary of a hyperbolic group. In general I suspect the answer is false but cannot think of a counterexample…

Posted on Categories Articles

## unity – Screen boundaries seem randomly placed

I have a strange issue in which the characters boundaries work fine, however are placed at four seemingly random edges outside of the game view as a result of (I’m assuming) my player boundaries script. The player actually stopping at the edges is working fine, the issue is just with the placement of those boundaries. I’m following a tutorial, and the code is pretty much just copy/pasted, so I’m not sure what’s going wrong.

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PSBoundariesOrthographic : MonoBehaviour {
public Camera MainCamera;
private Vector2 screenBounds;
private float objectWidth;
private float objectHeight;

// Use this for initialization
void Start () {
screenBounds = MainCamera.ScreenToWorldPoint(new Vector3(Screen.width, Screen.height, MainCamera.transform.position.z));
objectWidth = transform.GetComponent<SpriteRenderer>().bounds.extents.x; //extents = size of width / 2
objectHeight = transform.GetComponent<SpriteRenderer>().bounds.extents.y; //extents = size of height / 2
}

// Update is called once per frame
void LateUpdate(){
Vector3 viewPos = transform.position;
viewPos.x = Mathf.Clamp(viewPos.x, screenBounds.x * -1 + objectWidth, screenBounds.x - objectWidth);
viewPos.y = Mathf.Clamp(viewPos.y, screenBounds.y * -1 + objectHeight, screenBounds.y - objectHeight);
transform.position = viewPos;
}
}
``````

Above is my boundaries script

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class controlPlayer : MonoBehaviour
{
public GameObject player;
public float speed = 1000f;
public GameObject left;

bool _isLeftHeld;
bool _isRightHeld;

public void Update()
{
var movement = Input.GetAxis("Horizontal");

// Ignore cases where neither/both left and right are held at once.
if (_isRightHeld ^ _isLeftHeld)
{
// Override the movement input from the keyboard/joypad.
if (_isRightHeld) movement = 1f;
else movement = -1f;
}

transform.position += new Vector3(movement, 0, 0) * Time.deltaTime * speed;
}

public void HoldRight(bool held)
{
_isRightHeld = held;
_isLeftHeld = false;
}

public void HoldLeft(bool held)
{
_isLeftHeld = held;
_isRightHeld = false;
}
}
``````

My player controller. I should add that hold left and hold right are controlled by two buttons placed accordingly on the screen.

Posted on Categories Articles

## image processing – Separating boundaries from WatershedComponents

I am trying to look for local minimum on the boundaries between watershed regions.

Typically the boundaries are not connected and spanning across the whole image so it is possible to do it through

``````MorphologicalComponents[
1 - ImageData[Binarize[Colorize[WatershedComponents[image]], 0.1]]
]
``````

The problem is when there are more than two watershed components are intersecting at a point and the boundary collides. The boundaries will then considered as one entity. This is illustrated as the green line in the picture where the watershed components are treated as the foregrounds. Is there an efficient way to get unique pixels corresponding to the boundaries between two watershed components?

Posted on Categories Articles

## 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);
areas.push(largestArea);
}
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)) {
area++;
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
area++;
grid(row)(col) = 1;
}
}
}
return area;
}
``````

Posted on Categories Articles

## Unity calculating the boundaries of camera in 2d game

So im starting to learn Unity, working on a top down 2D game. I know I can use something like Cinemachine to clamp my camera to a 2DCollider, but I decided to try it to implement this manually as a exerciser.

Here is my plan:

1.) I put a BoxCollider2D around my map. I set collider to trigger mode to avoid collisions (although I guess I could just put it on another layer)

2.) I add that Box Collider to my camera object.

3.) In my camera object i put the following update code

``````        Vector2 min = (Vector2) roomColider.bounds.min + new Vector2(6,6);
Vector2 max = (Vector2)roomColider.bounds.max - new Vector2(roomColider.offset.x / 2 + 1, roomColider.offset.y / 2 + 1);
if(transform.position != target.position) {
Vector3 targetPosition = new Vector3(target.position.x, target.position.y, transform.position.z);
targetPosition.x = Mathf.Clamp(targetPosition.x, min.x, max.x);
targetPosition.y = Mathf.Clamp(targetPosition.y, min.y, max.y);
// Lerp for a smooth camera motion
transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing);
}
``````

However this seems to be off. It does clip the camera, but the boundingbox is some amount of units (~6 units) off in this case.

Posted on Categories Articles

## sql server – Transactions blocking other transactions, even across database boundaries

I’ve been banging my head against this problem for months now, and maybe I just don’t know the magic words to search for, but Google hasn’t been useful. I’ve found all sorts of ways to optimize queries and find and reduce resource locks so that a locked table or row doesn’t hold up transactions waiting on that resource, but I haven’t found anything discussing the problem I am having. So, if I am simply looking in the wrong place, help in refining my search would also be a suitable answer here (such as, as I mostly expect, this is a duplicate question of another I couldn’t find due to using the wrong search terms).

Anyway, my problem is this: if I have a long running transaction running in one database (completely contained within that database), the whole server freezes up. Queries in other databases are blocked until the first query finishes – which could be minutes. Even the simplest queries, like the TOP X of a table, or right clicking a table/database in SSMS and waiting for a context menu. I can’t even get the Activity Monitor to work while it is frozen.

And there is nothing particularly special about the query that is blocking everything. It isn’t a specific query, but pretty much any long running query, usually SELECT queries (as this is supposed to be a data warehouse). One or two joins, 3 or 4 WHERE clauses, an ORDER BY – it is usually only long running because it is iterating over a lot of rows. Not using transactions, no triggers defined, etc. The query is often, but not always, a view, if that makes a difference.

I can’t rule out that the SELECT query might be consuming resources in one or more system databases (tempdb) and that this shared resource is responsible for the system wide blocking, but the system is largely set up using defaults, so it seems like poor design if a relatively simple SELECT query could monopolize system databases by default, and I have trouble visualizing how an SSMS context menu TOP 10 query against a single table could depend on tempdb. This question seems partly relevant, but we have plenty of RAM and the resources used by the queries are completely contained in their respective databases (the query might join across databases, but the two databases used by the query are unrelated to the third database used by the blocked query), so only option 3 seems likely, but I don’t know either how to check that or how to correct it if it turns out to be the case. The databases are stored on a SAN over a fiber channel connection, with partitions for each of the following: system DBs, user DBs, logs, and the OS on its own local disk. Tempdb is broken across 8 files (on the same partition) to provide some parallelism. Most other settings are stock.

Load on the server is also rather light – most processing is done during off hours and so when these queries are run they are often the only ones being run, except of course when they aren’t and we have this problem, but even in this case there are usually only two or three queries running simultaneously.

My question is: considering the above, what can I do to allow SQL Server to perform some sort of time slicing so that other queries can run simultaneously with the currently blocking query? I understand that they might suffer some impact as they will be sharing compute cycles, but a half second query to get the TOP 10 rows in a table shouldn’t have to wait 10 minutes for a longer running query to complete.

Let me know if there is some specific information that you may need to provide an answer (assuming, again, that this isn’t already answered under a different set of query terms). I am not looking for query or index optimization advice as again, I have considered such advice already, and many times the queries are ad hoc or generated by a query tool (Ppwer BI) and so I have little or no control over their optimization. I suspect that the solution lies in server configuration, possibly settings or even arrangement of the file structure.

Posted on Categories Articles

## mathematical optimization – it says, N Integrate has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,20}

when I run the code it says,NIntegrate has evaluated to non-numerical values for all sampling points in the
region with boundaries {{0,20}

``````(Chi)(b_) := (Z/k1)*NIntegrate(Subscript(f, pp1)(q)*Subscript(F, pp1)(q)*BesselJ(0, q*b)*q, {q, 0, b}) +
(Nn/K1)*NIntegrate(Subscript(f, pn1)(q)*Subscript(F, pn1)(q)*BesselJ(0, q*b)*q, {q, 0, b})

data = Table({b, (Chi)(b)}, {b, 0, 5.1, 0.1})

``````

Posted on Categories Articles

## Why does zooming into JPEG images show chunky boundaries and or noise resembling smudges of magenta, yellow and cyan?

DPI (dots per inch) in an image determines how smooth or chunky it looks when you zoomin. Yes to reduce the size, jpg uses lossy compression.

Captcha images are low res files on purpose because they need to load quickly and they are not supposed to look particularly fine – they have quite a simple, specific purpose.

Magenta (complement of green), yellow (comp., of blue) and cyan (comp., of red) are primary colors RGB. sRGB is the color gamut used in jpg compression which explains you saw these colors.

Noise and resolution are variables independent of format of an image.

Posted on Categories Articles