2015-04-08 89 views
-1

說我在畫布上畫了一條線。我需要簡單地切割/分割線條,使其有兩條線。如何將畫布上劃的線分成兩部分

Line line = new Line(); 
SolidColorBrush brush = new SolidColorBrush(); 
brush.Color = colorPicker2.SelectedColor; 
line.Stroke = brush; 
line.StrokeThickness = SliderThickness.Value; 
line.X1 = currentPoint.X; 
line.Y1 = currentPoint.Y; 
line.X2 = e.GetPosition(DrawPanel).X; 
line.Y2 = e.GetPosition(DrawPanel).Y; 
currentPoint = e.GetPosition(DrawPanel); 
DrawPanel.Children.Add(line); 

對不起,我不是特定的。用戶將點擊該線的一個點,該線將在該點被切割成兩塊。然後用戶將拖動或移動該線的每個部分。

+1

如何U想砍呢?即使你在中間切割它,你也不會看到任何區別,直到其中一個的起點不同於其他終點。 – Muds

+0

我不確定問題是什麼。你能更詳細地解釋你想做什麼,或者簡單地用油漆畫出來嗎? – adhie

回答

0

試試這個:

var middleX = (line.X1 + line.X2)/2; 
var middleY = (line.Y1 + line.Y2)/2; 

Line line1 = new Line(); 
line1.X1 = line.X1; 
line1.Y1 = line.Y1; 
line1.X2 = middleX; 
line1.Y2 = middleY; 

Line line2 = new Line(); 
line2.X1 = middleX; 
line2.Y1 = middleY; 
line2.X2 = line.X2; 
line2.Y2 = line.Y2; 
+0

你試過嗎?這在ui上看起來有兩條不同的線嗎?我認爲op不清楚他現在在找什麼.. – Muds

+0

您可以更改其中一條線的位置,它看起來像2條不同的線。我認爲這不是這個問題的目的。 –

+0

這就是我說op不清楚.. – Muds

0

試試這個:

  Line line = new Line(); 
      SolidColorBrush brush = new SolidColorBrush(); 
      brush.Color = colorPicker2.SelectedColor; 
      line.Stroke = brush; 
      line.StrokeThickness = SliderThickness.Value; 
      line.X1 = currentPoint.X; 
      line.Y1 = currentPoint.Y; 
      line.X2 = e.GetPosition(DrawPanel).X; 
      line.Y2 = e.GetPosition(DrawPanel).Y; 
      currentPoint = e.GetPosition(DrawPanel); 
      DrawPanel.Children.Add(line); 

      Point middlePoint = ((line.X1 + line.X2)/2, (line.Y1 + line.Y2)/2) 

      Line newFirst = new Line(); 
      newFirst.X1 = line.X1; 
      newFirst.Y1 = line.Y1; 
      newFirst.X2 = middlePoint.X; 
      newFirst.Y2 = middlePoint.Y; 
      newFirst.Stroke = brush; 
      newFirst.StrokeThickness = SliderThickness.Value; 

      Line newSecond = new Line(); 
      newSecond.X1 = middlePoint.X; 
      newSecond.Y1 = middlePoint.Y; 
      newSecond.X2 = line.X2; 
      newSecond.Y2 = line.Y2; 
      newSecond.Stroke = brush; 
      newSecond.StrokeThickness = SliderThickness.Value; 

      DrawPanel.Children.Remove(line); 
      DrawPanel.Children.Add(newFirst); 
      DrawPanel.Children.Add(newSecond); 
+0

這與ans的發佈有何不同? – Muds

+0

1.當我發佈的答案我沒有看到你的,頁面wasnt刷新:) 2.其他答案不處理用戶界面。它會根據給定的點創建兩條邏輯線,但它不會將其作爲兩條不同的線顯示在畫布上。 與我的答案,如果你會改變例如「newFirst」的顏色,你會看到一條線被切成一半顏色 –

+0

想法是不犯這樣的錯誤,並讓所有用戶通過兩個相同的答案。通常頁面動態變化,以便我們可以避免這個錯誤..對於你的第二點,請確保你不要餵食OP,我們必須始終啓動思考過程,而不是提供所有預先烹飪的食物......只是爲了讓你知道我沒有發佈答案 – Muds