Plot the Minkowski distance with different p - r

As we know, when we calculate the Minkowski distance, we can get different distance value with different p (The power of the Minkowski distance).
For example, when p=1, the points whose Minkowski distance equal to 1 from (0, 0) combine a square. In R, dist() function can get the distance.
My question is with different p, I want to plot the distance with different p to get graphs like below.

Related

How to simulate distances from a fixed point to random points within a given radius in R?

I am trying to simulate N distances between a fixed point and other points randomly distributed around it within a given radius.
One way I've thought of is to simulate coordinates for the random points, then calculate the distances, then exclude distances greater than the given radius (say r = 250m):
X <- runif(N, -250, 250) # simulate random X coordinate
Y <- runif(N, -250, 250) # simulate random Y coordinate
distance <- sqrt(X^2 + Y^2) # calculate distance from random points to center
distance <- distance[distance < 250] # only include values within given radius
However, I am wondering if there is a way to simulate these distances without simulating the coordinates themselves. My end goal is to be able to do this in JAGS so solutions that work in JAGS are preferred. Is there a probability distribution that could be used to describe the probability of these distances to random points? An ideal solution would look something like this:
distance ~ pDistribution(N, 250)
or alternatively in JAGS:
for (1 in 1:N) {
distance[i] ~ pDistribution(250)
}
#jlhoward had a good idea with thinking in polar coordinates - that's what got me going in the right direction. However, by using r = runif(250), you would end up with points clustered around the center. To have a uniformly random distribution of points throughout the circle, there must be more points at greater distances from the center (because circumference/area increase with radius). Turns out you can do this with r <- 250 * sqrt(runif(N, 0, 1)). For my problem, all I needed was to generate these distances (i.e., radii), not the actual points, so this code is an adequate solution. This great video on finding random points in a circle is what helped me finally figure it out.

Average height of a point based on nearby points

I have a situation in my game. I am experimenting with terrain generation.
I have a bunch of peaks, whose position and elevation i know.
I have a point which is surrounded by all these peaks. I know its position. I am trying to calculate the elevation of this point.
I would like to calculate the height of this point, based on how close/far it is to each of these peaks, and the elevation of each of these peaks.
Example:
Peak 1 is at (0,0), with an elevation of 500
Peak 2 is at (100,100), with an elevation of 1000
Peak 3 is at (0,100), with an elevation of 750
If my point is at (99,99), i want the elevation of this point to be as close to 1000.
What is the name of this problem?
If you already have a solution to this, that too will be much appreciated.
Note: In addition, it will be helpful if the formula/equation also allows me to generate negative elevations. for example, a point midway between all the peaks could as well be under sea level. Any formula i can menatally think of usually gives me just positive results. I assume some kind of 'Slope' must be considered to allow this.
One equation i though of so far is
P1.height * (Sum of all distances - distance from P1)/(Sum of all distances) +
P2.height * (Sum of all distances - distance from P2)/(Sum of all distances) +
... Pn.height * (Sum of all distances - distance from Pn)/(Sum of all distances)
Thank you.
To draw the peaks your game needs to convert the coordinates of the peaks to screen coordinates.
Such calculation is usually done by multiplying a matrix with the vector containing the coordinates (in java AWT such matrix would be called a transform).
What you need is the inverse of that matrix so that you can apply it to your screen coordinates.
So the solution is:
get the matrix that is used for rendering the terrain
calculate the inverse matrix
apply it to your screen coordinates
And it might even be more efficient not to use the original matrix to calculate the inverse matrix but use the parameters (zero point, scale factors and rotation angle) which were used to calculate the original matrix. The same parameters can be used to calculate the inverse matrix.

R: how to set p for minkowski distance in pheatmap?

In R, there is a function to generate heatmap with dendrogram, called pheatmap::pheatmap. minkowski is one of the distance measuring options for pheatmap to draw dendrogram. However, how to set the p (The power of the Minkowski distance) in function pheatmap?
Thanks.
The Minkowski distance has nothing to do with the pheatmap package. It is part of the dist function in the stats package. If you try ?dist, you'll see that dist has a p parameter specifically for Minkowski method of taking distance:
dist(x, method = "minkowski", p = 2)

Determine if points are closely coplanar

I have 4 3d points (x,y,z), and I want to know if those points are close to be coplanar. I constructed 3 vectors AB, AC and AD and calculated the absolute value of the determinant which is here the same as the volume. I know that if the volume is 0 then the points are coplanar, but I want also to know if those points are closely coplanar ( I may choose a threshold for instance).
Any help will be appreciated,
Use some normalization of the volume (determinant).
For example, divide it by some function of tetrahedron facets' area (I chose arbitrary one to keep dimension)
Vnorm = Abs (V) / (S1 + S2 + S3 + S4)3/2
Another approach: divide squared distance from D vertice to ABC plane by ABC area (or distance by ABC perimeter)
You can compute the cross-product of vectors AB and AC, obtaining a N1 vector, normal to (ABC) plane. In the same way, compute the cross-product of vectors AB and AD obtaining a N2 vector, normal to (ABD) plane.
The scalar product N1.N2 = |N1|.|N2|.cos(X) where X is the angle between the two normal vectors. X should be zero if your points are exactly coplanar. You can compute X with Arccos function. Unities for X are radians. If X is lower than pi/180 for example you have an angle lower than 1 degree, so points nearly coplanar. You have to decide the exact desired threshold for this angle.

Calculate distance between two x/y coordinates?

I would like to calculate the distance between two x/y coordinates on the surface of a torus. So, this is a normal grid that has the property that its corners and sides are 'connected'. For example, on a grid of 500x500, the point at (499, 499) is adjacent to (0, 0) and the distance between e.g. (0,0) and (0,495) should then be 5.
Is there any good mathematical way of calculating this?
So you are looking for the Euclidean distance on the two-dimensional surface of a torus, I gather.
sqrt(min(|x1 - x2|, w - |x1 - x2|)^2 + min(|y1 - y2|, h - |y1 - y2|)^2)
where w and h are the width (x) and height (y) of the grid, respectively.
If/while the distance between x coordinates is larger than half of the grid X size, add grid X size to the smaller x coordinate.
Do the same for Y.
Then calculate the distance.
If your grid wraps around at the edges, there will be four distances between each coordinate (for 2 dimensions). I'm assuming you want to know the shortest distance.
Let's use a smaller grid, the numbers are a bit more manageable. Say the grid is 10x10. Let's also use just one dimension for simplicity (in which case there'll be just two distances), just as you have in your example. Say we have the points 0,2 and 0,6. The two distances between the points are d_1 = (6-2) = 4 and d_2 = (10-6) + 2 = 6, so in this case the shortest distance would be d_1.
In general, you can do the following:
For each coordinate:
subtract the smaller from the larger number
if the result is greater than half the width of the grid the shortest distance in this coordinate is the grid width minus the result
if the result is less than half the width of the grid, the shortest distance in this coordinate is the result
Then using Pythagoras' theorem, the shortest distance between the two points is the square root of the sum of the squares of the shortest distances in each direction. You can calculate the other three distances by calculating Pythagoras' theorem using the other combinations of distances in each direction.
As another poster has said, the shape formed when you wrap round at the edges (for a 2 dimensional grid) is a torus and I think the method I've used above is the same as the equation given but has the advantage that it can be extended to n-dimensions if required. Unfortunately there's not really an easy visualisation above 2 dimensions.
for points (x1,y1) and (x2,y2), you need to calculate 4 distances:
from (x1,y1) to (x2,y2)
from (x1,y1) to (x2, 500-y2)
from (x1,y1) to (500-x2, y2)
from (x1,y1) to (500-x2, 500-y2)
and then take the minimum of those.

Resources