Mapping a 2D grid onto a sphere - language-agnostic

I want to map a grid to a sphere like this:
In other words, for every point (x, y) ∈[0,1] on the left, I need the (x, y, z) coordinates of the equivalent point on the sphere, between the -45º and +45º meridians on each axis. You can also think of the source coordinates as two angles such that:
phi = -45º + x * 90º
theta = -45º + y * 90º
The traditional latitude-longitude or polar formulas I've found elsewhere are of no use because the results they produce are only distorted along one axis. Any other suggestions?

Define two functions, a and b, that map your x and y coordinates to the appropriate theta and phi angles:
a(x) = (pi / 4) * (2x - 1)
b(y) = (pi / 4) * (4y + 1)
And then just map the resulting spherical coordinate back into a Cartesian coordinate:
You'll get a function of r, x', y', members of [0, 1] x [0, 1], which will map the 2D coordinate onto a sphere of radius r.

Related

Calculate the lat/lng from Relative Position P2 ( x y z coordinates)

I have the relative position for some point P2 on the Map as x y z coordinates. This position is relative to some position P1 define by its longitude and latitude and heading.
how can I compute the longitude and latitude for P2?
For example: P2: x = 24, y = 26, z = 30 P1: lat/lng: 53.090734, 10.435428 with heading 110°.
P2 is relative to P1. How to compute P2 lat/lng?
This is what I tried but it doesn't work.
latP2 = latP1 - (Math.sin(Math.toRadians(heading)) * X + Math.cos(Math.toRadians(heading)) * Y) * (1.0 / 111000.0);
lonP2 = longP1 + (Math.cos(Math.toRadians(heading)) * X + Math.sin(Math.toRadians(heading)) * Y) * (1.0 / 71500.0);
I think I don't need to use z value in my calculation:
From the updaten quetsion and the discussion, you have got the coordinate in lat,lon and the direction of sight, measured in degrees relative to North.
Addionally you have an offset x,y given in meters.
Now you want the new position, e.g where increasing y is releated to line of sight.
Such a calculation always follow the same principle.
Convert your lat/long cooridnate(s) to a local cartesian coordinate system, measured in meters. (Now you have position x,y coordinates as learned in school)
Do the (vektor) math as you learned in school, which will give you a new point.
convert the new point back using the inverse transformation.
For step 1, and 3 see Find intersection of two Locations
and the discussion in chat

Solving the Points on a Rotated Rectangle

I am working in AS3.
I have a generic rectangle. This rectangle can have any length, any width and any rotation. I am trying to solve for the x and y coordinates of the four corners of the rectangle. I know the coordinates of the centre of the rectangle, I know its width, its height, the y distance between the highest and lowest point and the x distance between the farthest left and farthest right point as well as knowing the rotation.
My code currently looks like this (Object, of course, being the rectangle in question, keep in mind that when I apply this it can have any dimensions - This is just one possibility. Initial width and height are the actual length and width, while width and height referenced later are the x and y distances between the highest and lowest points and the farthest left and right points, rotation is of course rotation, and x and y are the object's centre coordinates).
import flash.events.Event;
addEventListener(Event.ENTER_FRAME, Rotate, false, 0, true);
var Radius:Number = Math.sqrt(((Object.height / 2) * (Object.height / 2)) + ((Object.width / 2) * (Object.width / 2)));
function Rotate(event:Event)
{
Object.rotation += 1;
Marker1.x = Math.sqrt((Radius * Radius) - ((Object.height / 2) * (Object.height / 2))) + Object.x;
Marker2.x = - Math.sqrt((Radius * Radius) - ((Object.height / 2) * (Object.height / 2))) + Object.x;
Marker3.y = Math.sqrt((Radius * Radius) - ((Object.width / 2) * (Object.width / 2))) + Object.y;
Marker4.y = - Math.sqrt((Radius * Radius) - ((Object.width / 2) * (Object.width / 2))) + Object.y;
Marker1.y = Object.y + (Object.height / 2);
Marker2.y = Object.y - (Object.height / 2);
Marker3.x = Object.x + (Object.width / 2);
Marker4.x = Object.x - (Object.width / 2);
}
As you can see I am attempting to use circle geometry to place four small circles (Markers 1-4) at the corners of the rectangle, just for testing purposes to confirm that I have gathered the correct coordinates. Problem is, the coordinates will always be placed in either +x and +y or -x and -y, but never the other two quadrants of the graph. I can't figure out a simple way of dynamically simulating the +- of the quadratic equation in the program. Does anyone know of a way to find these four points with and length, width and rotation of the rectangle?
If you represent the coordinates of the corners as offsets from the midpoint of the rectangle you can easily rotate them anti-clockwise by an angle θ with
dx' = dx × cos θ - dy × sin θ
dy' = dx × sin θ + dy × cos θ
You can then add the rotated offsets to the midpoint to recover the new coordinates of the corners.

Syncing overlay to displacement map filter

I'm using a DisplacementMapFilter to created a globe-like effect on a flat map. My problem is, I also want to sync some labels to this map. I have the x/y coordinates for their locations on the flat map, but I need to map them to the now-displaced image.
I would like to be able to do this using the BitmapData that contains the displacement map, so that changing the Bitmap changes both the displacement filter and the label locations. Also, the labels will not be static, and accuracy is fairly important.
There is a formula in DisplacementMapFilter reference:
dstPixel[x, y] =
srcPixel[
x + ((componentX(x, y) - 128) * scaleX) / 256,
y + ((componentY(x, y) - 128) *scaleY) / 256)
]
componentX/Y are color channels in the bitmap (you can bind any channel to coordinates).
As I understand, you need to shift map labels as filter would do. Just take label coordinates (x, y), sample source bitmap with getPixel32(x, y). Then you need to figure out which bytes to take for x, y - I guess by default it would be R, G components, respectively. Then use formula to get displaced label coordinates.
Note: getPixel32 returns uint color in ARGB format. Use shift operator (>>) to get color components:
uint ARGB = bitmap.getPixel32(x, y);
int B = ARGB & 0xFF;
int G = (ARGB >> 8) & 0xFF;
int R = (ARGB >> 16) & 0xFF;

Vectors calculations in physics

You are given the radius of a circle, as well as a point P in the circle( x,y), how do you write a function to return an x number of points( x,y), all on the circumference of the given circle. Also, how do you go about finding the angle between each generated point and point P.
I assume you would want the points on the circumference to be evenly distributed along the circumference. If this is the case, you can calculate the number of degrees between each point by dividing 360 by the number of points that you want.
Then, you can obtain any point's (x, y) coordinates as such:
(x, y) = (cos(angle), sin(angle))
where 'angle' the is the angle for the given point. (This is assuming you want values between -1 and 1, as is the case with a unit circle: http://en.wikipedia.org/wiki/Unit_circle) For example, if you want 4 points along the circle's circumference, you can calculate that there is exactly 360/4 = 90 degrees between consecutive points.
So let's call these points point0, point1, point2 and point3. Point0 is at an angle of 0 degrees, point1 at 90 degrees (1 * 90), point2 at 180 (2 * 90) and point3 at 270 (3 * 90). The coordinates for each point are then:
point0 = (cos(0), sin(0)) = (1, 0)
point1 = (cos(90), sin(90)) = (0, 1)
point2 = (cos(180), sin(180)) = (-1, 0)
point3 = (cos(270), sin(270)) = (0, -1)
Keep in mind that you normally start measuring angles on the right side of the horizontal axis of a circle. (On a clock: At the 3)
EDIT: Also please note that almost all trigonometric functions in programming take radian values instead of degrees. Radians can be hard to think with, however, which is why it's very useful to know how to convert radians and degrees to eachother. To convert degrees to radians, multiply the degree value by (pi/180). To convert radians to degrees, multiply the radian value by (180/pi). There is a reasoning behind this all, so if you would like to know more about this, I suggest you read up on radians. http://en.wikipedia.org/wiki/Radian
As far as the angle between these points and the point P goes; I will only give you some directions. You can calculate the x- and y-differences between the points and point P (this should be trivial for you, it consists of mere subtractions). Using these two values, you can calculate the angle between the points.

Find the last co-ordinate of isosceles triangle given coordinates of base and altitude

I have no clue about trigonometry, despite learning it in school way back when, and I figure this should be pretty straightforward, but trawling through tons of trig stuff on the web makes my head hurt :) So maybe someone could help me...
The title explains exactly what I want to do, I have a line:
x1,y1 and x2,y2
and want a function to find x3,y3 to complete an isosceles triangle, given the altitude.
Just to be clear, the line x1,y2 -> x2,y2 will be the base, and it will not be aligned any axis (it will be at a random angle..)
Does anyone have a simple function for this??
construct a normal to the vector (x1,y1)->(x2,y2). place it at the midpoint ((x1+x2)/2,(y1+y2)/2) and go out a distance h.
the normal will look like (-(y2-y1),x2-x1). make this a unit vector (http://en.wikipedia.org/wiki/Unit_vector).
add h times this unit vector to the midpoint.
The third point is on the perpendicular bisector of your base, and is altitude units away from the line.
Calculate the midpoint of the base by averaging the x and y coordinates.
Calculate the slope of your altitude: -dx/dy (perpendicular to dy/dx). You now have your line (point and slope).
y - my = -dx/dy * (x - mx)
Substitute your variables in the distance formula: d = sqrt(dx^2 + dy^2)
d = sqrt((x - mx)^2 + (y - my)^2)
d = sqrt((x - mx)^2 + (-dx/dy * (x - mx))^2)
d^2 = (x - mx)^2 + (-dx/dy * (x - mx))^2
d^2 - (x - mx)^2 = (-dx/dy * (x - mx))^2
±sqrt(d^2 - (x - mx)^2) = -dx/dy * (x - mx)
±sqrt(d^2 - (x - mx)^2) * dy/dx = x - mx
±sqrt(d^2 - (x - mx)^2) * dy/dx + mx = x
x = ±sqrt(d^2 - (x - mx)^2) * dy/dx + mx
Calculate the other variable (y here) using your line equation (from #2).
You now have two points; pick whichever you want...
In pseudocode:
dx = x1 - x2
midpoint = ((x1 + x2) / 2, (y1 + y2) / 2)
slope = -dx / (y1 - y2)
x = sqrt(altitude*altitude - dx*dx) / slope + midpoint.x
y = slope * (x - midpoint.x) + midpoint.y
This is probably not the most optimal method. Not sure if it even works. xD
Al I can remember is that an isosceles triangle will have sides of equal length, and equal angles at the base. If you have the height, then you have the final coordinate because this will be the point of intersection, right?