Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active June 23, 2018 19:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toomasv/6176114cf830390bc46bca29ba2b029f to your computer and use it in GitHub Desktop.
Save toomasv/6176114cf830390bc46bca29ba2b029f to your computer and use it in GitHub Desktop.
Study of quadratic bezier curve
Red [
Author: "Toomas Vooglaid"
Date: 2018-03-14
Needs: 'View
]
system/view/auto-sync?: off
btw-points: func [pt1 pt2 rel][
pt2 - pt1 * rel + pt1
]
points: [50x200 100x100 200x200]
blk: copy [] i: -1
foreach point points [append blk reduce ['circle point 2 'text point + 5x-10 rejoin ["P" i: i + 1]]]
append blk compose [pen gray line (points) pen black text 110x190 "t = " line 10x210 250x210]
blk2: copy blk
append blk compose [
pen red line-width 2 curve (points)
pen green line-width 1 line (p1: btw-points points/1 points/2 25%) (p2: btw-points points/2 points/3 25%)
circle (p1) 1 circle (p2) 1
pen black text (p1 - 20x10) "Q0" text (p2 + 5x-10) "Q1"
circle (p3: btw-points p1 p2 25%) 2 text (p3) "B"
text 130x190 ".25"
]
append blk2 compose [
pen green l: line (points/1) (points/2)
c1: circle (points/1) 1 c2: circle (points/2) 1
pen black c3: circle (points/1) 2
t: text 130x190 ""
pen red line-width 2
crv: curve (points/1) (points/1) (points/1)
]
tick: 0.0
view/tight compose/deep/only [
b1: box 300x250
draw (blk)
b2: box 300x250
rate 25
on-time [
tick: tick + 1
r: tick % 100 / 100
l/2: btw-points points/1 points/2 r
l/3: btw-points points/2 points/3 r
c1/2: l/2
c2/2: l/3
c3/2: btw-points c1/2 c2/2 r
crv/3: c1/2 crv/4: c3/2
t/3: form r
show b2
]
draw (blk2)
at 350x10 text "Rate:" at 390x10 rate: field 40x24 "25"
at 440x10 button 50x24 "Stop" [
either b2/rate [
b2/rate: none face/text: "Go" show [face b2]
][
b2/rate: rate/data face/text: "Stop" show [face b2]
]
]
]
@dockimbel
Copy link

Great work as usual! 👍

Some possible minor simplifications:

  • You can use compose/deep/only to remove one level of block wrapping:
>> blk: []
== []
>> compose/deep [ draw [(blk)] ]
== [draw []]
>> compose/deep/only [ draw (blk) ]
== [draw []]
  • You should be using view lay instead of view/no-wait lay. The latter will not start an event loop, which works fine from the GUI console, because the console provides already a View event loop. Though, in other contexts (like compilation), this won't work. The standard way is to use view to create an event loop, then view/no-wait for additional windows to use the existing event loop and not create new ones.

@toomasv
Copy link
Author

toomasv commented Mar 15, 2018

@dockimbel Thank you! I'll implement these points right away.

@ifmihai
Copy link

ifmihai commented Apr 8, 2018

i tried to run the example, from REPL

do %demos/quadratic-bezier.red

but doesn't work in my case
i'm on windows 10

2018-04-08_19-07-18

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment