Skip to content

Commit 77a5052

Browse files
committed
New function Script.arc/7 to add arcs to current path
1 parent 391e9d2 commit 77a5052

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

lib/scenic/script.ex

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ defmodule Scenic.Script do
202202
@op_sector 0x2F
203203
@op_circle 0x30
204204
@op_ellipse 0x31
205+
@op_arc 0x32
205206

206207
@op_push_state 0x40
207208
@op_pop_state 0x41
@@ -829,6 +830,33 @@ defmodule Scenic.Script do
829830
[{:ellipse, {radius0, radius1}} | ops]
830831
end
831832

833+
@doc """
834+
Adds a new **circle arc** shaped segment to the path.
835+
836+
This adds to the current path. It does not fill or stroke anything.
837+
838+
## Parameters
839+
840+
- cx, cy: The coordinates of the arc's center.
841+
- r: the radius of the arc.
842+
- a0, a1: the angles which the arc is drawn from angle a0 to a1, in radians.
843+
- dir: s the direction of the arc sweep:
844+
- `1` for counter clockwise sweep;
845+
- `2` for clockwise sweep.
846+
"""
847+
@spec arc(
848+
ops :: t(),
849+
cx :: number,
850+
cy :: number,
851+
r :: number,
852+
a0 :: number,
853+
a1 :: number,
854+
dir :: integer
855+
) :: ops :: t()
856+
def arc(ops, cx, cy, r, a0, a1, dir) do
857+
[{:arc, {cx, cy, r, a0, a1, dir}} | ops]
858+
end
859+
832860
@doc """
833861
Add a triangle to the current path.
834862
@@ -1538,6 +1566,23 @@ defmodule Scenic.Script do
15381566
]
15391567
end
15401568

1569+
defp serialize_op({:arc, {cx, cy, r, a0, a1, dir}}) do
1570+
[
1571+
<<
1572+
@op_arc::16-big,
1573+
0::16
1574+
>>,
1575+
<<
1576+
cx::float-32-big,
1577+
cy::float-32-big,
1578+
r::float-32-big,
1579+
a0::float-32-big,
1580+
a1::float-32-big,
1581+
dir::32-big
1582+
>>
1583+
]
1584+
end
1585+
15411586
defp serialize_op({:bezier_to, {cp1x, cp1y, cp2x, cp2y, x, y}}) do
15421587
[
15431588
<<
@@ -2261,6 +2306,20 @@ defmodule Scenic.Script do
22612306
{{:arc_to, {x1, y1, x2, y2, radius}}, bin}
22622307
end
22632308

2309+
defp deserialize_op(<<
2310+
@op_arc::16-big,
2311+
0::16,
2312+
cx::float-32-big,
2313+
cy::float-32-big,
2314+
r::float-32-big,
2315+
a0::float-32-big,
2316+
a1::float-32-big,
2317+
dir::32-big,
2318+
bin::binary
2319+
>>) do
2320+
{{:arc, {cx, cy, r, a0, a1, dir}}, bin}
2321+
end
2322+
22642323
defp deserialize_op(<<
22652324
@op_bezier_to::16-big,
22662325
0::16,

test/scenic/script_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,12 @@ defmodule Scenic.ScriptTest do
351351
assert expected == Script.serialize(expected) |> Script.deserialize()
352352
end
353353

354+
test "arc works" do
355+
expected = [{:arc, {0.0, 0.0, 5.0, 0.0, 6.0, 1}}]
356+
assert Script.arc([], 0, 0, 5, 0, 6, 1) == expected
357+
assert expected == Script.serialize(expected) |> Script.deserialize()
358+
end
359+
354360
# --------------------------------------------------------
355361
# transform commands
356362

0 commit comments

Comments
 (0)