プロが教えるわが家の防犯対策術!

こんばんは。
ピクチャーボックスの中に、n角形を描くプログラムを考えているのですが、どうしたらいいか分からなくなってしまったので質問させていただきますm(_ _;)m

nの値はテキストボックスを作ってそこに値を入れ、実行ボタンを押すと描ける、というものにしようと思っています。

多角形の書き方をいくつか考えてみたのですが、

1.360度をnで割って放射線を描き、それをつなぐような方法

2.外周の長さを計算しnで割る。―(1)
  内角の和を計算し、nで割る。―(2)
  (1)の長さの線を引き、その線から(2)の角度だけ振った方向に(1)の長さの線を描く…という作業をFor nextでn回繰り返す…

という感じです。
そもそもVBで角度指定ってできたかな?座標指定しかできなかったような…と思い、検索もしてみたのですが、キーワードが悪いのか、参考になるページが出てこなくて、困ってしまいました。

多角形をかくにはどういうプログラムにしたらよいのでしょうか?
学校の授業や教科書にあることだけでは知識が足りないので、お力を貸してください。
よろしくお願いしますm(_ _)m

A 回答 (3件)

一部訂正


If r <= 0 Then
Throw New System.ArgumentException("半径は正の数値を指定してください")
ElseIf n < 3 Then
    • good
    • 0
この回答へのお礼

おへんじがおそくなってしまいすみません.
ありがとうございました.

お礼日時:2008/03/07 15:39

'VB.NET用。

VB6/VBA用を考えるのは面倒なので却下。今後もその予定なし
'例外処理を行うべき位置に自信なしs

Option Explicit On
Option Strict On
Option Compare Binary
Option Infer Off

Public Class Q3785078A
Inherits System.Windows.Forms.Form
Private PictureBox1 As System.Windows.Forms.PictureBox
Private Button1 As System.Windows.Forms.Button
Private Textbox1 As System.Windows.Forms.TextBox
Private PointFArray As System.Drawing.PointF() = {New System.Drawing.PointF(0,0),New System.Drawing.PointF(0,0),New System.Drawing.PointF(0,0)}
Sub New()

Me.Size = New System.Drawing.Size(800,600)
PictureBox1 = New System.Windows.Forms.PictureBox()
Textbox1 = New System.Windows.Forms.Textbox()
Button1 = New System.Windows.Forms.Button()


PictureBox1.Width = 600
PictureBox1.Height = 500
PictureBox1.Left = 100
PictureBox1.Top = 0

PictureBox1.BackColor = System.Drawing.Color.White

TextBox1.Width = 100
TextBox1.Height = 20
TextBox1.Left = 500
TextBox1.Top = 530

Button1.Width = 100
Button1.Height = 20
Button1.Left = 600
Button1.Top = 530

AddHandler Button1.Click, AddressOf Button1_Click
AddHandler PictureBox1.Paint, AddressOf PictureBox1_Paint

Me.Controls.Add(PictureBox1)
Me.Controls.Add(TextBox1)
Me.Controls.Add(Button1)

End Sub


Private Sub CalculatePoligonArray(r As double,n As Integer)
If r <= 0 Then
ElseIf n < 3 Then
Throw New System.ArgumentException("3以上の整数を指定してください")
End If
ReDim PointFArray(n-1)


For i As Integer = 0 To n - 1
System.Diagnostics.Debug.Print("DoubleからSingleへの変換がSystem.Convert.ToSingle経由で行われました。")
System.Diagnostics.Debug.Print(System.Convert.ToSingle(r * System.Math.Cos(2 * i * System.Math.PI / n )).ToString())
System.Diagnostics.Debug.Print(System.Convert.ToSingle(r * System.Math.Sin(2 * i * System.Math.PI / n )).ToString())
PointFArray(i) = New System.Drawing.PointF(System.Convert.ToSingle(r + r * System.Math.Cos(2 * i * System.Math.PI / n )),System.Convert.ToSingle(r + r * System.Math.Sin(2 * i * System.Math.PI / n)))

Next i

End Sub


Private Sub PictureBox1_Paint(sender As Object ,e As System.Windows.Forms.PaintEventArgs)
e.Graphics.DrawPolygon(New System.Drawing.Pen(System.Drawing.Color.Black),PointFArray)
e.Graphics.Flush
End Sub

Private Sub Button1_Click(sender As Object ,e As System.EventArgs)

Button1.Enabled = False
Try
CalculatePoligonArray(200,Integer.Parse(TextBox1.Text))
Catch err As System.FormatException
System.Diagnostics.Debug.Print(err.ToString)
Catch err As System.ArgumentException
System.Diagnostics.Debug.Print(err.ToString)
Finally

End Try

PictureBox1.Refresh
Button1.Enabled = True
End Sub

End Class

Public Class himajin100000

Shared Sub Main(args As String())

Dim Form1 As Q3785078A
Form1 = New Q3785078A()
Form1.ShowDialog

End Sub

End Class
    • good
    • 0

>学校の授業や教科書にあることだけでは知識が足りないので


小学生または中学生ですか?
高校生以上であれば、三角関数を使用して簡単にできるはず
ですが?

この回答への補足

高校です.
三角関数という発想は全くありませんでした…お恥ずかしい限りです...
でもどのように使用したらいいのか考えたのですが,長さを求めることに使用できるということしか思いつきません…
もう少しアドバイスを頂けると幸いです.よろしくおねがいします.

補足日時:2008/02/17 22:14
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!