Skip to content

Instantly share code, notes, and snippets.

@toomasv
Last active May 14, 2019 08:07
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/01817e797fdb38d277d4c01dad89b326 to your computer and use it in GitHub Desktop.
Save toomasv/01817e797fdb38d277d4c01dad89b326 to your computer and use it in GitHub Desktop.
Studying tool for shape-dialect 'arc
Red [
Author: "Toomas Vooglaid"
Date: 2018-07-11
Last-edit: 2018-08-21
File: %arc.red
Purpose: "Study of shape-dialect `'arc`"
Licence: "MIT"
]
help: rtd-layout [
{ In } i "shape" /i { dialect drawing of } i "arc" /i b " starts" /b { from the current position. Here I use } i "move" /i { to set initial position.^/}
f 12 " " /f i "'arc's" /i b " first" /b { argument is its endpoint (} i "pair!" /i {). As } i "'arc" /i { is relative shape, the endpoint pair is set relative to starting point not to 0x0 (unless 0x0 is starting point of course).^/}
f 12 " " /f b "Then" /b { follow } i "x-radius" /i { and } i "y-radius" /i { (2 * } i "integer!" /i {). } i "'arc" /i { is always drawn from starting-point to endpoint, even if } i "x-radius" /i { or } i "y-radius" /i { is too small to actually reach either point. In this case arc is stretched proportionally, so that endpoint is reached. Try setting some small value for } i "x-radius" /i { or } i "y-radius" /i {...^/}
f 12 " " /f b "Fourth" /b { argument is } i "angle" /i {, which sets the rotation of the arc's underlying ellipse. This is meaningful only if } i "x-radius" /i { and } i "y-radius" /i { are different. Otherwise it just rotates the underlying circle. When you rotate the arc with differing } i "x-radius" /i { and } i "y-radius" /i {, the arc's dimensions may be streched for the aforementioned reason.^/}
f 12 " " /f b "Last" /b { two arguments are optional } i "`sweep`" /i { and } i "`large`" /i {. If } i "`sweep`" /i { is set } i "'arc" /i { is drawn clockwise instead of default counterclockwise. If } i "`large`" /i { is present the larger one of the two possible arcs is drawn. If you draw a chord through ellipse this divides ellipse into two segments and if the chord doesn't run through the center, then one segment and arc on it is larger. This larger arc is drawn and underlying ellipse is translated accordingly. If chord, which is determined by starting and ending points, runs through the center of underlying ellipse, } i "`large`" /i { has no effect.}
]
help/size/x: 500
example: function [][
;mov end x-rad y-rad ang swp lrg
x-rad: 10 + round/to random 180 10
until [x-rad <> y-rad: 10 + round/to random 130 10]
mov: as-pair round/to random 400 - x-rad 10 round/to random 300 - y-rad 10
end: as-pair round/to random x-rad 10 round/to random y-rad 10
if all [random true end/y < mov/y] [end/y: 0 - end/y]
ang: round/to random 180 10
ret: reduce [
'move mov to-lit-word 'arc end x-rad y-rad ang
'move mov to-lit-word 'arc end x-rad y-rad ang 'sweep 'large
]
attempt [unless find ret ex2/2 [ex2/-2: 255.255.235.150]]
ret
]
check-match: does [
ex2/-2: either find ex1/2 ex2/2 [255.205.40.50][255.255.235.150]
]
view/no-wait [
size 550x440 ;450
title "Shape's 'arc studying tool"
style lbl: text 50x24
style fld: field 50x24 [
arc/2/(face/extra): face/data txt/text: mold/only arc
arc2/2/(face/extra): face/data
]
style chk: check 30x24
tab-panel 525x420 [;430
"Arc" [
space 10x0 below
lbl "Start:" fld "180x140" extra 2 return
lbl "End:" fld "80x20" extra 4 return
lbl "X-radius:" fld "70" extra 5 return
lbl "Y-radius:" fld "40" extra 6 return
lbl "Angle:" fld "0" extra 7 return
lbl "Sweep:" chk [
case [
all [face/data arc/2/8 <> 'sweep] [insert at arc/2 8 'sweep remove at arc2/2 8]
all [not face/data arc/2/8 = 'sweep] [remove at arc/2 8 insert at arc2/2 8 'sweep]
]
txt/text: mold/only arc
] return
lbl "Large:" chk [
case [
all [face/data 'large <> last arc/2] [append arc/2 'large remove back tail arc2/2]
all [not face/data 'large = last arc/2] [remove back tail arc/2 append arc2/2 'large]
]
txt/text: mold/only arc
] return
lbl "Show ellipse:" 70x24 chk [
ellipse/2: either face/data [120.120.120]['off]
]
origin 10x70
panel white [
origin 0x0
at 0x0 box 200.200.200 501x301 draw [
fill-pen pattern 100x100 [
fill-pen pattern 10x10 [
pen 150.150.150 box 0x0 10x10
] pen 120.120.120 box 0x0 100x100
] box 0x0 500x300
]
box 500x300 draw [
ellipse: pen off ;fill-pen off
arc2: shape [move 180x140 'arc 80x20 70 40 0 sweep large]
fill-pen 255.255.235.150 pen black
arc: shape [move 180x140 'arc 80x20 70 40 0]
]
] pad 0x5 across
txt: text 510x25 with [text: mold/only arc] return
] "Help" [
below
at 0x0 box 510x390 white
rich-text 510x380 with [text: none draw: compose [
text 0x0 (help)
]]
] "Exercise" [
h5 430x24 {Try matching any of given 'arcs}
button "New" [ex1/2: example] return
code: field 430x24 hint "Your code for shape block" [
ex2/2: face/data
check-match
]
button "Show" [
unless empty? face/text [
ex2/2: code/data
check-match
]
] return
origin 10x85
panel white [
origin 0x0
at 0x0 box 200.200.200 501x301 draw [
fill-pen pattern 100x100 [
fill-pen pattern 10x10 [
pen 150.150.150 box 0x0 10x10
] pen 120.120.120 box 0x0 100x100
] box 0x0 500x300
]
box 500x300 with [
draw: append/only [
fill-pen 255.255.235.150
ex2: shape [move 0x0 'arc 0x0 0 0 0]
fill-pen 255.255.255.150
ex1: shape
] example
]
]
]
]
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment