VB2010 面積算出
panelを用いた図形の作成を行っております。
コード
Public Class Form1
Dim p1 As Integer 'Panel1のカウンター
Dim p2 As Integer 'Panel2のカウンター
Dim p3 As Integer 'Panel3のカウンター
Dim p1size As Single 'Panel1の面積
Dim p2size As Single 'Panel2の面積
Dim p3size As Single 'Panel3の面積
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Dim p1clnt As Drawing.Size = Panel1.ClientSize
Dim p2clnt As Drawing.Size = Panel2.ClientSize
Dim p3clnt As Drawing.Size = Panel3.ClientSize
p1size = p1clnt.Width * p1clnt.Height
p2size = p2clnt.Width * p2clnt.Height
p3size = p3clnt.Width * p3clnt.Height
Panel4.AllowDrop = True
End Sub
Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown, Panel2.MouseDown, Panel3.MouseDown
sender.DoDragDrop(sender, DragDropEffects.Move)
End Sub
Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop
Dim srsPnl As Panel = e.Data.GetData(GetType(Panel))
Dim dstPnl As New Panel
dstPnl.Size = srsPnl.Size
Dim dropp1 As Integer = srsPnl.Size.Width * srsPnl.Size.Height
If dropp1 = p1size Then
p1 += 1
ElseIf dropp1 = p2size Then
p2 += 1
ElseIf dropp1 = p3size Then
p3 += 1
End If
dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y)
dstPnl.BackColor = srsPnl.BackColor
AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown
AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove
Panel4.Controls.Add(dstPnl)
End Sub
Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter
If e.Data.GetDataPresent(GetType(Panel)) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private previousPos As Point
Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
previousPos = CursorPosition()
End Sub
Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim nowPos As Point = CursorPosition()
DirectCast(sender, Panel).Left += nowPos.X - previousPos.X
DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y
Console.WriteLine(nowPos.X & "-" & previousPos.X)
previousPos = nowPos
End If
End Sub
Function CursorPosition() As Point
Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10)
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ans As Single
ans = (p1 * p1size + p2 * p2size + p3 * p3size) * 0.0001
TextBox1.Text = ans
End Sub
End Class
上記のコードは、GroupBox内のpanelをpanel4へ移動させて図形を作成するものです。
移動させたpanelの数を数えて、全体の面積を算出させているのですが、
作成させた図形の右側1/4の面積だけ(左側1/4,下側1/4といった風に)の面積を算出させることは可能でしょうか?
画像
※図形は任意
重複なし(panel同士)
1m2 (青) 3枚
0.25m2 (赤) 5枚
0.09m2 (黄) 5枚
計 4.7m2
となっています。
この作成した図形の緑色で着色した部分の面積を出したいと考えております。
もしお時間等ありましたら、お力添えをいただけると嬉しく思います。
どうかよろしくお願いします。