Skip to content

Commit

Permalink
ptl tool fixed, now works correctly regardless of rotation settings
Browse files Browse the repository at this point in the history
  • Loading branch information
ronyan committed Jan 1, 2021
1 parent d8a6bfb commit 6fa90e9
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
2 changes: 1 addition & 1 deletion CSiTRadar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ void CSiTRadar::OnRefresh(HDC hdc, int phase)

// Draw PTL
if (hasPTL.find(radarTarget.GetCallsign()) != hasPTL.end()) {
HaloTool::drawPTL(&dc, radarTarget, p, 3, pixnm);
HaloTool::drawPTL(&dc, radarTarget, this, p, 3);
}

// Get information about the Aircraft/Flightplan
Expand Down
68 changes: 41 additions & 27 deletions HaloTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,74 @@ class HaloTool :
{
public:

static void drawHalo(CDC* dc, POINT p, double r, double pixpernm)
static inline double degtorad(double deg) {
double ans = deg * PI / 180;
return ans;
}

static CPosition calcPTL(CPosition origin, double ptlLen, double gs, double bearing) {
double lat2;
double long2;
double earthRad = 3440; // in nautical miles

lat2 = asin(sin(origin.m_Latitude * PI / 180) * cos((ptlLen * gs / 60) / earthRad) + cos(origin.m_Latitude * PI / 180) * sin((ptlLen * gs / 60) / earthRad) * cos(bearing * PI / 180));
long2 = degtorad(origin.m_Longitude) + atan2((sin(bearing * PI / 180)) * sin((ptlLen * gs / 60) / earthRad) * cos(origin.m_Latitude * PI / 180), (cos(sin(bearing * PI / 180) / earthRad) - sin(origin.m_Latitude * PI / 180) * sin(lat2)));
CPosition ptlEnd;

ptlEnd.m_Latitude = lat2 * 180 / PI;
ptlEnd.m_Longitude = long2 * 180 / PI;

return ptlEnd;
};

static double calcBearing(CPosition origin, CPosition end) {
double y = sin(degtorad(end.m_Longitude) - degtorad(origin.m_Longitude)) * cos(degtorad(end.m_Latitude));
double x = cos(degtorad(origin.m_Latitude)) * sin(degtorad(end.m_Latitude)) - sin(degtorad(origin.m_Latitude)) * cos(degtorad(end.m_Latitude)) * cos(degtorad(end.m_Longitude) - degtorad(origin.m_Longitude));
double phi = atan2(y, x);
double bearing = fmod((phi * 180 / PI + 360), 360);

return bearing;
}

static void drawHalo(CDC* dc, POINT p, double r, double pixpernm)
{
int sDC = dc->SaveDC();

//calculate pixels per nautical mile

int pixoffset = (int)round(pixpernm * r);

// draw the halo around point p with radius r in NM
// draw the halo around point p with radius r in NM
COLORREF targetPenColor = RGB(202, 205, 169);
HPEN targetPen = CreatePen(PS_SOLID, 1, targetPenColor);
dc->SelectObject(targetPen);
dc->SelectStockObject(HOLLOW_BRUSH);
dc->Ellipse(p.x - pixoffset, p.y - pixoffset, p.x + pixoffset, p.y + pixoffset);
dc->Ellipse(p.x - pixoffset, p.y - pixoffset, p.x + pixoffset, p.y + pixoffset);

DeleteObject(targetPen);

dc->RestoreDC(sDC);
};

static void drawPTL(CDC* dc, CRadarTarget radtar, POINT p, double ptlTime, double pixpernm)
static void drawPTL(CDC* dc, CRadarTarget radtar, CRadarScreen* radscr, POINT p, double ptlTime)
{
int sDC = dc->SaveDC();

double theta = (radtar.GetTrackHeading() + CSiTRadar::magvar) * PI / 180;
double ptlDistance = radtar.GetGS() / 60 * ptlTime * pixpernm;
double ptlYdelta = -cos(theta) * ptlDistance;
double ptlXdelta = sin(theta) * ptlDistance;

CPosition pos1 = radtar.GetPreviousPosition(radtar.GetPosition()).GetPosition();
CPosition pos2 = radtar.GetPosition().GetPosition();
double theta = calcBearing(pos1, pos2);
CPosition ptl = calcPTL(radtar.GetPosition().GetPosition(), ptlTime, radtar.GetPosition().GetReportedGS(), theta);
POINT p2 = radscr->ConvertCoordFromPositionToPixel(ptl);

COLORREF targetPenColor = C_PTL_GREEN;
HPEN targetPen = CreatePen(PS_SOLID, 1, targetPenColor);
dc->SelectObject(targetPen);

dc->MoveTo(p);
dc->LineTo(p.x + (int)round(ptlXdelta), p.y + (int)round(ptlYdelta));
dc->LineTo(p2);

DeleteObject(targetPen);

dc->RestoreDC(sDC);
};

static CPosition calcPTL(CPosition origin, double ptlLen, double gs, double bearing) {
double lat2;
double long2;
double earthRad = 3440; // in nautical miles

lat2 = asin(sin(origin.m_Latitude * PI / 180) * cos((ptlLen * gs / 60) / earthRad) + cos(origin.m_Latitude * PI / 180) * sin((ptlLen*gs/60) / earthRad) * cos(bearing * PI/180));
long2 = origin.m_Longitude + atan2((sin(bearing * PI / 180)) * sin((ptlLen * gs / 60) / earthRad) * cos(origin.m_Latitude * PI / 180), (cos(sin(bearing * PI / 180) / earthRad) - sin(origin.m_Latitude * PI / 180) * sin(lat2)));
CPosition ptlEnd;

ptlEnd.m_Latitude = lat2 * 180 / PI;
ptlEnd.m_Longitude = long2 * 180 / PI;

return ptlEnd;
};
};

0 comments on commit 6fa90e9

Please sign in to comment.