Skip to content

Commit 507cb29

Browse files
authored
Merge pull request #326 from GPrimola/new-script-arc-func
New function Scenic.Script.arc/7 to add arcs to current path
2 parents bebae80 + 77a5052 commit 507cb29

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
@@ -1539,6 +1567,23 @@ defmodule Scenic.Script do
15391567
]
15401568
end
15411569

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

2311+
defp deserialize_op(<<
2312+
@op_arc::16-big,
2313+
0::16,
2314+
cx::float-32-big,
2315+
cy::float-32-big,
2316+
r::float-32-big,
2317+
a0::float-32-big,
2318+
a1::float-32-big,
2319+
dir::32-big,
2320+
bin::binary
2321+
>>) do
2322+
{{:arc, {cx, cy, r, a0, a1, dir}}, bin}
2323+
end
2324+
22662325
defp deserialize_op(<<
22672326
@op_bezier_to::16-big,
22682327
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)