summaryrefslogtreecommitdiffstats
path: root/virtual-programs/display/arc.folk
blob: e2412bf08d024b7dbf51d0e65e2e9ce1f64934e4 (plain)
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
# Example:
#   When $this has region /r/ {
#     lassign [region centroid $r] x y
#     Wish to draw an arc with x $x y $y start 0 arclen 1 thickness 3 radius 100 color green
#   }

Wish the GPU compiles pipeline "arc" {{vec2 center float start float arclen float radius float thickness vec4 color} {
    float r = radius + thickness;
    vec2 vertices[4] = vec2[4](
         center - r,
         vec2(center.x + r, center.y - r),
         vec2(center.x - r, center.y + r),
         center + r
    );
    return vertices[gl_VertexIndex];
} {
    #define M_TWO_PI 6.283185307179586
    start = clamp(start, 0, M_TWO_PI);
    arclen = clamp(arclen, 0, M_TWO_PI);

    float dist = length(gl_FragCoord.xy - center) - radius;
    float angle = atan(-(gl_FragCoord.y - center.y), gl_FragCoord.x - center.x);

    // Shift angle from [-pi, pi) to [0, 2*pi]
    angle = (angle < 0) ? (angle + M_TWO_PI) : angle;
    float end = start + arclen;

    return ((dist < thickness && dist > 0.0) &&
            ((end < M_TWO_PI && angle > start && angle < end) || 
            (end >= M_TWO_PI && (angle > start || angle < end-M_TWO_PI)))) ? color : vec4(0, 0, 0, 0);

}}

When /someone/ wishes to draw an arc with /...options/ {
    dict with options {
        Wish the GPU draws pipeline "arc" with arguments \
            [list [list $x $y] $start $arclen $radius $thickness [getColor $color]]
    }
}