From c678e615a24c0f3553fb2eaf50f6f73151b1d636 Mon Sep 17 00:00:00 2001 From: codeimp Date: Fri, 3 Oct 2008 14:31:25 +0000 Subject: [PATCH] experimental label position calculator --- Source/Geometry/SectorTools.cs | 56 +++++++++++++++++++++++++++++++++- Source/Map/Sector.cs | 26 +++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/Source/Geometry/SectorTools.cs b/Source/Geometry/SectorTools.cs index fe00533d..78a054ea 100644 --- a/Source/Geometry/SectorTools.cs +++ b/Source/Geometry/SectorTools.cs @@ -50,7 +50,7 @@ namespace CodeImp.DoomBuilder.Geometry #endregion #region ================== Constants - + #endregion #region ================== Pathfinding @@ -579,5 +579,59 @@ namespace CodeImp.DoomBuilder.Geometry } #endregion + + #region ================== Sector Labels + + // This finds the ideal label position for a sector + public static Vector2D FindLabelPosition(Sector s, float resolution) + { + Vector2D foundpoint = new Vector2D(); + float founddist = 0.0f; + + // Calculate grid numbers + RectangleF bbox = s.CreateBBox(); + int nodesx = (int)(bbox.Width / resolution); + int nodesy = (int)(bbox.Height / resolution); + + // Make list of linedefs + List lines = new List(s.Sidedefs.Count); + foreach(Sidedef sd in s.Sidedefs) lines.Add(sd.Line); + + // Scan the polygon + for(int x = 0; x < nodesx; x++) + { + for(int y = 0; y < nodesy; y++) + { + // Calculate absolute point + Vector2D p = new Vector2D(bbox.X + (float)x * resolution, + bbox.Y + (float)y * resolution); + + // Point inside sector? + Linedef ld = MapSet.NearestLinedef(lines, p); + Sidedef sd = (ld.SideOfLine(p) < 0) ? ld.Front : ld.Back; + if((sd != null) && (sd.Sector == s)) + { + // Go for all linedefs to calculate the smallest distance + float mindist = int.MaxValue; + foreach(Linedef l in lines) + { + float dist = l.DistanceToSq(p, true); + if(dist < mindist) mindist = dist; + } + + // Better match? + if(mindist > founddist) + { + foundpoint = p; + founddist = mindist; + } + } + } + } + + return foundpoint; + } + + #endregion } } diff --git a/Source/Map/Sector.cs b/Source/Map/Sector.cs index 55bfccbe..f89ef8cb 100644 --- a/Source/Map/Sector.cs +++ b/Source/Map/Sector.cs @@ -247,7 +247,31 @@ namespace CodeImp.DoomBuilder.Map #endregion #region ================== Methods - + + // This creates a bounding box rectangle + // This requires the sector polygon to be up-to-date! + public RectangleF CreateBBox() + { + // Setup + float left = float.MaxValue; + float top = float.MaxValue; + float right = float.MinValue; + float bottom = float.MinValue; + + // Go for vertices + foreach(FlatVertex v in vertices) + { + // Update rect + if(v.x < left) left = v.x; + if(v.y < top) top = v.y; + if(v.x > right) right = v.x; + if(v.y > bottom) bottom = v.y; + } + + // Return rectangle + return new RectangleF(left, top, right - left, bottom - top); + } + // This joins the sector with another sector // This sector will be disposed public void Join(Sector other)