Capt.Murasa 发表于 2014-8-25 19:00:25

【賣技向】用Visual Basic編時變向量場

本帖最后由 Capt.Murasa 于 2014-8-25 19:20 编辑

場函數F(x,y,t):
i分量: Cos(1.5 * y) * Sin(3 * t)
j分量: Sin(1.5 * x) * Cos(3 * t)
抱歉渣片質


http://v.youku.com/v_show/id_XNzYxNTY3ODMy.html
上次也發過了圖片...這次就加入了時間參數..
另外, 為了顯示,箭矢實際長度被統一
哂技能向,除此之外,無其他用意
最近的新人令我發現..........我真的是技窮了

完整程式碼如下:


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Math

'除form之外,還外加了1個timer,label
Form1
    Dim p1 As PointF
    Dim p2 As PointF
    Dim x As Double
    Dim y As Double
    Public time As Double
    Dim pen1 As New Pen(Color.Red, 1)
    Function Xdirect(Optional ByVal x As Double = 0, Optional ByVal y As Double = 0, Optional ByVal t As Double = 0) As Double
'此函式計算 i 分量值
       Dim s As Double
      s = Cos(1.5 * y) * Sin(3 * t)' i 分量
      Return s
    End Function
    Function Ydirect(Optional ByVal x As Double = 0, Optional ByVal y As Double = 0, Optional ByVal t As Double = 0) As Double
'此函式計算 j 分量值
      Dim s As Double
      s = Sin(1.5 * x) * Cos(3 * t) ' j 分量
      Return s
    End Function

    Sub Adjusting(ByVal a As Double, ByVal b As Double) '此副程式用作 調整箭矢長度
      Dim m As Double
      If (a = 0 And b = 0) Then
            y = 0 : x = 0
      Else
            m = Sqrt((a ^ 2) + (b ^ 2))
            x = 0.2 * a / m
            y = 0.2 * b / m
      End If
    End Sub

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
      Dim myArrow As New AdjustableArrowCap(3, 6) '
      Dim customArrow As CustomLineCap = myArrow
      Dim capPen As New Pen(Color.Black) '
      capPen.CustomStartCap = myArrow
      e.Graphics.DrawLine(pen1, 0, 450, 970, 450)
      e.Graphics.DrawLine(pen1, 450, 0, 450, 970)
      For i As Double = -5 To 5 Step 0.3
            For j As Double = -5 To 5 Step 0.3
                Adjusting(Xdirect(i, j, time), Ydirect(i, j, time))

                p1 = New PointF((i * 100 + 450), (450 - j * 100))
                p2 = New PointF((i + x) * 100 + 450, 450 - (j + y) * 100)
                e.Graphics.DrawLine(capPen, p2, p1)
            Next
      Next
    End Sub


    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      time += 0.05
      Label1.Text = time '顯示參數t值
   Me.Refresh()
    End Sub
   
    End Class


drzzm32 发表于 2014-8-25 19:05:20

本帖最后由 drzzm32 于 2014-8-25 19:06 编辑

喂,楼主,这个是算法编程哦。。。
最好给出部分代码。。。
尤其是绘制部分。。。因为很少有人意识到绘制的重要性

PS:等我电脑好了我准备把物理引擎给实现,模拟一个重力场。。。也是VB哦

ofz 发表于 2014-8-26 12:42:33

本帖最后由 ofz 于 2014-8-26 12:47 编辑

不错的说,我也来我也来~

先看结果:

在线演示:http://glsl.heroku.com/e#19414.1

然后是代码(其实在演示页面里就有):
// 以下是 glsl
#ifdef GL_ES
precision mediump float;
#endif

#define GRID 100.0

uniform float time;
uniform vec2 resolution;

void main( void ) {
      
      vec2 pos = (gl_FragCoord.xy - resolution) * 4.;
      vec2 src = floor(pos / GRID) * GRID + GRID / 2.;
      vec2 tgt = vec2(cos(1.5 * src.y) + sin(3. * time), sin(1.5 * src.x) + cos(3. * time)) * GRID / 2. + src;
      
      vec3 color = distance(pos, src) + distance(pos, tgt) - distance(src, tgt) < 0.1 ? vec3(1., 1., 1.) : vec3(0., 0., 0.);
      gl_FragColor = vec4(color, 1.0);

}



解放军 发表于 2015-1-2 08:26:17

agou663 发表于 2015-1-3 19:22:23

。抱歉。有点晕。但我还是努力盯着看着
页: [1]
查看完整版本: 【賣技向】用Visual Basic編時變向量場