Tämä koodi muodostaa käyrän. Se kerää tietoa neljästä pisteestä ympärillään ja muodostaa niistä käyrän erään vaikean kaavan mukaan, jota en itse keksinyt.
Kaava on yleinen, ja varmasti hyödyllinen kaikille.
Kääntyy kaikilla vb.net kääntäjillä.
Public Class Form1
Inherits System.Windows.Forms.Form
Dim points(7) As Point
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(968, 798)
Me.Name = "Form1"
Me.Text = "Interpolation"
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
points(0) = New Point(0, 400)
points(1) = New Point(200, 300)
points(2) = New Point(300, 100)
points(3) = New Point(400, 250)
points(4) = New Point(500, 350)
points(5) = New Point(600, 100)
points(6) = New Point(700, 300)
points(7) = New Point(800, 200)
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
Dim last, uus As Point
last = points(0)
Dim i, a As Integer
For a = 0 To points.Length - 1
' Markkerit
e.Graphics.DrawEllipse(Pens.Red, points(a).X, points(a).Y, 10, 10)
' piirretään 100:aan kohtaan
For i = 0 To 100
' Jos a on menossa yli taulukkoindeksin, järjestetään jokaiseen tilanteeseen sopiva käsittelijä
If a = 0 Then
uus.Y = Cubic_Interpolate(Me.Height, Me.Height, points(a).Y, points(a + 1).Y, i / 100)
ElseIf a = 1 Then
uus.Y = Cubic_Interpolate(Me.Height, points(a - 1).Y, points(a).Y, points(a + 1).Y, i / 100)
ElseIf a = points.Length - 1 Then
uus.Y = Cubic_Interpolate(points(a - 2).Y, points(a - 1).Y, points(a).Y, Me.Height, i / 100)
Else
uus.Y = Cubic_Interpolate(points(a - 2).Y, points(a - 1).Y, points(a).Y, points(a + 1).Y, i / 100)
End If
uus.X = last.X + 1
e.Graphics.DrawLine(Pens.Black, last.X, last.Y, uus.X, uus.Y)
last = uus
Next
Next
End Sub
' interpolaation ydin. se ottaa 4 pistettä ja palauttaa kahden keskimmäisen pisteen välisen matkan kohdan korkeuden. x kertoo, mistä kohtaa väliä piste otetaan. x on väliltä 0 - 1.
' tätä kaavaa en ole siis itse keksinyt. Jos haluat tietää lisää googleta :D
Function Cubic_Interpolate(ByVal v0, ByVal v1, ByVal v2, ByVal v3, ByVal x) As Integer
Dim P As Integer = (v3 - v2) - (v0 - v1)
Dim Q As Integer = (v0 - v1) - P
Dim R As Integer = v2 - v0
Dim S As Integer = v1
Return P * x ^ 3 + Q * x ^ 2 + R * x + S
End Function
End ClassLöytyisikö exeä?
Löytyy. täältä
Hieano. Miten sä osaat?
Hieno käyrä. Ja eiköhän se aika varmasti oikean kaavan mukaan mene, kun se on pekin tekemä. :)
Hassu. En tosin keksi mihin tätä tarvitaan tätä kaavaa :P
Olisi kiva jos pisteitä voisi itse raahata ja katsoa miten eri sijainnit vaikuttaa toisiin pisteisiin
Käyttötarkoituksia ovat esimerkiksi perlinnoisen melukartan interpoloiminen; random maaston teko ja väriliu'ut. Muitakin sovelluskohteita keksii nopeasti.
Aihe on jo aika vanha, joten et voi enää vastata siihen.