2011-09-09 17 views
2

PictureBoxにポリライン(1つ以上の線分で構成される連続線)を描きたい。PictureBoxでポリラインを描く

これでは、各セグメントの終点を指定し、各セグメントの距離を計算することで複数の行を作成することができます。

Sample

+0

を現在のポイント間の距離と、次を得るために、そのいくつかの簡単なピタゴラスの数学尋ねた画像は最高です。人々は誤解するのが好きです! – Bitterblue

答えて

1

あなたがピクチャボックスにこれをしたい場合は、最も簡単な方法はPictureBoxから独自のコントロールを継承し、ダウンのPictureBoxの上に、マウスのエンドポイントを追加するための機能を提供することです。

次に、マウスクリックの位置をリストに格納し、OnPaintを上書きしてエンドポイント(4x4四角を選択)と各エンドポイント間の線を描画します。これは、基本的なコードです:

public class EndPointPictureBox : PictureBox 
{ 
    private List<PointF> points = new List<PointF>(); 
    public EndPointPictureBox() 
    { 
    } 

    protected override void OnMouseDown(MouseEventArgs e) 
    { 
     points.Add(new PointF(e.X,e.Y)); 
     base.OnMouseDown(e); 
     this.Invalidate(); 
    } 

    protected override void OnPaint(PaintEventArgs pe) 
    { 
     base.OnPaint(pe); 

     Graphics g = pe.Graphics; 
     foreach(var point in points) 
      g.DrawRectangle(Pens.Black,point.X-2.0f,point.Y-2.0f,4.0f,4.0f); 
     if(points.Count>1) 
      g.DrawLines(Pens.Black,points.ToArray()); 

    } 
} 

あなたは今だけのPictureBoxのようなフォームにこれを追加し、通常の方法でその中に入るためにあなたのimaggeを選択することができます。

ピクチャボックス内で数回クリックすると、サンプル画像のようにエンドポイントが描画されます。ここに私のマシンからの例です:

Example endpoints

次に、あなたの次の要件は、エンドポイント間の距離を取得します。これは、隣の隣人への参照を持つEndPointを表すクラスを追加することによって行うことができます。

public class EndPoint 
{ 
    public EndPoint(int index, List<PointF> points) 
    { 
     this.Position = points[index]; 
     if (index < points.Count - 1) 
      this.Next = points[index + 1]; 
    } 
    public PointF Position { get; private set; } 
    public PointF Next { get; private set; } 

    public double GetDistanceToNext() 
    { 
     if(this.Next == PointF.Empty) 
      return 0; 

     var xDiff = this.Position.X - Next.X; 
     var yDiff = this.Position.Y - Next.Y; 

     return Math.Abs(Math.Sqrt((xDiff*xDiff) + (yDiff*yDiff))); 
    } 
} 

そして、あなたはこれらのリスト取得するために、あなたの新しいピクチャボックスにメソッドを追加することができます:ナイスリー

public List<EndPoint> GetEndPoints() 
{ 
    var list = new List<EndPoint>(); 
    for(var i=0;i<points.Count;i++) 
     list.Add(new EndPoint(i,points)); 
    return list; 
} 
+0

ありがとう....私はそれを持っている! – cooldj

+0

私は各線分の中央に計算された距離を描きたいなら、どうすればいいのですか?このように... http://i.stack.imgur.com/nPBpx.png – cooldj

関連する問題