This is the query for a gaming application to get a list of targets for the enemy that excludes locations that the enemy can't see. This is a simplified version of my query to target my specific question.
SELECT * FROM `game_moblist` WHERE (posx!=0 AND posy!=0) AND (posx!=1100 AND posy!=220)
posx is the x coordinate posy is the y coordinate
I'm writing a loop to exclude any tiles that cannot be seen
The issue I see is that its treated as if the parenthesis aren't there. All posx=1100 are excluded and not the ordered pair (1100,220) what is the proper syntax for what I'm trying to do? The only solution I thought of is to combine the two numbers into a unique single number but I'd rather learn something new.
I think you mean:
WHERE NOT (posx=0 AND posy=0)
AND NOT (posx=1100 AND posy=220)
which can be rewritten also as:
WHERE (posx, posy) NOT IN ((0, 0), (1100, 200))
Related
My question is related to plotting amplitude spectrum.
Problem 1: (I have solved it) I have to represent the following function as a discrete set of N=100 numbers separated by time increment of 1/N:
e(t) = 3sin2.1t + 2sin1.9t
I did it using stem function in matlab and plotted it.
Problem 2: (I have question about it) The next thing was to repeat the same above all, using dataset of 200 points with time increment of 1/N and 1/2N.
My question is a bit basic but I just want to clear if I am following the right path to solve my problem.
I want to ask that for problem 2, for both 1/N and 1/2N, should I use N=200 (as I believe it is separate problem)?
A few of my mates have suggested using N=100 for 1/N and N=200 for 1/2N.
which one is the right thing?
Any help will be highly appreciated. Thanks
I use mysql spatial functions.
I have to understand if points lies withing multipolygon.
Initially I used MBRContains but it works in a strange way, so I faced the following error: Mysql function MBRContains is not accurate
My next step was switching to the functions ST_Contains. But I found out that if I use polygon(from multipolygon) vertex as argument - function returns false but I want to unclude all multipolygon borders.
P.S.
I found that where are function:
ST_Touches(g1, g2)
Two geometries spatially touch if their interiors do not intersect,
but the boundary of one of the geometries intersects either the
boundary or the interior of the other
Looks like it works like I want(in OR conditions with ST_contains) but documentation is not clear for me. Can you explain how can 2 conditions be truth together
1. Interiors do not intersects
2. Boundary intersects the interrior.
?
Question:
How can I achieve the behaviour I want?
This looks like a working solution:
ST_Contains(g1,g2) || ST_Touches(g1, g2)
Looks like ST_Distance(AREA, #point)) = 0 includes border
In my database I have polygons stored. Now I need to search all the polygons that are within a certain radius.
Even if the polygon is only a small part inside the region, then it should be included inside the results (so once there is a minimal match, there is a match).
What is the best way to do this? I have been thinking about creating another polygon and search everything that intersects this, but don't know if this is a valid method?
Yes, I think it is the best approach. You can create a polygon using ST_BUFFER and then you can use ST_INTERSECT to find if polygons will intersect your polygon.
May be you can also do it using ST_DISTANCE. It will calculate minimum distance of a point from polygon.
Select ST_DISTANCE(polygons,POINT(x, y)) as distance, polygon_id from your_polygon_table WHERE distance <= 10
I struggled with the same issue and came up with the following that works well.
select this.poly, other.poly
from table this, table other
where this.name = 'name of subject polygon'
and ST_Distance(ST_Centroid(this.polygon),ST_Centroid(other.polygon)) < 'desired distance';
This will work with the initial point is in the same table as the other polygons. If you have a fixed initial point you can simplify the query as follows:
select poly from table
where ST_Distance('26.36, -81.07',ST_Centroid(other.polygon)) < 'desired distance';
My query is
SELECT A.broker_property_id, A.broker_owner_id
FROM property_requirement_new AS A,
(SELECT X(latlong), Y(latlong)
FROM client_property_new
WHERE property_id = 132) AS B
WHERE (POW((A.X(latlong)-B.X(latlong))*111.12, 2) + POW((A.Y(latlong) - B.Y(latlong))*111.12, 2)) <= 4
Here, latlong is a field of mysql's POINT datatype.
But this query is not being executed and is showing the following error:
FUNCTION a.X does not exist
Can anyone help me out with the correct method to do it or to spot the error in my code?
X and Y are functions that take a Point (geometry) as an input. It is not a function of table A, as you have written it, ie, A.X(latlong) is throwing an error as it implies that X is a function of table A.
You would need to write
select X(latlon), Y(latlon) from A
or in your case, if you are using subqueries:
select X(A.latlon), Y(A.latlong) from (.....) A
There is apparently an ST_Distance function in MySQL 5.6, but I have never used it and it seems somewhat undocumented.
There is some discussion of distance functions in MySQL here: Fastest Way to Find Distance Between Two Lat/Long Points One of the general problems of spatial in MySQL is that it does not support projections properly, so you are either left doing Pythagoras on planar coordinates, or implementing some version of the haversine formula and assuming the world is a sphere -- which will work well for short distances if massive precision is not your biggest concern.
The problem is as follows,
I would be given a set of x and y coordinates(an coordinate array of around 30 to 40 thousand) of a long rope. The rope is lying on the ground and can be in any shape.
Now I would be given a start point(essentially x and y coordinate) and an ending point.
What is the efficient way to determine the set of x and y coordinates from the above mentioned coordinate array lie between the start and end points.
Exhaustive searching ie looping 40k times is not an acceptable solution (mentioned on the question paper)
A little bit margin for error is acceptable
We need to find the start point in the array, then the end point. For each, we can think of the rope as describing a function of distance from that point, and we're looking for the lowest point on that distance graph. If one point is a long way away and another is pretty close, we can do some kind of interpolation guess of where to search next.
distance
| /---\
|-- \ /\ -
| -- ------- -- ------ ---------- -
| \ / \---/ \--/
+-----------------------X--------------------------- array index
In the representation above, we want to find "X"... we look at the distances at a few points, get an impression of the slope of the distance curve, possibly even the rate of change of that slope, to help guide our next bit of probing....
To refine the basic approach of doing binary- or interpolated- searches in areas where we know the distance values are low, we may be able to use the following:
if we happen to be given the rope length and know the coordinate samples are equidistant along the rope, then we can calculate a maximum change in distance from our target point per sample.
if we know the rope has a stiffness ensuring it can't loop in a trivially small diameter, then
there's a known limit to how fast the slope of the curve can change
distance curve converges to vertical on both sides of the 0 point
you could potentially cross-reference/combine distance with, or use instead, the direction of each point from the target: only at the target would the direction instantly change ~180 degrees (how well the data points capture this still depends on the distance between adjacent samples and any stiffness of the rope).
Otherwise, there's always risk the target point may weirdly be encased by two very distance points, frustrating our whole searching algorithm (that must be what they mean about some margin for error - every now and then this search would have to revert to a O(N) brute-force search because any trend analysis fails).
For a one-time search, sometimes linear traversal is the simplest, fastest solution. Maybe that's the case for this problem.
Iterate through the ordered list of points until finding the start or end, and then collect points until hitting the other endpoint.
Now, if we expected to repeat the search, we could build an index to the points.
Edit: This presumes no additional constraints beyond those mentioned by #koool. Constraining the distance between the points would allow the hill-climbing approach described in #Tony's answer.
I don't think you can solve it accurately using anything other than exhaustive search. Say for cases where the rope is folded into half and the resulting double rope forms a spiral with the two ends on the centre.
However if we assume that long portions of the rope are in straight line, then we can eliminate a lot of points based on the slope check:
if (abs(slope(x[i],y[i],x[i+1],y[i+1])
-slope(x[i+1],y[i+1],x[i+2],y[i+2]))<tolerance)
eliminate (x[i+1],y[i+1]);
This will reduce the search time significantly if large portions of the rope are in straight line. But will be linear WRT number of remaining points.
So basically, you've got a sorted list of the points that comprise the entire rope and you're given two arbitrary points from within that list, and tasked with returning the sublist that exists between those two points.
I'm going to make the assumption that the start and end points that are provided are guaranteed to coincide exactly with points within the sorted list (otherwise it introduces a host of issues, particularly if the rope may be arbitrarily thin and passes by the start/end points multiple times).
That means all you're really looking for are the indices of the two provided coordinates. Or the index of one, and the answer to "is the second coordinate to the right or to the left?".
A simple O(n) solution to that would be:
For each index in array
coord = array[index]
if (coord == point1)
startIndex = index
if (coord == point2)
endIndex = index
if (endIndex < startIndex)
swap(startIndex, endIndex)
return array.sublist(startIndex, endIndex)
Or, if you wanted to optimize for repeated queries, I'd suggest a hashing based approach where you map each cooordinate to its index in the array. Something like:
//build the map (do this once, at init)
map = {}
For each index in array
coord = array[index]
map[coord] = index
//find a sublist (do this for each set of start/end points)
startIndex = map[point1]
endIndex = map[point2]
if (endIndex < startIndex)
swap(startIndex, endIndex)
return array.sublist(startIndex, endIndex)
That's O(n) to build the map, but once it's built you can determine the sublist between any two points in O(1). Assuming an efficient hashmap, of course.
Note that if my assumption doesn't hold, then the same solutions are still usable, provided that as a first step you take the provided start and end points and locate the points in the array that best correspond to each one. As noted, unless you are given some constraints regarding the thickness of the rope then interpolating from an arbitrary coordinate to one that's actually part of the rope can only be guesswork at best.