I have a polygon given in the image , I am using x windows programming work on my requirement.
I am assuming a 2 dimensional geometry.
For each vertex, you have it's co-ordinates (x, y).
You also have the cor-ordinates for the point selected by the user (X, Y).
From co-ordinate geometry, given two points on a line, you can always find an equation representing the line passing through those two points.
Get the equations of the lines passing through the user selected point (X, Y) and the vertices.
i.e If you have a pentagon, 5 vertices with co-ordinates (x1, y1), (x2, y2), (x3, y3), (x4, y4), (x5, y5) and the co-ordinate of the user selected point is (X, Y), find the equations of the lines passing through...
(x1, y1) and (X, Y) --> L1
(x2, y2) and (X, Y) --> L2
(x3, y3) and (X, Y) --> L3
(x4, y4) and (X, Y) --> L4
(x5, y5) and (X, Y) --> L5
Next, Find the equations for the edges in the same manner as stated above..
(x1, y1) and (x2, y2) --> E1
(x2, y2) and (x3, y3) --> E2
(x3, y3) and (x4, y4) --> E3
(x4, y4) and (x5, y5) --> E4
(x5, y5) and (x1, y1) --> E5
Now, take L1 and solve this with E1, E2, E3, E4 and E5. If there is a solution for at least one combination, then do not select the point. Else select it.
Repeat this process for each L and you will have your vertices.
There are tons of links on the internet to find the equation of a line passing through two points.
Related
I need some help with programming the calculation of displacement. Given the distance the object has moved in the XY plane, and the yaw (heading) of the object, I'd like to calculate the displacement of the object. For example, the object moved 5m North and 2m East.
The data I have is the distance it travels in the XY plane (X distance and Y distance) and the heading of the device which is determined by the direction of X. I know how to calculate it on paper but I am having trouble to program it.
I've attached two examples that shows how I obtained the calculations. The black dot is the displacement of the object. In the 1st example, the X-distance travelled is 3 meters while the Y-distance is 4 meters and the heading of the X-axis is 70°. After calculating, I managed to get that it has travelled 2.733m South and 4.187m East. In example 2, it travel, the X-distance is -5m and the Y-distance is -12m, the heading is 160°. I calculated that the object travels 8.80m North and 9.566m East.
Example 1
Example 2
Assume you have your XY pane rotated by some angle Zp and have a 2D movement vector within that pane, let's say its direction rotated by Zv – what is total rotation of the vector, relative to the global orientation? Zp + Zv...
So all you need to do is applying the rotation Zp to the movement vector within XY-pane, i. e. apply a rotation matrix to the vector. This matrix for a rotation by z looks like:
cos(z) -sin(z)
sin(z) cos(z)
With our data above:
north = cos(Zp) * x - sin(Zp) * y
east = sin(Zp) * x + cos(Zp) * y
which gives, with your first sample data applied:
north = cos(70°) * 3 - sin(70°) * 4 ~ -2.7327
east = sin(70°) * 3 + cos(70°) * 4 ~ 4.1872
north = cos(160°) * -5 - sin(160°) * -12 ~ 8.8027
east = sin(160°) * -5 + cos(160°) * -12 ~ 9.5662
Corresponding pretty much to the values you calculated (note: negative movement to north is positive movement towards south...).
What you are doing is a conversion from polar to Cartesian coordinates. https://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates
If your Cartesian coordinates are not aligned to the polar axis, just compensate by adding the rotation angle to the polar argument.
How to get meter of STDistance() or STLength()?
I use
linestring(x y z, x y z, x y z, ...).STLength() //
point(x y).STDistance(point(x y))
but object has 0 of SRID...can I get meter length?
I am trying to let the program draw a hexagon, The program should let the user enter the coordinates of two points only, I will assume those points are the terminals of a side, then I need to calculate the coordinates of other four points, but how?
P.S: I use the library graphics.h that contains draw polygon which requires 2 arrays of x and y coordinates for all points
Given two points (x1, y1), (x2, y2), the next point on the hexagon can be computed with the formulas
dx = x2 - x1
dy = y2 - y1
x3 = x2 + ((√3)/2) dx - (1/2) dy
y3 = y2 + (1/2) dx + ((√3)/2) dy
These are derived from general rotation formulas; note that cos 60° = (√3)/2 and sin 60° = 1/2.
I've got a question about forming the loop function for processing the earthquake data per bin.
Our map is as below, with the data, and runs from -125W to -114W, and 32N to 42N.
In order to process the data quickly and efficiently, we should use a loop. However, I'm not sure how to go about starting it!
We would like to sample a 1x1 degree bin, roughly as below...
Starting in the top left corner, sample 41 to 42N and -125 to -124W
Run calculation on sample area
Move 1 degree East and run calculation
repeat until at rightmost edge
return to leftmost edge and move down a degree.
Could anyone advise on how to do this? I'm not sure where to start, so if someone can outline a framework from which I can adapt... :)
Once we have worked out how to do it for a 1 degree bin, we can improve resolution and run our calculations :)
The code below nests the loops as required
printf "" >! loop1.txt
# x = -125
while ($x <= -114)
# y = 32
while ($y <= 42)
printf "$x $y\n" >> loop1.txt
# y++
end
# x++
end2
and produces an output of
x1 y1
x1 y2
x1 y3
x2 y1
x2 y2
x2 y3
etc.
Required now, however, is to integrate an awk command during the second loop, to create a third column output
I want to find a way to check whether two line segments intersects or not.I am using Xlib programming to implement this.
I checked on internet but i only found the ways to find the intersection point of two lines but not of two line segments.
How can i implement this using X lib programming?
You don't need Xlib for this. Let the two segments be
A1 = (x1, y1) -> B1 = (x1 + dx1, y1 + dy1) and
A2 = (x2, y2) -> B2 = (x2 + dx2, y2 + dy2).
Let
vp = dx1 * dy2 - dx2 * dy1
If vp == 0 the segments are parallel and there is no intersection.
Otherwise, let v = (vx, vy) be the vector between A1 and A2
vx = x2 - x1
vy = y2 - y1
Compute
k1 = (vx * dy2 - vy * dx2) / vp
k2 = (vx * dy1 - vy * dx1) / vp
If either k1 or k2 fall outside the [0, 1] interval, the segments don't intersect (but the underlying lines do intersect). Otherwise, the intersection is at
(x1 + k1 * dx1, y1 + k1 * dy1)
which incidentally, if you wonder about symmetry, will be the same point as
(x2 + k2 * dx2, y2 + k2 * dy2)
These formulas are basically similar to the answer on How do you detect where two line segments intersect? except coding from there would not necessarily be trivial for either a newbie or someone in a rush (like I have been myself many times).