Scaling a "skewed polygon" lazarus / 21-General ---------------------------------------------------------------------- MarkMLl · 2024-07-16 13:20 I appreciate that some might raise their eyebrows at the idea of a polygon being skewed, but I ask for their indulgence while I explain the problem. The attached image shows the circuit pattern associated with a small airfield, with the runway on the lower of the two long legs (marked with a grey track). The Northeastern approach is offset due to a requirement to avoid overflying certain properties ("avoid" areas marked in red). I have been attempting to expand the circuit track using software to give me an overlay such as the one I've drawn in manually (yellow), with the intention of being able to identify a "circuit perimeter" within which aircraft should exercise particular caution. Nominally, the perimeter is 500m outside the circuit. Unfortunately, I seem to have presented myself with a rather thorny problem :-) The main issue is that due to the (approximate) rectangle being rotated, even if I translate it so that the geographical origin is at its centre two of the corners (marked with a through-line) lie in the "wrong quadrant" with the result that standard graphics operations don't work properly: they move at least one corner in the wrong direction, and/or by a significantly wrong distance. It might be possible to compensate for that by rotating the circuit pattern before trying to scale it, but as soon as that approach was applied to an airfield with two active runways I'm pretty sure it would go wrong. There are two approaches I feel might work: a) Move each line (leg of the circuit) out by 500m along the normal, then (iteratively?) work out a new point of intersection. I feel the latter stage could get hairy, particularly since it would have to accommodate situations where the lines already crossed due to a rentrant point. b) For each pair of lines, compute the angle between them and move the intersection an appropriate distance. If they met at a right angle this would presumably be Sqrt(2) * 500m, for a rentrant point this would be proportionately smaller... wouldn't have to be exact. I'm not for the moment attaching what I've been struggling with so far, since I think the "quadrant problem" makes it unusable by definition. Any thoughts would be appreciated. MarkMLl ---------------------------------------------------------------------- loaded · 2024-07-16 15:48 https://forum.lazarus.freepascal.org/index.php/topic,67542.msg520124.h tml#msg520124 In the sample application I sent in the relevant message called "Interactive_ Transparent_ Polygon" (which I wrote with the experience I gained from cartography knowledge), there is a function of rotating clockwise or counterclockwise from any point of the polygon (Ctrl + Mouse Middle Wheel). There is also a method for enlarging or reducing (Shift + Mouse Middle wheel). ---------------------------------------------------------------------- MarkMLl · 2024-07-16 16:51 I've corrected the misleading initial subject line. MarkMLl ---------------------------------------------------------------------- MarkMLl · 2024-07-16 17:28 Program illustrating my problem attached. My code is pretty sloppy: I'm doing maths on degrees when I should be scaling them to metres first but there's some fairly serious sign errors in there I've not managed to track down yet. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-16 17:31 As I understand, you mean a "polygon buffering" -  offsetting with all points in the perimeter of the new shape are equidistant with their closest points in the perimeter of the original shape. Am I right? ---------------------------------------------------------------------- MarkMLl · 2024-07-16 18:47 Yes. Draw airspace perimeter outside (500m by default) the circuit. The magnitude handling in my code is crappy, but there's a far more serious problem- which I can't see- in the sign handling. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-17 11:31 Quote from: MarkMLl on July 16, 2024, 08:47:56 pmYes. Draw airspace perimeter outside (500m by default) the circuit. The magnitude handling in my code is crappy, but there's a far more serious problem- which I can't see- in the sign handling. MarkMLl IMO the scaling won't work. I wrote makePerimeter2 function. The approach is to offset each segment outwards, then to connect them with a (Bezier) curve. Segments intersection point was taken twice for the control points, which isn't optimal. The reentrant case is not considered, but it must be trivial since the intersection point must replace both segment ends and no need for a curve. ---------------------------------------------------------------------- MarkMLl · 2024-07-17 11:45 Quote from: alpine on July 17, 2024, 01:31:52 pmIMO the scaling won't work. I wrote makePerimeter2 function. The approach is to offset each segment outwards, then to connect them with a (Bezier) curve. Segments intersection point was taken twice for the control points, which isn't optimal. The reentrant case is not considered, but it must be trivial since the intersection point must replace both segment ends and no need for a curve. Thanks very much, I'll take a look. Obviously this is very much subsidiary to the Barkway example which was intended to be entirely to illustrate adding areas and lines to LMV, but I thought it was a useful example in which to raise it. I can see one of the problems in my original code: the signs are transposed between lat and long when working out the distance the points should be offset. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-17 17:19 For smoother curves you can add (see the highlighted lines below): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  // Calculate points of a 4-point bezier  procedure CalcBezier(APoints: array of TPoint; out APoly: TPointArray);  var    NPoints: Integer;  PtArray: PPoint;    PtCount: LongInt = 0;  begin    NPoints := Length(APoints);    if NPoints < 4 then      Exit; // Curve must have at least 4 points    PtArray := Nil;    APoly := Nil;    try      // Assuming APoints[1]=APoints[2]=crossing point, replace APoints[1]    // with a 2/3-rd of and APoints[2] with a 2/3-rd      // of to make the curve smoother    APoints[1].X := Round((2 * APoints[1].X + APoints[0].X) / 3);      APoints[1].Y := Round((2 * APoints[1].Y + APoints[0].Y) / 3);      APoints[2].X := Round((2 * APoints[3].X + APoints[2].X) / 3);      APoints[2].Y := Round((2 * APoints[3].Y + APoints[2].Y) / 3);       PolyBezier2Polyline(APoints, PtArray, PtCount, True);             ... EDIT: It occurs to me now that instead of calculating the curve you can just add the two control points [1,2] and thus create a miter joint, which IMHO is preferable no matter it's less accurate ---------------------------------------------------------------------- cdbc · 2024-07-17 21:24 Hi @alpine: How do you highlight lines like that, in your forum-post?!? I can't seem to figure it out... Regards Benny ---------------------------------------------------------------------- alpine · 2024-07-17 23:26 See the attached picture. ---------------------------------------------------------------------- cdbc · 2024-07-18 04:54 Hi Cool, thanks mate  Regards Benny ---------------------------------------------------------------------- MarkMLl · 2024-07-18 09:18 Rather to my surprise, it appears that my original approach does work... not perfectly since it doesn't take the angle of intersecting lines into account, and it operates along lines from the center so the perimeter is somewhat distorted on a typical airfield circuit pattern. Unfortunately, I can't get your better way to work, even with LMV r9394... there's +infs in the output values and LMV (possibly wisely) refuses to plot the resulting area. See bottom of new geometry unit. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-18 11:00 Quote from: MarkMLl on July 18, 2024, 11:18:14 amUnfortunately, I can't get your better way to work, even with LMV r9394... there's +infs in the output values and LMV (possibly wisely) refuses to plot the resulting area. See bottom of new geometry unit. That's because when moved to the separate geometry.pas unit, you forgot to initialize CosLat0 global (used for Lat,Lon to/from X,Y) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  CosLat0: Double = 0.615661475; // = Cos(DegToRad(52)); Must be the cosine of the map center longitude, but assuming we have fixed that on 52. ---------------------------------------------------------------------- MarkMLl · 2024-07-18 11:16 Quote from: alpine on July 18, 2024, 01:00:32 pmQuote from: MarkMLl on July 18, 2024, 11:18:14 amUnfortunately, I can't get your better way to work, even with LMV r9394... there's +infs in the output values and LMV (possibly wisely) refuses to plot the resulting area. See bottom of new geometry unit. That's because when moved to the separate geometry.pas unit, you forgot to initialize CosLat0 global (used for Lat,Lon to/from X,Y) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  CosLat0: Double = 0.615661475; // = Cos(DegToRad(52)); Got it: in form creation (mea culpa). Result looks a bit small visually but nicely aligned. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-18 11:26 Here is the faulty calculation (which I deliberately neglected) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // Calculate expansion meters to pixels for the DoSegm/OfsLine  ExpandPt := Point(    Round(expandMetres * 1.0/68 * CI_FACTOR),    Round(expandMetres * 1.0/111 * CI_FACTOR)); ---------------------------------------------------------------------- MarkMLl · 2024-07-18 12:12 Ah- meters to pixels. I see. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-18 12:26 Quote from: MarkMLl on July 18, 2024, 02:12:26 pmAh- meters to pixels. I see. Even the comment is wrong  Should be in equirectangular units (LatLonToXY, XYToLatLon) ---------------------------------------------------------------------- MarkMLl · 2024-07-18 12:58 Quote from: alpine on July 18, 2024, 02:26:06 pmQuote from: MarkMLl on July 18, 2024, 02:12:26 pmAh- meters to pixels. I see. Even the comment is wrong  Should be in equirectangular units (LatLonToXY, XYToLatLon) Actually, I'm still scratching my head over that one: changing CI_FACTOR (e.g. to 10) appears to have no effect. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-18 13:46 Quote from: MarkMLl on July 18, 2024, 02:58:44 pmQuote from: alpine on July 18, 2024, 02:26:06 pmQuote from: MarkMLl on July 18, 2024, 02:12:26 pmAh- meters to pixels. I see. Even the comment is wrong  Should be in equirectangular units (LatLonToXY, XYToLatLon) Actually, I'm still scratching my head over that one: changing CI_FACTOR (e.g. to 10) appears to have no effect. MarkMLl This is the factor to make equirectangular coords integers suitable for TPoint. See LatLonToXY, XYToLatLon. Changing it will affect only the significance/precision (Range checks?). ---------------------------------------------------------------------- alpine · 2024-07-22 11:08 Since my trackToArea2 was so wrong at expansion offset calculation, I've performed some point-and-click measurements, found the miscalculation and corrected it:Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   // Calculate expansion meters to pixels for the DoSegm/OfsLine  Rp.InitLatLon( // Rp: TRealPoint    expandMetres * 1.0/111000,    expandMetres * 1.0/68000);  ExpandPt := LatLonToXY(Rp); {instead of:}  ExpandPt := Point(    Round(expandMetres * 1.0/68) * CI_FACTOR,    Round(expandMetres * 1.0/111) * CI_FACTOR); Meanwhile, I've noticed that in your trackToArea1 the  region points were moved along the beam from the center of the bounding box. That moves vertices by the given distance, but results in a smaller distances between the line segments. ---------------------------------------------------------------------- MarkMLl · 2024-07-22 19:04 Thanks, I'll take a look at that tomorrow. I've been experimenting trying to get as much as possible done using simple geometry, the incentive- on reflection- being that I want the result to be easily processed by checks for whether an aircraft is following the circuit, close to the circuit and so on since different levels of alert apply. I'm 50+ years out of practice at this sort of stuff :-) I've got it working fairly well- including for reentrant angles- if I expand along the mean corner angle with a fudge factor so that the actual distance outside the circuit is realistic. I've not yet got it working if I try to expand along normals: I obviously know that it's basically just a lot of sin/cos stuff and while I can visualise what I'm trying to do I'm having a hard time getting it right in practice :-/ I'll probably put a few more hours into it tomorrow, but what I've got is visually OK and in practice smaller airfields will generally have a simple rectangular circuit: I suspect that the local field's kinked approach is fairly unusual which makes being able to handle that from the start useful. MarkMLl ---------------------------------------------------------------------- alpine · 2024-07-22 19:44 Quote from: MarkMLl on July 22, 2024, 09:04:13 pmThanks, I'll take a look at that tomorrow. No problem. I wanted just to resolve this in order to put it as an example in LMV. Quote from: MarkMLl on July 22, 2024, 09:04:13 pmI've been experimenting trying to get as much as possible done using simple geometry, the incentive- on reflection- being that I want the result to be easily processed by checks for whether an aircraft is following the circuit, close to the circuit and so on since different levels of alert apply. IMHO Sin/cos for normals is not an issue since the expansion circuit will be calculated just once, then saved. The proximity checks can be performed fairly easy, even the distance between each pair of points in the expanded circuit (denominator) can be pre-calculated. Once a series of expanded circuits have been pre-computed (for each alarm), then the proximity check should be a simple check of which is the most outer circuit that the aircraft is inside of. That is even easier - just the sign of the oriented distance must be the same for all segments, no need for magnitude.