Login Register
Frontpage Code library Pastebin

QuadraticLerp & Bezier-käyrä

Author: MaGetzUb
Added: 24. marraskuuta 2015 kello 12.17
Edited: 24. marraskuuta 2015 kello 12.21
Category: Matematiikka

Description

Tämä koodi sisältää QuadraticLerpin, ja funktion, jolla voi piirtää bezier käyriä helposti. Funktiot: - DrawBezier * Piirtää bezier käyrän * Parametreiksi annetaan - alkupiste (x1, y1) - loppupiste (x2, y2) - konfiguraatio piste (cx, cy) - tarkkuus (precision, vakiona 300) - QuadraticLerp * Palauttaa kolmesta annetusta arvosta interpoloidun arvon * Parametreiksi annetaan neliä arvoa - a - b - c - t (vaihe, tämä arvo on väliltä 0,0 ... 1,0)

Code

Select all
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
SCREEN 800, 600

x1 As Float = Rand(0, 800)
y1 As Float = Rand(0, 600)


x2 As Float = Rand(0, 800)
y2 As Float= Rand(0, 600)



Repeat 
    ClearText
    If KeyHit(28) Then
        x1 As Float = Rand(0, 800)
        y1 As Float = Rand(0, 600)
        
        x2 As Float = Rand(0, 800)
        y2 As Float= Rand(0, 600)
    EndIf 


    cx = MouseX()
    cy = MouseY()

    DrawBezier(x1, y1, x2, y2, cx, cy)


DrawScreen
Forever 

Function DrawBezier(x1 As Float, y1 As Float, x2 As Float, y2 As Float, cx As Float, cy As Float, precision As Integer = 300)
  
    ox As Float = x1
    oy As Float = y1
    For i = 0 To precision
        bezx As Float = QuadraticLerp(x1, cx, x2, i / Float(precision))
        bezy As Float = QuadraticLerp(y1, cy, y2, i / Float(precision))
        Line ox, oy, bezx, bezy
        ox = bezx
        oy = bezy
    Next i
EndFunction 

Function QuadraticLerp#(a As Float, b As Float, c As Float, t As Float)
    Return (1.0-t)*(1.0-t)*a + (1.0-t)*2*t*b + t*t*c
EndFunction 

Comments

No comments. You can be first!

Leave a comment

You must be logged in to comment.