Collision between two GPS points - c++

I would like to do sphere to sphere intersection or ellipsoid to ellipsoid if I have two GPS coordinate points. What is the formula for that ?
Suppose I have two GPS points with two different lon,lat,alt. I would like to do sphere to sphere intersection or ellipsoid to ellipsoid. Is there a formula for that ?
WGS84toXYZ(xAv, yAv, zAv, (m_sPosAV.GetLongitude()*math::pi) / 180, (m_sPosAV.GetLatitude()*math::pi) / 180, (m_sPosAV.GetAltitude()*math::pi) / 180); // lon direction Nort
WGS84toXYZ(xPoi, yPoi, zPoi, (poi.Position().GetLongitude()*math::pi) / 180, (poi.Position().GetLatitude()*math::pi) / 180, (poi.Position().GetAltitude()*math::pi) / 180); // lon direction Nort
Sphere avSphere;
Sphere poiSphere;
avSphere.position.x = xAv;
avSphere.position.y = yAv;
avSphere.position.z = 0;
avSphere.radius = 1550000;
poiSphere.position.x = xPoi;
poiSphere.position.y = yPoi;
poiSphere.position.z = 0;
poiSphere.radius = 100000;
if (doesItCollide(avSphere, poiSphere))
{
qDebug() << "collision sphere";
}

Sphere is easy:
If distance between your 2 points is smaller than the sum of your radii for the spheres, you got yourself an intersection.

Related

OpenCV drawCircle and draw Rectangles on the circle line

I want to draw circles with different radius and then I want to draw rectangles on this circle.
It should look like this:
]
I have tried it with the formula for the circle
y_Circle = Center_Circle.y + sqrt(pow(Radius, 2) - pow(x_Circle - Center_Circle.x, 2));
but this is just for the lower part of the circle. For the upper part I need this formula, but with a "-" after Center_Circly.y.
The Problem is, that i´m not getting the rectangles in the position like in the image above. It looks like this:
In this image I draw the rectangles on a circle with the formula above. For a better unterstanding I have drawn two circles by hand to show the problem.
You can see, that there is space between the rectangles and in the lower part there is no space between the rectangles. Is there another possibility to do that in an easier way? Maybe like this: Draw a circle with openCV, get access to the coordinates of the circle line and draw the rectangles of this circle line. But I don´t know how to get acces to the coordinates of the circle.
Here is my code-snippet:
for (int Radius = Rect_size; Radius < MaxRadius;)
{
x_Circle = MaxRadius - Radius;
circumference_half = 2 * 3.1415 * Radius / 2;
Rectangle_count = circumference_half / Rect_size;
for (int i = 0; i < Rectangle_count - 1; i++)
{
y_Circle = Center_Circle.y + sqrt(pow(Radius, 2) - pow(x_Circle - Center_Circle.x, 2));
if (y_Circle <= FRAME_Heigth && x_Circle <= FRAME_WIDTH && x_Circle >=0)
{
test = Rect(x_Circle, y_Circle, Rect_size, Rect_size);
rectangle(RectangePic, test, Scalar(0, 255, 255), 1, 8);
imshow("testee", RectangePic);
waitKey();
}
x_Circle += Rect_size;
}
Radius += Rect_size;
}
Try this script for these results:
import cv2, numpy as np, math
# Parameters for the window
hw = 789
# Parameters for the circle
circle_center = hw/2, hw/2
radius = hw/2.25
circle_thickness = 2
circle_color = (255,0,255)
# Parameters for the boxes
num_boxes = 50
box_size = 30
box_color = (0,255,0)
box_thickness = 2
# Create background image
bg = np.zeros((hw, hw, 3), np.uint8)
# Draw circle
cv2.circle(bg, tuple(np.array(circle_center, int)), int(radius), circle_color, circle_thickness)
# Time to draw some boxes!
for index in range(num_boxes):
# Compute the angle around the circle
angle = 2 * math.pi * index / num_boxes
# Compute the center of the box
x, y = circle_center[0] + math.sin(angle)*radius, circle_center[1] + math.cos(angle)*radius
# Compute the corners of the
pt1 = x-box_size/2, y-box_size/2
pt2 = x+box_size/2, y+box_size/2
# Draw Box
cv2.rectangle(bg, tuple(np.array(pt1, int)),tuple(np.array(pt2, int)), box_color, box_thickness)
cv2.imshow('img', bg)
cv2.waitKey(0)
cv2.destroyAllWindows()

Fanning out an "arc" of card meshes

I have n number of cards. Each card is a units in width.
Many popular card games display a hand of cards in the "fanned out" position (see images below), and I would like to do the same. By utilizing the following formula, I'm able to place cards in an arc:
// NOTE: UE4 uses a left-handed, Z-up coordinate system.
// (+X = Forward, +Y = Right, and +Z = Up)
// NOTE: Card meshes have their pivot points in the center of the mesh
// (meshSize * 0.5f = local origin of mesh)
// n = Number of card meshes
// a = Width of each card mesh
const auto arcWidth = 0.8f;
const auto arcHeight = 0.15f;
const auto rotationAngle = 30.f;
const auto deltaAngle = 180.f;
const auto delta = FMath::DegreesToRadians(deltaAngle) / (float)(n);
const auto halfDelta = delta * 0.5f;
const auto halfMeshWidth = a * 0.5f;
const auto radius = halfMeshWidth + (rotationAngle / FMath::Tan(halfDelta));
for (unsigned y = 0; y < n; y++)
{
auto ArcX = (radius * arcWidth) * FMath::Cos(((float)y * delta) + halfDelta);
auto ArcY = (radius * arcHeight) * FMath::Sin(((float)y * delta) + halfDelta);
auto ArcVector = FVector(0.f, ArcX, ArcY);
// Draw a line from the world origin to the card origin
DrawDebugLine(GetWorld(), FVector::ZeroVector, ArcVector, FColor::Magenta, true, -1.f, 0, 2.5f);
}
Here's a 5-Card example from Hearthstone:
Here's a 5-Card example from Slay The Spire:
But the results I'm producing are, well... Suboptimal:
No matter how I tweak the variables, the cards on the far left and far right side are getting squashed together into the hand. I imagine this has to do with how the points of a circle are distributed, and then squashed downwards (via arcHeight) to form an ellipse? In any case, you can see that the results are far from similar, even though if you look closely at the example references, you can see that an arc exists from the center of each card (before those cards are rotated in local space).
What can I do to achieve a more evenly spaced arc?
Your distribution does look like an ellipse. What you need is a very large circle, where the center of the circle is way off the bottom of the screen. Something like the circle below, where the black rectangle is the screen area where you're drawing the cards, and the green dots are the card locations. Note that the radius of the circle is large, and the angles between the cards are small.

Creating a bounding sphere using a GPS coordinate

I would like to create an imaginary bounding sphere around an object. Giving the position of the object in GPS coordinates WGS84 format. And also giving the position in cartesian coordinates.
I have tried to convert degrees lon,lat,alt to radians, and use a function by Spketre, to convert them to Cartesian coordinates.
The problem is I'm doing sphere to sphere collision detection and what I have found that the positions that are converted from spherical to Cartesian are so near so that the sphere intersection method is returning true.
WGS84toXYZ(xAv, yAv, zAv, (m_sPosAV.GetLongitude()*math::pi) / 180, (m_sPosAV.GetLatitude()*math::pi) / 180, (m_sPosAV.GetAltitude()*math::pi) / 180); // lon direction Nort
WGS84toXYZ(xPoi, yPoi, zPoi, (poi.Position().GetLongitude()*math::pi) / 180, (poi.Position().GetLatitude()*math::pi) / 180, (poi.Position().GetAltitude()*math::pi) / 180); // lon direction Nort
Sphere avSphere;
Sphere poiSphere;
avSphere.position.x = xAv;
avSphere.position.y = yAv;
avSphere.position.z = zAv;
avSphere.radius = 1550;
poiSphere.position.x = xPoi;
poiSphere.position.y = yPoi;
poiSphere.position.z = zPoi;
poiSphere.radius = 1200;
if (doesItCollide(avSphere, poiSphere))
{
qDebug() << "collision";
}

draw circle in XPS document API

How can I draw a circle with XPS document API?
I use following code, bit the circle is not drawn at all if start point and end point of circle is same. It draws "the arc of angle zero instead"..
XPS_POINT startPoint = { 500, 500};
hr = xpsFactory->CreateGeometryFigure(&startPoint, &figure);
XPS_SEGMENT_TYPE segmentTypes[1] = {
XPS_SEGMENT_TYPE_ARC_LARGE_COUNTERCLOCKWISE
};
// x - coordinate of the arc's end point.
// y - coordinate of the arc's end point.
// Length of the ellipse's radius along the x-axis.
// Length of the ellipse's radius along the y-axis.
// Rotation angle.
FLOAT segmentData[5] = {
startPoint.x, // if it is startPoint.x + 0.001, I see kind pf a circle
startPoint.y,
radius,
radius,
360
};
BOOL segmentStrokes[1] = {
TRUE// Yes, draw each of the segment arcs.
};
// Add the segment data to the figure.
hr = figure->SetSegments(
1,
5,
segmentTypes,
segmentData,
segmentStrokes);
hr = figure->SetIsClosed(TRUE);
hr = figure->SetIsFilled(TRUE);

Calculating vertices of a rotated rectangle

I am trying to calculate the vertices of a rotated rectangle (2D).
It's easy enough if the rectangle has not been rotated, I figured that part out.
If the rectangle has been rotated, I thought of two possible ways to calculate the vertices.
Figure out how to transform the vertices from local/object/model space (the ones I figured out below) to world space. I honestly have no clue, and if it is the best way then I feel like I would learn a lot from it if I could figure it out.
Use trig to somehow figure out where the endpoints of the rectangle are relative to the position of the rectangle in world space. This has been the way I have been trying to do up until now, I just haven't figured out how.
Here's the function that calculates the vertices thus far, thanks for any help
void Rect::calculateVertices()
{
if(m_orientation == 0) // if no rotation
{
setVertices(
&Vertex( (m_position.x - (m_width / 2) * m_scaleX), (m_position.y + (m_height / 2) * m_scaleY), m_position.z),
&Vertex( (m_position.x + (m_width / 2) * m_scaleX), (m_position.y + (m_height / 2) * m_scaleY), m_position.z),
&Vertex( (m_position.x + (m_width / 2) * m_scaleX), (m_position.y - (m_height / 2) * m_scaleY), m_position.z),
&Vertex( (m_position.x - (m_width / 2) * m_scaleX), (m_position.y - (m_height / 2) * m_scaleY), m_position.z) );
}
else
{
// if the rectangle has been rotated..
}
//GLfloat theta = RAD_TO_DEG( atan( ((m_width/2) * m_scaleX) / ((m_height / 2) * m_scaleY) ) );
//LOG->writeLn(&theta);
}
I would just transform each point, applying the same rotation matrix to each one. If it's a 2D planar rotation, it would look like this:
x' = x*cos(t) - y*sin(t)
y' = x*sin(t) + y*cos(t)
where (x, y) are the original points, (x', y') are the rotated coordinates, and t is the angle measured in radians from the x-axis. The rotation is counter-clockwise as written.
My recommendation would be to do it out on paper once. Draw a rectangle, calculate the new coordinates, and redraw the rectangle to satisfy yourself that it's correct before you code. Then use this example as a unit test to ensure that you coded it properly.
I think you were on the right track using atan() to return an angle. However you want to pass height divided by width instead of the other way around. That will give you the default (unrotated) angle to the upper-right vertex of the rectangle. You should be able to do the rest like this:
// Get the original/default vertex angles
GLfloat vertex1_theta = RAD_TO_DEG( atan(
(m_height/2 * m_scaleY)
/ (m_width/2 * m_scaleX) ) );
GLfloat vertex2_theta = -vertex1_theta; // lower right vertex
GLfloat vertex3_theta = vertex1_theta - 180; // lower left vertex
GLfloat vertex4_theta = 180 - vertex1_theta; // upper left vertex
// Now get the rotated vertex angles
vertex1_theta += rotation_angle;
vertex2_theta += rotation_angle;
vertex3_theta += rotation_angle;
vertex4_theta += rotation_angle;
//Calculate the distance from the center (same for each vertex)
GLfloat r = sqrt(pow(m_width/2*m_scaleX, 2) + pow(m_height/2*m_scaleY, 2));
/* Calculate each vertex (I'm not familiar with OpenGL, DEG_TO_RAD
* might be a constant instead of a macro)
*/
vertexN_x = m_position.x + cos(DEG_TO_RAD(vertexN_theta)) * r;
vertexN_y = m_position.y + sin(DEG_TO_RAD(vertexN_theta)) * r;
// Now you would draw the rectangle, proceeding from vertex1 to vertex4.
Obviously more longwinded than necessary, for the sake of clarity. Of course, duffymo's solution using a transformation matrix is probably more elegant and efficient :)
EDIT: Now my code should actually work. I changed (width / height) to (height / width) and used a constant radius from the center of the rectangle to calculate the vertices. Working Python (turtle) code at http://pastebin.com/f1c76308c