楕円形の回転          <TOP>


楕円形を描画し回転させてみます。

EditBoxに直接角度を入力するとその角度で楕円を描画します。自動にチェックを入れると0〜350°まで自動的に回転させています。

フォームをリサイズすると楕円の大きさも変わります。

左:角度の間隔を設定して自動的に回転させています。    右図:「自動」のチェックを外して指定の角度で回転させています。

 

描画スピードを調整し状況を確認しています。リストは楕円の回転(U)

 

 

'================================================================
'= 楕円形の回転(T)
'=   (EllipseRotate.bas)
'================================================================
#include "Windows.bi"

Var Shared Edit1 As Object
Var Shared Edit2 As Object
Var Shared Check1 As Object
Var Shared Timer1 As Object

Edit1.Attach GetDlgItem("EDit1") : Edit1.SetFontSize 14
Edit2.Attach GetDlgItem("EDit2") : Edit2.SetFontSize 14
Check1.Attach GetDlgItem("Check1") : Check1.SetFontSize 14
Timer1.Attach GetDlgItem("Timer1")

Var Shared mCx As Single
Var Shared mCy As Single
Var Shared mWidth As Single
Var Shared mHeight As Single
Var Shared eStep As Integer

#define PI 3.14159265

'================================================================
'=
'================================================================
Declare Sub DrawEllipse(cx As Single, cy As Single, wid As Single, hgt As Single, Angle As Single)
Sub DrawEllipse(cx As Single, cy As Single, wid As Single, hgt As Single, Angle As Single)
    Var Sin_Angle As Single
    Var Cos_Angle As Single
    Var theta As Single
    Var dtheta As Single
    Var X As Single
    Var Y As Single
    Var RX As Single
    Var RY As Single

    Cls

    Angle = Angle * PI / 180
    Sin_Angle = Sin(Angle)
    Cos_Angle = Cos(Angle)

    theta = 0
    dtheta = 2 * PI / 50

    '最初の位置
    X = wid * Cos(theta)
    Y = hgt * Sin(theta)

    RX = cx + X * Cos_Angle + Y * Sin_Angle
    RY = cy - X * Sin_Angle + Y * Cos_Angle
    Pset(RX, RY)

    Do While theta < 2 * PI
        theta = theta + dtheta
        X = wid * Cos(theta)
        Y = hgt * Sin(theta)

        RX = cx + X * Cos_Angle + Y * Sin_Angle
        RY = cy - X * Sin_Angle + Y * Cos_Angle

        Line - (RX, RY)
    Loop
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Timer1.SetInterval 5
    Timer1.Enable 0
    Edit2.ShowWindow 0
End Sub

'================================================================
'=
'================================================================
Declare Sub Timer1_Timer edecl ()
Sub Timer1_Timer()
    Var Angle As Single

    On Error Goto *Er_Trap

    Angle = Val(Edit1.GetWindowText)
    eStep = Val(Edit2.GetWindowText)
    Angle = Angle + eStep
    If Angle > 350 Then Angle = 0
    Edit1.SetWindowText Str$(Angle)
    Exit Sub

*Er_Trap
    Resume Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Edit1_Change edecl ()
Sub Edit1_Change()
    Var Angle As Single

    On Error Goto *Er_Trap
    Angle = Val(Edit1.GetWindowText)
    On Error Goto 0

    DrawEllipse mCx, mCy, mWidth, mHeight, Angle
    Exit Sub

*Er_Trap
    Resume Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Edit1_SetFocus edecl ()
Sub Edit1_SetFocus()
    Timer1.Enable 0
    Edit2.ShowWindow 0
    Check1.SetCheck 0
End Sub

'================================================================
'=
'================================================================
Declare Sub Check1_on edecl ()
Sub Check1_on()
    If CHeck1.GetCheck = 0 Then
        Timer1.Enable 0
        Edit2.ShowWindow 0
        eStep = 0
    Else
        Timer1.Enable -1
        Edit2.ShowWindow -1
    End If
    SetFocus
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_ReSize edecl ()
Sub MainForm_ReSize()
    mCx = GetWidth / 2
    mCy = GetHeight / 2

    If GetWidth > GetHeight Then
        mWidth = GetHeight * 0.45
        mHeight = GetWidth * 0.25
    Else
        mWidth = GetWidth * 0.45
        mHeight = GetHeight * 0.25
    End If

    Edit1_Change
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End

'================================================================
'= 楕円形の回転(U)
'=    (EllipseRotate2.bas)
'================================================================
#include "Windows.bi"

Var Shared Text1 As Object
Var Shared Text2 As Object
Var Shared Text3 As Object
Var Shared Text4 As Object
Var Shared Edit1 As Object
Var Shared Edit2 As Object
Var Shared Check1 As Object
Var Shared Timer1 As Object

Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Text2.Attach GetDlgItem("Text2") : Text2.SetFontSize 14
Text3.Attach GetDlgItem("Text3") : Text3.SetFontSize 14
Text4.Attach GetDlgItem("Text4") : Text4.SetFontSize 14
Edit1.Attach GetDlgItem("EDit1") : Edit1.SetFontSize 14
Edit2.Attach GetDlgItem("EDit2") : Edit2.SetFontSize 14
Check1.Attach GetDlgItem("Check1") : Check1.SetFontSize 14
Timer1.Attach GetDlgItem("Timer1")

Var Shared mCx As Single
Var Shared mCy As Single
Var Shared mWidth As Single
Var Shared mHeight As Single
Var Shared eStep As Integer

#define PI 3.14159265

'================================================================
'= cx:楕円中心(x) cy:楕円中心(y) wid:フォーム幅 hgt:フォーム高さ angle:角度
'================================================================
Declare Sub DrawEllipse(cx As Single, cy As Single, wid As Single, hgt As Single, angle As Single)
Sub DrawEllipse(cx As Single, cy As Single, wid As Single, hgt As Single, angle As Single)
    Var sin_angle As Single                      'サイン
    Var cos_angle As Single                      'コサイン
    Var theta As Single                          
    Var dtheta As Single                         
    Var X As Single
    Var Y As Single
    Var RX As Single                   	 '楕円描画の点(X)
    Var RY As Single                             '楕円描画の点(Y)

    Cls

    angle = angle * PI / 180
    sin_angle = Sin(angle)
    cos_angle = Cos(angle)

    theta = 0
    dtheta = 2 * PI / 50                         '楕円を描くライン数(50の線で描画)

    '最初の位置
    X = wid * Cos(theta)
    Y = hgt * Sin(theta)

    RX = cx + X * cos_angle + Y * sin_angle
    RY = cy - X * sin_angle + Y * cos_angle

    SetDrawWidth 3                               '描画する太さ(3)
    Pset(RX, RY), 0                              '楕円描画の始まり(黒の点)
    SetDrawWidth 0                               '描画する太さ(通常に戻す)
    Text1.SetWindowText Str$(Int(RX))            '点のX位置
    Text2.SetWindowText Str$(Int(RY))            '点のY位置


    Do While theta < 2 * PI
        theta = theta + dtheta
        X = wid * Cos(theta)
        Y = hgt * Sin(theta)

        RX = cx + X * cos_angle + Y * sin_angle
        RY = cy - X * sin_angle + Y * cos_angle

        Line - (RX, RY), , 5
        Text3.SetWindowText Str$(Int(RX))
        Text4.SetWindowText Str$(Int(RY))
        Wait 5
    Loop
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Timer1.SetInterVal 10
    Timer1.Enable 0
    Edit2.ShowWindow 0
End Sub

'================================================================
'=
'================================================================
Declare Sub Timer1_Timer edecl ()
Sub Timer1_Timer()
    Var angle As Single

    On Error GoTo *Er_Trap

    angle = Val(Edit1.GetWindowText)             '角度
    eStep = Val(Edit2.GetWindowText)             '角度のステップ
    angle = angle + eStep
    If angle > 350 Then angle = 0                '350°まで描画 → 0°
    Edit1.SetWindowText Str$(angle)
    Exit Sub

*Er_Trap
    Resume Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Edit1_Change edecl ()
Sub Edit1_Change()
    Var angle As Single

    On Error Goto *Er_Trap
    angle = Val(Edit1.GetWindowText)
    On Error GoTo 0

    DrawEllipse mCx, mCy, mWidth, mHeight, angle
    Exit Sub

*Er_Trap
    Resume Next
End Sub

'================================================================
'=
'================================================================
Declare Sub Edit1_SetFocus edecl ()
Sub Edit1_SetFocus()
    Timer1.Enable 0
    Edit2.ShowWindow 0
    Check1.SetCheck 0
End Sub

'================================================================
'=
'================================================================
Declare Sub Check1_on edecl ()
Sub Check1_on()
    If CHeck1.GetCheck = 0 Then
        Timer1.Enable 0
        Edit2.ShowWindow 0
        eStep = 0
    Else
        Timer1.Enable -1
        Edit2.ShowWindow -1
    End If
    SetFocus
End Sub

'================================================================
'=
'================================================================
Declare Sub MainForm_ReSize edecl ()
Sub MainForm_ReSize()
    mCx = GetWidth / 2
    mCy = GetHeight / 2

    If GetWidth > GetHeight Then
        mWidth = GetHeight * 0.45
        mHeight = GetWidth * 0.25
    Else
        mWidth = GetWidth * 0.45
        mHeight = GetHeight * 0.25
    End If

    Edit1_Change
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End