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
Related
Say we are given a rectangle with following coordinates,
-95.3898486, 29.8421133
-95.3908478, 29.8417155
-95.3904025, 29.8409942
-95.3894423, 29.8413990
-95.3898486, 29.8421133
(plotted above)
How do we calculate by what degree rectangle is rotated with respect to the center?
Having two neighbor vertices A and B, you can calculate direction angle of AB vector
alpha = atan2(B.y - A.y, B.x - A.x)
Also note that you can rotate rectangle both by angle alpha and by angle (90-alpha) (might be calculated for another square side).
If you work in lat/lon coordinates (not sure whether it is needed for ready projection in screen coordinates), formula is a bit more complex (bearing here), while rough approximation might be used for some purposes
alpha = atan2(B.lon - A.lon, (B.lat - A.lat) / Cos(A.lon))
Example in Python
import math
A = [-95.38984863773346,29.84211329694586]
B = [-95.3894422830677,29.84139904417855]
alpha = math.atan2(B[1]-A[1], B[0]-A[0])
print(math.degrees(alpha)) #not corrected
beta = math.atan2(B[1]-A[1], (B[0]-A[0]) / math.cos(math.radians(A[1])))
print(math.degrees(beta)) #corrected by latitude squeezing
-60.36346643470065
-56.73935489918559
Lets say I have a point within a circle(not necessarily the origin) moving at a given vector how would I calculate the x and y coordinate of the point where it hits the edge of the circle.
Shift all coordinates by -cx, -cy. Now circle is centered at origin and has equation
x^2+y^2=R^2
Point coordinate (px, py), unit direction vector is (dx,dy). Equation of ray:
x = px + t * dx
y = py + t * dy
Substitute these variables into the circle equation, solve equation, find parameter t>0, then find intersection point (x,y), shift it back by (cx, cy).
i have an image and 3 points with following datas for each point:
x and y 2d-world coordinates
x image coordinate
how can i calculate the camera orientation (only left/right) and the 2d-world position?
thanks.
edit: the image is a normal photography (so perspective projection). The world coordinate is a top view of a map, so Orthographic projection).
Given a point in world space, the projection can be expressed as
(x - cx) * cos(phi) - (y - cy) * sin(phi)
proj(x, y) = -----------------------------------------
(x - cx) * sin(phi) + (y - cy) * cos(phi)
cx and cy are the camera position and phi is the camera rotation. The projection will result in a value in camera coordinates (not image coordinates). To transform image coordinates to camera coordinates, usw
cameraX(imageX) = (2 * imageX / W - 1) * tan(fovy / 2) * ratio
W is the pixel width of the image, fovy is the vertical field of view, ratio is the image's aspect ratio.
Then you want to solve the system of equations formed by the three given points. There is an analytic solution, but it is quite complex. So you're left with numerical (probably least-squares) solvers. Pick one, plug in the formula and get your result. Since you optimize for both a position and an angle, you may want to normalize the values so that they have a similar range. I got quite good results with levmar for similar problems if you're unsure what optimizer to use.
This all assumes that the camera does not distort the image.
I'm trying to use latitude and longitude coordinates to plot a map in Processing. Is there a way to set the zero coordinates of the X and Y axis to the center of the display window.
Or does anyone know how to convert spherical coordinates to cartesian?
Thanks
I'll assume you have spherical coordinates of r, radius; theta, horizontal angle around Z-axis starting at (1,0,0) and rotating toward (0,1,0); and phi, vertical angle from positive Z-axis toward negative Z-axis; that being how I remember it from back when. Remember that angles are in radians in most programming languages; 2*pi radians = 180 degrees.
x = r * cos(theta) * sin(phi)
y = r * sin(theta) * sin(phi)
z = r * cos(phi)
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