Expand polygon envelop to square using decimal degrees(wgs 84) - math

I working in wgs64 gis.
I have the coordinates of polygon given by decimal degrees.
I trying to expand the polygon envelop from a rectangle to a square.
I understand that a rectangle of one degree of latitude and longitude on the sphere has not the same length in km unless it is situated on the equator.
But I don't understand the math behind how to fix my envelop.
I am doing this:
Envelope contextEnvelope = CurrElement.Envelope;
double Max = Math.Max(contextEnvelope.Width, contextEnvelope.Height);
contextEnvelope.Expand((Max * 15 - contextEnvelope.Width) / 2, (Max * 15 - contextEnvelope.Height) / 2);

Related

Translating Screen Coordinates [ x, y ] to Camera Pan and Tilt angles

I have a IP Camera which can PTZ. I am currently streaming live feed into the browser and want to allow user to click a point on the screen and the camera will pan and tilt so that the user clicked position will now become the center point of view.
my Camera Pan 360 degrees and Tilt from -55 to 90.
any algorithm that will guide to me achieve my goal ??
Let's start by declaring a 3D coordinate system around the camera (the origin). I will use the following: The z-axis points upwards. The x-axis is the camera direction with pan=tilt=0 and positive pan angles will move the camera towards the positive y-axis.
Then, the transform for a given pan/tilt configuration is:
T = Ry(-tilt) * Rz(pan)
This is the transform that positions our virtual image plane in 3D space. Let's keep that in mind and go to the image plane.
If we know the vertical and horizontal field of view and assume that lens distortions are already corrected, we can set up our image plane as follows: The image plane is 1 unit away from the camera (just by declaration) in the view direction. Let the center be the plane's local origin. Then, its horizontal extents are +- tan(fovx / 2) and its vertical extents are +- tan(fovy / 2).
Now, given a pixel position (x, y) in this image (origin in the top left corner), we first need to convert this location into a 3D direction. We start by calculating the local coordinates in the image plane. This is for the image's pixel width w and pixel height h:
lx = (2 * x / w - 1) * tan(fovx / 2)
ly = (-2 * y / h + 1) * tan(fovy / 2) (local y-axis points upwards)
lz = 1 (image plane is 1 unit away)
This is the ray that contains the according pixel under the assumption that there is no pan or tilt yet. But now it is time to get rid of this assumption. That's where our initial transform comes into play. We just need to transform this ray:
tx = cos(pan) * cos(tilt) * lx - cos(tilt) * sin(pan) * ly - sin(tilt) * lz
ty = sin(pan) * lx + cos(pan) * ly
tz = cos(pan) * sin(tilt) * lx - sin(pan) * sin(tilt) * ly + cos(tilt) * lz
The resulting direction now describes the ray that contains the specified pixel in the global coordinate system that we set up in the beginning. All that's left is calculate the new pan/tilt parameters:
tilt = atan2(tz, tx)
pan = asin(ty / sqrt(tx^2 + ty^2 + tz^2))

how map 2d grid points (x,y) onto sphere as 3d points (x,y,z)

I have a set of 2d grid points (x,y) that I want to map/project onto a sphere as 3d points (x,y,z).
I realize there will be some warping towards the poles as abs(y) increases but my grid patch will only cover a portion of the sphere near the equator so severe warping will be avoided.
I'm having trouble finding the right equations for that.
Paraphrased from the wikipedia article on Mercator projection:
Given a "mapping sphere" of radius R,
the Mercator projection (x,y) of a given latitude and longitude is:
x = R * longitude
y = R * log( tan( (latitude + pi/2)/2 ) )
and the inverse mapping of a given map location (x,y) is:
longitude = x / R
latitude = 2 * atan(exp(y/R)) - pi/2
To get the 3D coordinates from the result of the inverse mapping:
Given longitude and latitude on a sphere of radius S,
the 3D coordinates P = (P.x, P.y, P.z) are:
P.x = S * cos(latitude) * cos(longitude)
P.y = S * cos(latitude) * sin(longitude)
P.z = S * sin(latitude)
(Note that the "map radius" and the "3D radius" will almost certainly have different values, so I have used different variable names.)
I suppose that your (x,y) on the sphere are latitude, longitude.
If so, see http://tutorial.math.lamar.edu/Classes/CalcII/SphericalCoords.aspx.
There:
phi = 90 degree - latitude
theta = longitude
rho = radius of your sphere.
I would expect that you could use the inverse of any of a number of globe projections.
Mercator is pretty good around the equator compared to other projections.
Formulas are on the wiki page.
http://en.wikipedia.org/wiki/Mercator_projection

Draw a rectangle arround a polygon when given in certain angle

Using Google Maps V3 API
How can I draw a rectangle (It wouldn't exactly be a google map rectangle anymore but a rectangular polygon) around a polygon. Important however is that the rectangle has be rotated around a certain given angle. I do not mean to calculate the min and max of the coord of the polygon first to draw a rectangle and rotate it afterwards;
This image may be a better description from what I am trying to accomplish.
What I am trying to accomplish is to get the bounding rectangle of the polygon at an angle.
This is very similar to finding a bounding box, which is not rotated.
If your angle of rotation is alpha, then take the unit vectors along Ox and Oy and rotate them by this angle alpha to get unit vectors X and Y.
Now you are looking for the bounding box with respect to X,Y.
For each point (represented by a vector) P in your set you can get the oriented projection by taking the dot product: Px = P.X and Py = P.Y
If
Mx is the max Px, mx is the min Px.
My is the max Py, my is the min Py.
then this is your bounding rectangle.
I am not sure what representation of it you need, but say its vertices would be:
mx * X + my * Y
mx * X + My * Y
Mx * X + my * Y
Mx * X + My * Y

polygon coordinates

how can we draw a polygon. when only the sides and radius is given.
I have to make a pop up box which will take as input the radius and number of sides and will draw a ploygon. just need the formula.
Imagine a circle of radius r. It is like a regular polygon with an infinite number of sides.
Trigonometry tells us:
x = r * cos(a);
y = r * sin(a);
We know there are 360 degrees or 2pi radians in a circle. So to draw it we would start with angle = 0, calculate that co-ord, step to the next angle and calculate that point, then draw a line between the two.
There are only so many points we can calculate around the edge of the circle, eventually it won't make any difference. If the circle is small enough, even 8 sides will look round.
To draw an 8 sided circle we want 8 points evenly spaced around the circle. Divide the circle into 8 angles, each one is 2 * pi / 8 radians.
So:
angle = 0.0;
step = 2 * pi / 8;
for ( n = 0; n < 8; n++ ) {
x = radius * cos(angle);
y = radius * sin(angle);
angle += step;
}
Now you can draw an octagon, change it to draw the general case.

finding a dot on a circle by degree?

Let's say we have a 100x100 coordinate system, like the one below. 0,0 is its left-top corner, 50,50 is its center point, 100,100 is its bottom right corner, etc.
Now we need to draw a line from the center outwards. We know the angle of the line, but need to calculate the coordinates of its end point. What do you think would be the best way to do it?
For example, if the angle of the line is 45 degrees, its end point coordinates would be roughly 75,15.
You need to use the trigonometric functions sin and cos.
Something like this:
theta = 45
// theta = pi * theta / 180 // convert to radians.
radius = 50
centerX = 50
centerY = 50
p.x = centerX + radius * cos(theta)
p.y = centerY - radius * sin(theta)
Keep in mind that most implementations assume that you're working with radians and have positive y pointing upwards.
Use the unit circle to calculate X and Y, but because your radius is 50, multiply by 50
http://en.wikipedia.org/wiki/Unit_circle
Add the offset (50,50) and bob's your uncle
X = 50 + (cos(45) * 50) ~ 85,36
Y = 50 - (sin(45) * 50) ~ 14,65
The above happens to be 45 degrees.
EDIT: just saw the Y axis is inverted
First you would want to calculate the X and Y coordinates as if the circle were the unit circle (radius 1). The X coordinate of a given angle is given by cos(angle), and the Y coordinate is given by sin(angle). Most implementations of sin and cos take their inputs in radians, so a conversion is necessary (1 degree = 0.0174532925 radians). Now, since your coordinate system is not in fact the unit circle, you need to multiply the resultant values by the radius of your circle. In this given instance, you would multiply by 50, since your circle extends 50 units in each direction. Finally, using a unit circle coorindate system assumes your circle is centered at the origin (0,0). To account for this, add (or subtract) the offset of your center from your calculated X and Y coordinates. In your scenario, the offset from (0,0) is 50 in the positive X direction, and 50 in the negative Y direction.
For example:
cos(45) = x ~= .707
sin(45) = y ~= .707
.707*50 = 35.35
35.35+50 = 85.35
abs(35.35-50) = 14.65
Thus the coordinates of the ending segment would be (85.35, 14.65).
Note, there is probably a built-in degrees-to-radians function in your language of choice, I provided the unit conversion for reference.
edit: oops, used degrees at first

Resources