Advertisement
gigi78

from square to ellipse rotating simple animation

Jun 15th, 2021 (edited)
922
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.17 KB | None | 0 0
  1. orts System.Drawing.Drawing2D
  2. Imports System.Math
  3.  
  4. Public Class Form1
  5.  
  6.     Private boxes As New List(Of roundedBox)
  7.     Private bg As BufferedGraphics
  8.     Private contesto As New BufferedGraphicsContext
  9.     Private WithEvents renderLoop As New Timer
  10.  
  11.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  12.  
  13.         With Me
  14.             .Size = New Size(800, 800)
  15.             .BackColor = Color.White
  16.         End With
  17.  
  18.         With renderLoop
  19.             .Interval = 10
  20.             .Enabled = True
  21.         End With
  22.  
  23.         contesto.MaximumBuffer = Me.Size
  24.         bg = contesto.Allocate(Me.CreateGraphics, Me.DisplayRectangle)
  25.         bg.Graphics.Transform = New Matrix(1, 0, 0, -1, Me.Width / 2, Me.Height / 2)
  26.         bg.Graphics.SmoothingMode = SmoothingMode.AntiAlias
  27.  
  28.         For i As Double = 10 To 180 Step 10
  29.             boxes.Add(New roundedBox(New Point(0, 0), 400, 400, i))
  30.         Next
  31.  
  32.     End Sub
  33.  
  34.     Private Sub TrenderLoop_Tick(sender As Object, e As EventArgs) Handles renderLoop.Tick
  35.  
  36.         bg.Graphics.Clear(Me.BackColor)
  37.  
  38.         For Each r As roundedBox In boxes
  39.             r.disegna(bg.Graphics)
  40.         Next
  41.  
  42.         bg.Render(Me.CreateGraphics)
  43.  
  44.     End Sub
  45.  
  46. End Class
  47.  
  48. Public Class roundedBox
  49.  
  50.     Private origine As Point
  51.     Private altezza As Integer
  52.     Private larghezza As Integer
  53.     Private gradi As Double
  54.     Private percorso As New GraphicsPath
  55.     Public rotazione As Double
  56.     Private Const rad As Double = PI / 180
  57.  
  58.     Public Sub New(p As Point, width As Integer, height As Integer, rot As Double)
  59.  
  60.         origine = p
  61.         larghezza = width
  62.         altezza = height
  63.         rotazione = rot
  64.         trasforma()
  65.         gradi = 0
  66.  
  67.     End Sub
  68.  
  69.     Public Sub trasforma()
  70.  
  71.         Dim coefficenteDiRotazione As Double
  72.  
  73.         If gradi < 180 Then
  74.             coefficenteDiRotazione = (Sin(gradi * rad))
  75.             altezza = (400 - (320 * Sin(gradi * rad)))
  76.             gradi += 0.5
  77.         ElseIf gradi = 180 Then
  78.             gradi = 0
  79.         End If
  80.  
  81.         coefficenteDiRotazione *= rotazione
  82.  
  83.         Dim fX As Double = ((larghezza / 2) * Sin(gradi * rad) + 1) * 2
  84.         Dim fY As Double = ((altezza / 2) * Sin(gradi * rad) + 1) * 2
  85.  
  86.         percorso.Reset()
  87.         percorso.AddArc(New Rectangle(origine.X - larghezza / 2, origine.Y + (altezza / 2), fX, fY), 90, 90)
  88.         percorso.AddArc(New Rectangle(origine.X - larghezza / 2, (origine.Y - (altezza / 2)) + fY, fX, fY), 180, 90)
  89.         percorso.AddArc(New Rectangle((origine.X + (larghezza / 2) - fX), (origine.Y - (altezza / 2) + fY), fX, fY), 270, 90)
  90.         percorso.AddArc(New Rectangle((origine.X + (larghezza / 2) - fX), origine.Y + (altezza / 2), fX, fY), 360, 90)
  91.         percorso.CloseFigure()
  92.  
  93.         percorso.Transform(New Matrix(1, 0, 0, 1, 0, -fY))
  94.  
  95.         percorso.Transform(New Matrix(Cos(coefficenteDiRotazione * rad), Sin(coefficenteDiRotazione * rad), -Sin(coefficenteDiRotazione * rad), Cos(coefficenteDiRotazione * rad), 0, 0))
  96.  
  97.     End Sub
  98.  
  99.     Public Sub disegna(g As Graphics)
  100.  
  101.         trasforma()
  102.         g.DrawPath(New Pen(Color.Black), percorso)
  103.  
  104.     End Sub
  105.  
  106. End Class
  107.  
  108.  
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement