How do I test a hit on an equilateral triangle? - language-agnostic

I have an equilateral triangle centered at (0,0). The distance between each vertex and the center is 1. What is the math that will let me test if a given coordinate pair falls inside the triangle? I have found examples to do that with arbitrary triangles, but for this very specific case, it seems to me that it should be fairly straightforward.

If one vertice of triangle has (0,1) coordinate (is it right?), then point (X,Y) falls inside the triangle when three simple conditions are met together:
Y <= 1 + Sqrt(3) * X
Y <= 1 - Sqrt(3) * X
Y >= -1/2
or shorter:
Y <= 1 - Sqrt(3) * Abs(X)
Y >= -1/2

Related

Finding three functions that interpolates between equitation

For a past week I been trying to construct specific functions that would satisfy some the requirements, sadly it was not successful. I decided to give a shot in stack overflow.
I have three shapes in 2D world as shown in the image. Shape A with center a, Shape B with center b and shape C with center c. For this problem shape form does not matter so I will not expand on that. Other important variables are d_ab with represents minimum distance between shape A and B, d_bc - distance between B and C, d_ac - distance between A and C. Also d_ab, d_bc, d_ac ranges from 0 to 1, where 1 represents shapes are touching and 0 their too far from specific threshold.
Here is the image of it
I am trying to find three new functions:
f_a(d_ab, d_bc, d_ac) = x
f_b(d_ab, d_bc, d_ac) = y
f_c(d_ab, d_bc, d_ac) = z
Firstly, these functions with inputs d_ab, d_bc, d_ac in the left side table should result to values x, y, z right side table:
d_ab
d_bc
d_ac
x
y
z
1
0
0
(a+b)/2
(a+b)/2
c
0
1
0
a
(b+c)/2
(b+c)/2
0
0
1
(a+c)/2
b
(a+c)/2
1
1
1
(a+b+c)/3
(a+b+c)/3
(a+b+c)/3
0
0
0
a
b
c
Secondly, all other inputs d_ab, d_bc, d_ac that are not described in table (From 0 to 1) should result in interpolated values between closest ensuring continuity (Continuity I am referring there is that small change in input should not produce big difference in output, there is probably better way to describe this mathematically).
For example:
f_a(0.3, 0, 0) = x
Most likely will combine equitation's from table:
f_a(0, 0, 0) = a
f_a(1, 0, 0) = (a+b)/2
Resulting into:
f_a(0.3, 0, 0) = 0.7 * a + 0.3 * (a+b)/2
I am curious is there a method to find these kind of functions, I assume there could be more than one that satisfies this requirement. But I am fine with any of it as long it has that continuity that I referred above.
I almost sure there is better way to represent this problem mathematically, but I am not that great with it.

MySQL GROUP BY if Multiple Numbered Columns are Close to Each Other (+/- 1)

I have a mysql table with a large list of coordinates (x, y, z). I want to find the most common spots, but when the same place is logged, it isn't identical. For example, x could be 496.0481 or 496.3904, but that is actually the same place.
When I do the following query I get a list of the absolute exact matches, but those are very few and far between:
SELECT x, y, z, COUNT(*) AS coords
FROM coordinates
GROUP BY x, y, z
ORDER BY coords DESC
LIMIT 10;
How can I adjust this to be grouped by each of x, y, and z to be +/- 1 to catch a larger area? I've tried a mix of IF and BETWEEN statements but can't seem to get anything to work.
If I do GROUP BY round(x), round(y), round(z), that gets a larger range but doesn't capture if the number goes from 496 to 497 even if they are just slightly different.
Thanks in advance for the help.
Very naive way:
select t1.x as x1, t1.y as y1, t1.z as z1, t2.x as x2, t2.y as y2, t2.z as z2
from coordinates t1
join coordinates t2 on sqrt(power(t2.x-t1.x, 2) + power(t2.y-t1.y, 2) + power(t2.z-t1.z, 2)) <= 1
For each coordinates (t1) query finds all other coordinates (t2) that dinstanced less or equal than 1 from each other.
But this query has very bad performance: O(n^2)

If xy determines z can x determine z and y determine z?

It is a functional dependency question.
I know that when x->yz then x->y and x->z.But is the above dependency possible?
If xy determines z can x determine z and y determine z?
Yes, if xy -> z then it's possible that also x -> z and y -> z.
Suppose z can only have one value; then a given x, y or xy only ever appears with that one value. Or suppose x -> z and y -> z and x must equal y. Or suppose both x and y are unique; then xy is unique. (A case of that is when both x & y are candidate keys.) In fact any time that x -> z and y -> z, xy -> z.
(To show something is possible it's always worth trying some cases, especially very simple ones, in case they are examples, so you don't have to prove the general case.)

Distance between two points in mysql not working?

EDIT: I have checked the other questions but none use this formula
I have rows with 2 values (x,y). I also have another point that is the center of a circunference with a certain radius. I want to check if the distance between this point and my rows' points coordinates is less that the radius. I am trying to apply the distance between two points formula to calculate the distance between the center point and the rows' point and decide wether the row location is inside the radius of the circle ( < radius):
var query = "SELECT * FROM table WHERE SQRT((POWER("+x+",2)-POWER(location_x,2))+(POWER("+y+",2)-POWER(location_y,2))) < " + radius;
For some reason this is not working and no rows are returned despite changing the rows' points coordinates. Any idea?
Coordinates in the formula might have a negative value. In our test sample they do.
I am pretty sure your Cartesian distance formula is incorrect.
The formula you need is:
sqrt( (x - location_x)^2 + (y - location_y)^2 )
This translates into SQL like so:
SELECT *
FROM table
WHERE SQRT( (POWER("+ x + "-location_x,2))
+(POWER("+ y + "-location_y,2))
) < " + radius

Calculating short distances between lat/long points

I have a MySQL table with Spatial Points, and need to calculate distances. I found lots of material on doing this using the Haversine formula, however all of these assume a large distance between points. In my case, I only care about short distances (< 1 mile) so I don't need to correct for the earth's curvature. My intuition is using the Haversine formula will be inaccurate at such small distances. Any suggestions?
Your intuition is incorrect. Consider the haversine formula, and the definition of haversine, according to Wikipedia (φ is latitude and ψ is longitude):
There is a further fact that is relevant: for small values of θ, sin θ is approximately equal to θ; more relevantly, it is approximately linear in θ. Therefore, haversin θ will be approximately (θ/2)². This approximation gets better as θ approaches zero.
If the latitude and longitude are close together, then φ₂ - φ₁ and ψ₂ - ψ₁, which are what the haversine function is applied to here, will be close to zero, meaning that the formula is approximately
(d/2r)² = ((φ₂ - φ₁) / 2)² + cos(φ₁) cos(φ₂) ((ψ₂ - ψ₁) / 2)²
Now note that this formula has the same form as Euclidean distance in two dimensions with some arbitrary scaling factors (remembering that (kx)² = k² x² so we can move constants in and out of the squares):
k₁ d² = k₂ ∆φ² + k₃ ∆ψ²
Lastly, I assert without proof that those arbitrary scaling factors turn out to be the same ones which convert changes in latitude/longitude to linear distance.
Therefore, the haversine formula does not become inaccurate for small distances; it is precisely the same as an ordinary Euclidean distance calculation, in the limit of small distances.
Create your points using Point values of Geometry datatypes in
MyISAM table
Create a SPATIAL index on these points
Use MBRContains() to find the values:
SELECT *
FROM table
WHERE MBRContains(LineFromText(CONCAT(
'('
, #lon + 10 / ( 111.1 / cos(RADIANS(#lon)))
, ' '
, #lat + 10 / 111.1
, ','
, #lon - 10 / ( 111.1 / cos(RADIANS(#lat)))
, ' '
, #lat - 10 / 111.1
, ')' )
,mypoint)
, or, in MySQL 5.1 and above:
SELECT *
FROM table
WHERE MBRContains
(
LineString
(
Point
(
#lon + 10 / ( 111.1 / COS(RADIANS(#lat))),
#lat + 10 / 111.1
)
Point
(
#lon - 10 / ( 111.1 / COS(RADIANS(#lat))),
#lat - 10 / 111.1
)
),
mypoint
)
This will select all points approximately within the box (#lat +/- 10 km, #lon +/- 10km).
This actually is not a box, but a spherical rectangle: latitude and longitude bound segment of the sphere. This may differ from a plain rectangle on the Franz Joseph Land, but quite close to it on most inhabited places.
Apply additional filtering to select everything inside the circle (not the square)
Possibly apply additional fine filtering to account for the big circle distance (for large distances)
here following solution to click