旋转,移动,绘制和缩放有什么问题?

时间:2013-12-23 15:07:30

标签: vb.net rotation zoom move

' details of bitmap
Dim bm As New Bitmap(1000, 1000)
Dim graph As Graphics = Graphics.FromImage(bm)
'when mouse down on pic draw values
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
  Dim rad As Double
  Dim angle As Integer
  rad = angle * System.Math.PI / 180
  'equations for rotate lines

  Dim x As Integer = System.Math.Sin(rad) - System.Math.Cos(rad)
  Dim y As Integer = System.Math.Sin(rad) + System.Math.Cos(rad)
  Dim z As Integer = System.Math.Cos(rad) + System.Math.Sin(rad)

  graph.DrawLine(New Pen(Color.Orange, 1), 150 + e.X, 210 + e.Y, y + 150 - e.X, (x) + 210 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 160 + e.X, 220 + e.Y, (y) + 160 - e.X, (x) + 220 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 170 + e.X, 230 + e.Y, y + 170 - e.X, (x) + 230 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 180 + e.X, 240 + e.Y, (y) + 180 - e.X, (x) + 240 - e.Y)

  graph.DrawLine(New Pen(Color.Orange, 1), 210 + e.X, 150 + e.Y, y + 210 - e.X, (x) + 150 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 220 + e.X, 160 + e.Y, (y) + 220 - e.X, (x) + 160 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 230 + e.X, 170 + e.Y, y + 230 - e.X, (x) + 170 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 240 + e.X, 180 + e.Y, (y) + 240 - e.X, (x) + 180 - e.Y)

  graph.DrawLine(New Pen(Color.Green, 1), 210 + e.X, 150 + e.Y, 150 + e.X, 210 + e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), 220 + e.X, 160 + e.Y, 160 + e.X, 220 + e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), 230 + e.X, 170 + e.Y, 170 + e.X, 230 + e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), 240 + e.X, 180 + e.Y, 180 + e.X, 240 + e.Y)

  graph.DrawLine(New Pen(Color.Green, 1), y + 210 - e.X, (x) + 150 - e.Y, y + 150 - e.X, (x) + 210 - e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), (y) + 220 - e.X, (x) + 160 - e.Y, (y) + 160 - e.X, (x) + 220 - e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), y + 230 - e.X, (x) + 170 - e.Y, y + 170 - e.X, (x) + 230 - e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), (y) + 240 - e.X, (x) + 180 - e.Y, (y) + 180 - e.X, (x) + 240 - e.Y)

  PictureBox1.Image = bm
End Sub

当鼠标移动旋转线但在这里我的要求想要使它没有拉伸并绘制对象并旋转:

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
  PictureBox1.Refresh()
  Dim graph As Graphics = PictureBox1.CreateGraphics
  Dim rad As Double
  Dim angle As Integer
  rad = angle * System.Math.PI / 180

  Dim x As Integer = System.Math.Sin(rad) - System.Math.Cos(rad)
  Dim y As Integer = System.Math.Sin(rad) + System.Math.Cos(rad)
  Dim z As Integer = System.Math.Cos(rad) + System.Math.Sin(rad)

  graph.DrawLine(New Pen(Color.Green, 1), e.X, e.Y, x + 200 - e.X, (y) + 400 - e.Y)
  graph.DrawLine(New Pen(Color.Red, 1), e.X, e.Y, (y) + 400 - e.X, (z) + 200 - e.Y)
  graph.DrawLine(New Pen(Color.Blue, 1), e.X, e.Y, z + 400 + e.X, x + 200 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 150 + e.X, 210 + e.Y, y + 150 - e.X, (x) + 210 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 160 + e.X, 220 + e.Y, (y) + 160 - e.X, (x) + 220 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 170 + e.X, 230 + e.Y, y + 170 - e.X, (x) + 230 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 180 + e.X, 240 + e.Y, (y) + 180 - e.X, (x) + 240 - e.Y)

  graph.DrawLine(New Pen(Color.Orange, 1), 210 + e.X, 150 + e.Y, y + 210 - e.X, (x) + 150 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 220 + e.X, 160 + e.Y, (y) + 220 - e.X, (x) + 160 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 230 + e.X, 170 + e.Y, y + 230 - e.X, (x) + 170 - e.Y)
  graph.DrawLine(New Pen(Color.Orange, 1), 240 + e.X, 180 + e.Y, (y) + 240 - e.X, (x) + 180 - e.Y)

  graph.DrawLine(New Pen(Color.Green, 1), 210 + e.X, 150 + e.Y, 150 + e.X, 210 + e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), 220 + e.X, 160 + e.Y, 160 + e.X, 220 + e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), 230 + e.X, 170 + e.Y, 170 + e.X, 230 + e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), 240 + e.X, 180 + e.Y, 180 + e.X, 240 + e.Y)

  graph.DrawLine(New Pen(Color.Green, 1), y + 210 - e.X, (x) + 150 - e.Y, y + 150 - e.X, (x) + 210 - e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), (y) + 220 - e.X, (x) + 160 - e.Y, (y) + 160 - e.X, (x) + 220 - e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), y + 230 - e.X, (x) + 170 - e.Y, y + 170 - e.X, (x) + 230 - e.Y)
  graph.DrawLine(New Pen(Color.Green, 1), (y) + 240 - e.X, (x) + 180 - e.Y, (y) + 180 - e.X, (x) + 240 - e.Y)
End Sub

enter image description here

1 个答案:

答案 0 :(得分:1)

复制/粘贴代码的方法是错误的。

graph.DrawLine(New Pen(Color.Orange, 1), 150 + e.X, 210 + e.Y, y + 150 - e.X, (x) + 210 - e.Y)
graph.DrawLine(New Pen(Color.Orange, 1), 160 + e.X, 220 + e.Y, (y) + 160 - e.X, (x) + 220 - e.Y)
graph.DrawLine(New Pen(Color.Orange, 1), 170 + e.X, 230 + e.Y, y + 170 - e.X, (x) + 230 - e.Y)
graph.DrawLine(New Pen(Color.Orange, 1), 180 + e.X, 240 + e.Y, (y) + 180 - e.X, (x) + 240 - e.Y)

95%的编程是关于阅读代码。因此,当您编写代码时,您需要确保尽可能少地编写代码,这样您以后花费的时间就会减少,或者其他人会花费更少的时间。

如果查看代码#1的墙,可以看到所有序列都以数字150或210开头。然后有一个+10增量。此外,将yx放在方括号中也没有意义。使用上面的代码,您的代码可以重写:

Sub DrawLines(g As Graphics, color As Color, offsetX As Integer, offsetY As Integer) 
  For i As Integer = 0 to 3
    Dim leftShift As Integer = offsetX + i*10
    Dim topShift As Integer = offsetY + i*10
    g.DrawLine(New Pen(color, 1),
               leftShift + e.X, topShift + e.Y,
               y + leftShift - e.X, x + topShift - e.Y)     
  Next
End Sub

然后这4个和其他4个相似的行变为:

DrawLines(graph, Color.Orange, 150, 210)
DrawLines(graph, Color.Orange, 210, 150)

现在,是不是更具可读性?是不是更容易发现这两行中的问题,而不是其他8行?请确保,在您开始深入研究问题之前,您的“桌子”上没有任何其他内容可能会让您失望。这个原则称为DRY。在这种情况下,代码质量会降低您找到解决方案的速度。当我们看两个相似的代码片段时,我们试图找出相似之处(当然)。差异越大 - 查看代码所花费的时间就越少。花点时间以类似于上面的方式重构,并在完成后回复。