Figuring out what cell in a slanted grid a point is in - math

I have the following (simplified) situation in my game:
A slanted (sheared?) grid and a point somewhere in that grid. I have to figure out in which cell that point lies. Sorry if this is a noob question but I would really appreciate some help.
We can see, visually, that the red point lies in (1,1) but how do I figure that out via code?
Of course if this was a standard grid, I would just do (x/100, y/250)

You can define two base vectors - in this case horizontal vector A=(100, 0) and left-down vector like B=(-20, 150) are good. Base point (0,0).
Now for point P express it's coordinates in components of vectors A, B:
u * ax + v * bx = px
u * ay + v * by = py
Solve this equation system for unknowns coefficients u, v. Integer parts of result coefficients correspond to cell row and column.
For given example u,v values should be about 2.2 and 1.8, so 1st row (counting from 0) and 2nd column

Related

Calculate the coordinate of the small center hexagon of a group of hexagon in a grid

I am stuck on a problem that seems easy to solve but I can't seem to pinpoint the right formula.
I have a list of hexagon groups in a cube coordinate system. I know the cube coordinates of the groups but I need to calculate the "global" coordinate of a small hexagon in a given group.
For example, in the image below, I know the coordinates for GroupA (x=0, y=0, z=0) and GroupB (x=-1, y=1, z=0). How can I calculate the coordinates of the center tile of GroupB given that each group has the same radius (in this case the radius is 1) and they don't overlap each other (let's see it as a tiling of groups starting from 0,0,0 that creates a hex grid)?
In this simple example, I know as a human being that the center tile of GroupB is (x=-1, y=3, z=-2) but I need to code that logic in a way that a computer can calculate it for any given group on the map. I don't particularly need help on the code itself but the overall logic.
In this article, the author does the opposite (going from small hexagon and trying to find its group):
https://observablehq.com/#sanderevers/hexagon-tiling-of-an-hexagonal-grid
Any help would be greatly appreciated!
Thanks!
It looks like I have found something that seems to work.
Please feel free to correct me if I'm mistaken.
Based on the article I linked in my original question, I came up with an algorithm that calculates the small hexagon central coordinates based on its higher group coordinates (in this case, I've used a group with a radius of 10). I took the original algorithm and removed the area division the author did. The code is in javascript. The i, j and k variables are the cube coordinates of the group. The function returns the cube coordinates of the central small hex :
getGroupCentralTileCoordinates(i, j, k)
{
let r = 10;
let shift = 3 * r + 2;
let xh = shift * i + j;
let yh = shift * j + k;
let zh = shift * k + i;
return {
'x': (1 + xh - yh) / 3,
'y': (1 + yh - zh) / 3,
'z': (1 + zh - xh) / 3
};
}

Calculate the 3rd point of an equilateral triangle from two points at any angle, pointing the "correct" way for a Koch Snowflake

Perhaps the question title needs some work.
For context this is for the purpose of a Koch Snowflake (using C-like math syntax in a formula node in LabVIEW), thus why the triangle must be the correct way. (As given 2 points an equilateral triangle may be in one of two directions.)
To briefly go over the algorithm: I have an array of 4 predefined coordinates initially forming a triangle, the first "generation" of the fractal. To generate the next iteration, one must for each line (pair of coordinates) get the 1/3rd and 2/3rd midpoints to be the base of a new triangle on that face, and then calculate the position of the 3rd point of the new triangle (the subject of this question). Do this for all current sides, concatenating the resulting arrays into a new array that forms the next generation of the snowflake.
The array of coordinates is in a clockwise order, e.g. each vertex travelling clockwise around the shape corresponds to the next item in the array, something like this for the 2nd generation:
This means that when going to add a triangle to a face, e.g. between, in that image, the vertices labelled 0 and 1, you first get the midpoints which I'll call "c" and "d", you can just rotate "d" anti-clockwise around "c" by 60 degrees to find where the new triangle top point will be (labelled e).
I believe this should hold (e.g. 60 degrees anticlockwise rotating the later point around the earlier) for anywhere around the snowflake, however currently my maths only seems to work in the case where the initial triangle has a vertical side: [(0,0), (0,1)]. Else wise the triangle goes off in some other direction.
I believe I have correctly constructed my loops such that the triangle generating VI (virtual instrument, effectively a "function" in written languages) will work on each line segment sequentially, but my actual calculation isn't working and I am at a loss as to how to get it in the right direction. Below is my current maths for calculating the triangle points from a single line segment, where a and b are the original vertices of the segment, c and d form new triangle base that are in-line with the original line, and e is the part that sticks out. I don't want to call it "top" as for a triangle formed from a segment going from upper-right to lower-left, the "top" will stick down.
cx = ax + (bx - ax)/3;
dx = ax + 2*(bx - ax)/3;
cy = ay + (by - ay)/3;
dy = ay + 2*(by - ay)/3;
dX = dx - cx;
dY = dy - cy;
ex = (cos(1.0471975512) * dX + sin(1.0471975512) * dY) + cx;
ey = (sin(1.0471975512) * dX + cos(1.0471975512) * dY) + cy;
note 1.0471975512 is just 60 degrees in radians.
Currently for generation 2 it makes this: (note the seemingly separated triangle to the left is formed by the 2 triangles on the top and bottom having their e vertices meet in the middle and is not actually an independent triangle.)
I suspect the necessity for having slightly different equations depending on weather ax or bx is larger etc, perhaps something to do with how the periodicity of sin/cos may need to be accounted for (something about quadrants in spherical coordinates?), as it looks like the misplaced triangles are at 60 degrees, just that the angle is between the wrong lines. However this is a guess and I'm just not able to imagine how to do this programmatically let alone on paper.
Thankfully the maths formula node allows for if and else statements which would allow for this to be implemented if it's the case but as said I am not awfully familiar with adjusting for what I'll naively call the "quadrants thing", and am unsure how to know which quadrant one is in for each case.
This was a long and rambling question which inevitably tempts nonsense so if you've any clarifying questions please comment and I'll try to fix anything/everything.
Answering my own question thanks to #JohanC, Unsurprisingly this was a case of making many tiny adjustments and giving up just before getting it right.
The correct formula was this:
ex = (cos(1.0471975512) * dX + sin(1.0471975512) * dY) + cx;
ey = (-sin(1.0471975512) * dX + cos(1.0471975512) * dY) + cy;
just adding a minus to the second sine function. Note that if one were travelling anticlockwise then one would want to rotate points clockwise, so you instead have the 1st sine function negated and the second one positive.

Orientation of the Vector

I have a line and a few points and I need to determine which points are under and which are beyond the line. I tried to find a line that is in 90degrees angle with my line and crosses the points but i couldnt figure out whether the vectors orientation is up or down. Can you help? Thank you
You can find line equation and substitute points in thin equation.
Easy case: Let's line is not vertical, so it might be described by equation
y = a * x + b
for every query point (px, py) calculate value
S = py - a * px - b
When S is positive, point is above the line, when negative - below.
If your line is defined by base point B and direction vector D, you can determine - what semi-plane (against the line) query point P belongs to - using cross product sign
Sign (D x (P-B))
Note that in this case term "below" depends also on sign of X-component of vector D

How to find points on the circumference of a arc knowing a start point, an end point and the radius?

Please see the image below for a visual clue to my problem:
I have the coordinates for points 1 and 2. They were derived by a formula that uses the other information available (see question: How to calculate a point on a circle knowing the radius and center point).
What I need to do now (separately from the track construction) is plot the points in green between point 1 and 2.
What is the best way of doing so? My Maths skills are not the best I have to admit and I'm sure there's a really simple formula I just can't work out (from my research) which to use or how to implement.
In the notation of my answer to your linked question (i.e. x,y is the current location, fx,fy is the current 'forward vector', and lx,ly is the current 'left vector')
for (i=0; i<=10; i++)
{
sub_angle=(i/10)*deg2rad(22.5);
xi=x+285.206*(sin(sub_angle)*fx + (1-cos(sub_angle))*(-lx))
yi=y+285.206*(sin(sub_angle)*fy + (1-cos(sub_angle))*(-ly))
// now plot green point at (xi, yi)
}
would generate eleven green points equally spaced along the arc.
The equation of a circle with center (h,k) and radius r is
(x - h)² + (y - k)² = r² if that helps
check out this link for points http://www.analyzemath.com/Calculators/CircleInterCalc.html
The parametric equation for a circle is
x = cx + r * cos(a)
y = cy + r * sin(a)
Where r is the radius, cx,cy the origin, and a the angle from 0..2PI radians or 0..360 degrees.

Find Circum Center of Three point of Triangle [Not using Compass]

I am trying to find circumcenter of Given Three point of Triangle……..
NOTE: all these three points are with X,Y and Z Co-Ordinate Means points are in 3D
I know that the circumcenter is the point where the right bisectors intersect….
But for that I have to find middle point of each side then the right bisectors and then intersection point of that …..this is long and error some process……
Is there not any formula which just takes as input these three points of triangle and giving us the Circumcenter of Triangle ……?
Thanks………
The wiki page on Circumscribed circle has it in terms of dot and cross products of the three vertex vectors. It also has a formula for the radius of the circle, if you are so interested.
First of all, you need to make sure that points are not collinear. i.e. do not lie in the same line. For that you need to find the direction cosines of the lines made by three points, and if they have same direction cosines, halt, you can't get circle out of it.
For direction cosine please check this article on wikipedia.
(A way of finding coordinate-geometry and geometry -- based on the theorem that, a perpendicular line from center of circle bisects a chord)
Find the equation of the plane.
This equation must reduce to the form
and the direction cosines (of the line perpendicular to plane determines the plane), so direction cosines of the line perpendicular to this line is
given by this link equations -- 8,9,10 (except replace it for l, m, n).
Find the equation of the lines (all three) in 3-d
(x-x1)/l=(y-y1)/m=(z-z1)/n (in terms of direction cosines) or
(x-x1)/(x2-x1)=(y-y1)/(y2-y1)=(z-z1)/(z2-z1)
Now we need to find the equation of line
a) this perpendicular to the line, from 2 (let l1, m1, n1 be direction cosines of this line)
b) must be contained in place from 1 (let l2, m2, n2 be direction cosines of this line perpendicular to plane)
Find and solve (at least two lines) from 3, sure you will be able to find the center of the circle.
How to find out equation ??? as we are finding the circum-center, we will get our points (i.e. it is the midpoint of the two points) and for a) we have
l1*l+m1*m+n1*n = 0 and l2*l+m2*m+n2*n = 0 where l, m, n are direction cosines of our, line, now solving this two equation, we can get l, m interms of n. And we use this found out (x1,y1,z1) and the value of l, m, 1 and we will have out equation.
The other process is to solve the equation given in this equation
https://stackoverflow.com/questions/5725871/solving-the-multiple-math-equations
Which is the deadliest way.
The other method is using the advantage of computer(by iteration) - as I call it (but for this you need to know the range of the coordinates and it consumes lot of memory)
it's like this (You can make it more precise by incrementing at 1/10) but certainly bad way.
for(i=minXrange, i>=maxXrange; i++){
for(j=minYrange, j>=maxYrange; j++){
for(i=minZrange, i>=maxZrange; k++){
if(((x1-i)^2 + (y1-j)^2 + (z1-k)^2) == (x2-i)^2 + (y2-j)^2 + (z2-k)^2) == for z)){
return [i, j, k];
}
}
}
}

Resources