2016-11-20 163 views
1

我正在製作自動化的平面圖生​​成桌面應用程序。在此,首先我繪製多邊形上的點,使用這種方法如何檢測多邊形的邊角?

public void DrawPolygonPointF(PaintEventArgs e) { 

// Create pen. 
Pen blackPen = new Pen(Color.Black, 3); 

// Create points that define polygon. 
PointF point1 = new PointF(50.0F, 50.0F); 
PointF point2 = new PointF(100.0F, 25.0F); 
PointF point3 = new PointF(200.0F, 5.0F); 
PointF point4 = new PointF(250.0F, 50.0F); 
PointF point5 = new PointF(300.0F, 100.0F); 
PointF point6 = new PointF(350.0F, 200.0F); 
PointF point7 = new PointF(250.0F, 250.0F); 
PointF[] curvePoints = 
     { 
      point1, 
      point2, 
      point3, 
      point4, 
      point5, 
      point6, 
      point7 
     }; 

// Draw polygon curve to screen. 
e.Graphics.DrawPolygon(blackPen, curvePoints); 
} 

注意:這些點是不實際的點,他們僅用於演示目的。我正在閱讀文本文件中的要點。

現在我需要生成一個特殊類型的網格。 在生成網格時,第一步是檢測拐角並擴展拐角線。 如何檢測多邊形的邊角因此我可以移動到下一步生成網格?

角落標記。我需要在水平左側擴展標有黑色的角落,另一個是在右側擴展直到觸及線條。

附上截圖。

CORNERS ARE MARKED WITH BLACK AND RED

在此先感謝

+0

首先,你必須定義什麼是角落 –

+0

我有一個截圖。我想與你分享屏幕短片,但堆棧溢出不允許我? –

+0

編輯帖子並添加圖片... – igorushi

回答

0

在我的理解,你想擴展的邊緣,而不是角落。
的程序可以是:

  1. 枚舉邊緣(每2個相鄰的點限定的邊緣)
  2. 對於每個邊緣發現,如果它的垂直或水平(abs(x1-x2) > abs(y1-y2)
  3. 查找如果邊緣可以延長,水平方向(左/右)和垂直(上/下)

    布爾CheckHorizo​​ntalExtensibilityToRight(點[] curvePoints,點角) { 返回curvePoints.Any(CP => cp.Y < corner.Y & & cp.X < corner.X); }

0

試試這個例子,看看你是否能適應它爲您解決問題...

using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication 
{ 
    public partial class Form1 : Form 
    { 
     private struct LineSegment 
     { 
      private PointF _a, _b; 

      public PointF A { get { return _a; } } 
      public PointF B { get { return _b; } } 

      public LineSegment(PointF a, PointF b) 
      { 
       _a = a; _b = b; 
      } 

      public float GetLengthSquared() 
      { 
       var dx = _a.X - _b.X; 
       var dy = _a.Y - _b.Y; 
       return dx * dx + dy * dy; 
      } 

      public bool RectContains(PointF a) 
      { 
       var x = a.X; 
       var y = a.Y; 
       var x1 = _a.X; 
       var y1 = _a.Y; 
       var x2 = _b.X; 
       var y2 = _b.Y; 
       return (x1 < x2 ? x1 <= x && x2 >= x : x2 <= x && x1 >= x) && (y1 < y2 ? y1 <= y && y2 >= y : y2 <= y && y1 >= y); 
      } 

      public bool ExtendToIntersectWith(LineSegment b) 
      { 
       var x1 = _a.X; 
       var y1 = _a.Y; 
       var x2 = _b.X; 
       var y2 = _b.Y; 

       var x3 = b._a.X; 
       var y3 = b._a.Y; 
       var x4 = b._b.X; 
       var y4 = b._b.Y; 

       var a1 = y2 - y1; 
       var b1 = x1 - x2; 
       var c1 = x1 * y2 - x2 * y1; 

       var a2 = y4 - y3; 
       var b2 = x3 - x4; 
       var c2 = x3 * y4 - x4 * y3; 

       var d = a1 * b2 - b1 * a2; 
       if (d == 0) 
        return false; 

       var x = (c1 * b2 - b1 * c2)/d; 
       var y = (a1 * c2 - c1 * a2)/d; 
       var p = new PointF(x, y); 

       if (b.RectContains(p) && !RectContains(p)) 
       { 
        if (new LineSegment(_a, p).GetLengthSquared() < new LineSegment(_b, p).GetLengthSquared()) 
         _a = p; 
        else 
         _b = p; 
        return true; 
       } 

       return false; 
      } 
     } 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Paint(object sender, PaintEventArgs e) 
     { 
      PointF[] curvePoints = 
      { 
       /* 
       new PointF(50.0F, 50.0F), 
       new PointF(100.0F, 25.0F), 
       new PointF(200.0F, 5.0F), 
       new PointF(250.0F, 50.0F), 
       new PointF(300.0F, 100.0F), 
       new PointF(350.0F, 200.0F), 
       new PointF(250.0F, 250.0F) 
       */ 
       new PointF(30F, 10F), 
       new PointF(60F, 10F), 
       new PointF(60F, 20F), 
       new PointF(90F, 20F), 
       new PointF(90F, 60F), 
       new PointF(10F, 60F), 
       new PointF(10F, 40F), 
       new PointF(30F, 40F), 
      }; 
      int n = curvePoints.Length; 
      LineSegment[] lineSegments = new LineSegment[n]; 
      int i = 0; 
      for (; i < n - 1; ++i) 
       lineSegments[i] = new LineSegment(curvePoints[i], curvePoints[i + 1]); 
      lineSegments[i] = new LineSegment(curvePoints[i], curvePoints[0]); 
      for (i = 0; i < n; ++i) 
       for (int j = 0; j < n; ++j) 
        lineSegments[i].ExtendToIntersectWith(lineSegments[j]); 
      for (i = 0; i < n; ++i) 
      { 
       var lineSegment = lineSegments[i]; 
       e.Graphics.DrawLine(Pens.Black, lineSegment.A, lineSegment.B); 
      } 
      //e.Graphics.DrawPolygon(Pens.Black, curvePoints); 
     } 
    } 
}