Predefined Shapes
Predefined Shapes
Hobby provides predefined shapes that are already solved and ready to use. They can be transformed and styled without calling build().
Available Shapes
fullcircle()
A unit circle with diameter 1, centered at the origin.
local circle = h.fullcircle()halfcircle()
The upper half of the unit circle.
local half = h.halfcircle()quartercircle()
A quarter circle (first quadrant arc).
local quarter = h.quartercircle()unitsquare()
A unit square from (0,0) to (1,1).
local square = h.unitsquare()Using Shapes
Predefined shapes are typically scaled and positioned:
local h = require("hobby")
-- A circle with diameter 50, centered at (100, 100)
local circle = h.fullcircle()
:scaled(50)
:shifted(100, 100)
:stroke("blue")
-- A square with side 30, positioned at (50, 50)
local square = h.unitsquare()
:scaled(30)
:shifted(50, 50)
:stroke("red")
h.svg()
:padding(10)
:add(circle)
:add(square)
:write("shapes.svg")Shape Properties
length
Returns the number of segments in the path.
local circle = h.fullcircle():scaled(50)
print(circle.length) -- 8
local square = h.unitsquare():scaled(30)
print(square.length) -- 4Points
point(x, y)
Creates a point that can be used with paths and transformations.
local p = h.point(100, 50)Points support arithmetic operations:
local a = h.point(10, 20)
local b = h.point(30, 40)
local sum = a + b -- (40, 60)
local diff = a - b -- (-20, -20)
local scaled = a * 2 -- (20, 40)
local neg = -a -- (-10, -20)dir(angle)
Creates a unit vector at the given angle (in degrees).
local right = h.dir(0) -- (1, 0)
local up = h.dir(90) -- (0, 1)
local diag = h.dir(45) -- (0.707, 0.707)Geometry Helpers
midpoint(a, b)
Returns the midpoint between two points.
local mid = h.midpoint(h.point(0, 0), h.point(100, 100))
-- Result: (50, 50)distance(a, b)
Returns the Euclidean distance between two points.
local d = h.distance(h.point(0, 0), h.point(3, 4))
-- Result: 5between(a, b, t)
Returns the point at parameter t along the line from a to b.
local p = h.between(h.point(0, 0), h.point(100, 0), 0.25)
-- Result: (25, 0)