반응형

베지어 스플라인 곡선을 그려보자.

 

Node2D를 생성하고 스크립트를 추가한다.

 

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
48
using Godot;
 
public partial class test : Node2D
{    
    private Curve2D pos;
    
    private Vector2 position0;
    private Vector2 position1;
    private Vector2 position2;
    private Vector2 position3;
    private Vector2 position4;
 
    private Vector2 control0;
    private Vector2 control1;
 
    public override void _Ready()
    {
        pos = new Curve2D();
 
        position0 = new Vector2(200200);
        position1 = new Vector2(400400);
        position2 = new Vector2(600200);
        position3 = new Vector2(800400);
        position4 = new Vector2(1000200);
 
        control0 = new Vector2(-1000);
        control1 = new Vector2(1000);
 
        BezierSpline(position0, position1, position2, position3,
            position4, control0, control1);
    }
 
    public override void _Draw()
    {
        //DrawPolyline(pos.GetBakedPoints(), Colors.Red, 5);
        DrawPolyline(pos.Tessellate(), Colors.Red, 5);
    }
 
    private void BezierSpline(Vector2 p0, Vector2 p1, Vector2 p2,
        Vector2 p3, Vector2 p4, Vector2 c0, Vector2 c1)
    {
        pos.AddPoint(p0, c0, c1);
        pos.AddPoint(p1, c0, c1);
        pos.AddPoint(p2, c0, c1);
        pos.AddPoint(p3, c0, c1);
        pos.AddPoint(p4, c0, c1);
    }
}
 

 

 

위와 같이 코드를 작성하고 실행한다.

_Draw()는 처음 한 번만 호출된다. 스플라인이 변경되거나 _Draw()를 다시 호출할 필요가 있다면 QueueRedraw()를 사용한다.

 

각 포인트에서 컨트롤이 적용된 스플라인이 표시된다.

 

※ 참고

Custom drawing in 2D

Beziers, curves, and paths

About Spline Interpolation

 

반응형
Posted by J-sean
:
반응형

베지어 곡선을 계산하고 적용해 보자.

 

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
48
49
50
51
52
53
54
55
56
57
58
59
#import numpy as np
import bezier
import pygame
 
pygame.init()
pygame.display.set_caption("Super fun game development")
screen = pygame.display.set_mode((640480))
clock = pygame.time.Clock()
running = True
 
#nodes = np.asfortranarray([
#    [0.0, 640.0*2/4, 640.0*3/4, 640.0],
#    [0.0, 960.0, -960.0, 960.0],
#    ])
#curve = bezier.Curve(nodes, degree=3)
 
#nodes = np.asfortranarray([
#    [0.0, 640.0*2/4, 640.0*3/4, 640.0],
#    [0.0, 960.0, -960.0, 960.0],
#    ])
 
#nodes = np.array([
#    [0.0, 640.0*2/4, 640.0*3/4, 640.0],
#    [0.0, 960.0, -960.0, 960.0],
#    ])
 
nodes = [
    [0.0640.0*2/4640.0*3/4640.0],
    [0.0960.0-960.0960.0],
    ]
# 곡선 위 노드 행렬. 행은 차원, 열은 노드의 좌표를 나타낸다.
# (0, 0), (640*2/4, 960), (640*3/4, -960), (640, 960)
 
curve = bezier.Curve.from_nodes(nodes)
# 베지어 곡선을 생성한다. 차수는 node로부터 계산한다.
 
index = 0.0
 
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN and event.key == pygame.K_ESCAPE:
            running = False
 
    screen.fill("black")
 
    index += 0.005
    if index > 1:
        index = 0.0
 
    coord = curve.evaluate(index)
    # 베지어 곡선 위 index 지점 좌표를 계산한다. index는 1을 넘을 수 있다.
    pygame.draw.circle(screen, "yellow", (coord[0][0], coord[1][0]), 100)
    pygame.display.flip()
 
    clock.tick(60)
 
pygame.quit()
 

 

 

베지어 곡선을 따라 원이 이동한다.

 

반응형
Posted by J-sean
: