From 4eff88679c3996280da29d67d5d66499e0c5c82d Mon Sep 17 00:00:00 2001 From: hung Date: Sat, 28 Mar 2020 20:21:39 +0700 Subject: [PATCH 1/3] new shape: star --- lib/gears/shape.lua | 37 +++++++++++++++++++++++++++++ tests/examples/gears/shape/star.lua | 15 ++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/examples/gears/shape/star.lua diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index f13278a93..26d08c7fe 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -43,9 +43,46 @@ local g_matrix = require( "gears.matrix" ) local unpack = unpack or table.unpack -- luacheck: globals unpack (compatibility with Lua 5.1) local atan2 = math.atan2 or math.atan -- lua 5.3 compat +local min = math.min +local cos = math.cos +local sin = math.sin local module = {} +--- Add a star shape to the current path. +-- The star size will be the minimum of the given width and weight +-- +-- @DOC_gears_shape_star_EXAMPLE@ +-- +-- @param cr A cairo context +-- @tparam number width The width constraint +-- @tparam number height The height constraint +-- @tparam number n Number of grams (default n = 5 -> pentagram) +-- @staticfct gears.shape.star +function module.star(cr, width, height, n) + -- use the minimum as size + local s = min(width, height) / 2 + + -- draw pentagram by default + n = n or 5 + local a = 2 * math.pi / n + + -- place the star at the center + cr:translate(width/2, height/2) + cr:rotate(-math.pi/2) + + for i = 0,(n - 1) do + cr:line_to(s * cos((i ) * a), s * sin((i ) * a)) + cr:line_to(s/2 * cos((i + 0.5) * a), s/2 * sin((i + 0.5) * a)) + end + + -- restore the context + cr:rotate(math.pi/2) + cr:translate(-width/2, -height/2) + + cr:close_path() +end + --- Add a rounded rectangle to the current path. -- Note: If the radius is bigger than either half side, it will be reduced. -- diff --git a/tests/examples/gears/shape/star.lua b/tests/examples/gears/shape/star.lua new file mode 100644 index 000000000..5aa76731f --- /dev/null +++ b/tests/examples/gears/shape/star.lua @@ -0,0 +1,15 @@ +--DOC_GEN_IMAGE --DOC_HIDE +local shape,cr,show = ... --DOC_HIDE + +shape.star(cr, 70, 70, 4) +show(cr) --DOC_HIDE + +shape.star(cr, 70, 70, 9) +show(cr) --DOC_HIDE + +shape.transform(shape.star) : translate(70/2, 70/2) +: rotate(math.pi) : scale(0.5, 0.75) +: translate(-70/2, -70/2) (cr, 70, 70) +show(cr) --DOC_HIDE + +--DOC_HIDE vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 From 7f7793378e6c59456ec95a86139b07a399fa3fca Mon Sep 17 00:00:00 2001 From: hung Date: Sat, 28 Mar 2020 20:43:07 +0700 Subject: [PATCH 2/3] Use cr:save and cr:restore instead --- lib/gears/shape.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index 26d08c7fe..eaea82025 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -67,7 +67,9 @@ function module.star(cr, width, height, n) n = n or 5 local a = 2 * math.pi / n + -- place the star at the center + cr:save() cr:translate(width/2, height/2) cr:rotate(-math.pi/2) @@ -77,8 +79,7 @@ function module.star(cr, width, height, n) end -- restore the context - cr:rotate(math.pi/2) - cr:translate(-width/2, -height/2) + cr:restore() cr:close_path() end From 33f99fea1c005d21b21e568e10b11842b40347c3 Mon Sep 17 00:00:00 2001 From: hung Date: Sat, 28 Mar 2020 21:13:53 +0700 Subject: [PATCH 3/3] Remove double blank line --- lib/gears/shape.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/gears/shape.lua b/lib/gears/shape.lua index eaea82025..ac12f58b2 100644 --- a/lib/gears/shape.lua +++ b/lib/gears/shape.lua @@ -67,7 +67,6 @@ function module.star(cr, width, height, n) n = n or 5 local a = 2 * math.pi / n - -- place the star at the center cr:save() cr:translate(width/2, height/2)