https://github.com/stumpycr/stumpy_utils
Extensions (e.g. drawing functions) for stumpy_core
https://github.com/stumpycr/stumpy_utils
computer-graphics crystal draw
Last synced: 5 months ago
JSON representation
Extensions (e.g. drawing functions) for stumpy_core
- Host: GitHub
- URL: https://github.com/stumpycr/stumpy_utils
- Owner: stumpycr
- License: mit
- Created: 2016-11-13T15:00:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-09-18T07:15:10.000Z (over 5 years ago)
- Last Synced: 2025-07-13T14:41:09.050Z (8 months ago)
- Topics: computer-graphics, crystal, draw
- Language: Crystal
- Homepage:
- Size: 206 KB
- Stars: 10
- Watchers: 1
- Forks: 5
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Stumpy Utils
A collection of drawing functions for `stumpy_core`, `stumpy_png`, ...
## Usage
### Install the `stumpy_utils` shard
1. `shards init`
2. Add the dependency to the `shard.yml` file
``` yaml
...
dependencies:
stumpy_utils:
github: stumpycr/stumpy_utils
...
```
3. `shards install`
You also need to install and require (at least) one of the image extensions
(
[stumpy_png](https://github.com/stumpycr/stumpy_png),
[stumpy_gif](https://github.com/stumpycr/stumpy_gif)
)
or
[stumpy_core](https://github.com/stumpycr/stumpy_core).
### Latest Release
[](https://github.com/stumpycr/stumpy_utils/releases)
## Canvas
### `canvas.line(x0, y0, x1, y1, color = RGBA::BLACK)`
Use the Xiaolin Wo algorithm
to draw an antialiased line from (x0, y0) to (x1, y1).
`x0`, etc can be floats, too.
#### Example
``` crystal
require "stumpy_png"
require "stumpy_utils"
include StumpyPNG
record Branch, x0 : Int32, y0 : Int32, dir : Int32, length : Float64 do
RADIANTS = Math::PI / 180.0
FACTOR = (0.6..1.0)
ANGLE = (30..90)
def x1; x0 + (Math.sin(dir * RADIANTS) * length).to_i; end
def y1; y0 + (Math.cos(dir * RADIANTS) * length).to_i; end
def split
angle = rand(ANGLE) / 2
[Branch.new(x1, y1, dir + angle, length * rand(FACTOR)),
Branch.new(x1, y1, dir - angle, length * rand(FACTOR))]
end
end
branches = [Branch.new(128, 256, 180, 50.0)]
canvas = Canvas.new(256, 256, RGBA::WHITE)
10.times do |i|
branches = branches.flat_map do |b|
canvas.line(b.x0, b.y0, b.x1, b.y1, RGBA::DARKGREEN)
b.split
end
end
StumpyPNG.write(canvas, "tree.png")
```

### `canvas.circle(center_x, center_y, radius, color = RGBA::BLACK, filled = false)`
Use the Xiaolin Wo algorithm
to draw an antialiased (filled) circle
around (center_x, center_y).
#### Example
``` crystal
require "stumpy_png"
require "stumpy_utils"
include StumpyPNG
canvas = Canvas.new(400, 400, RGBA::WHITE)
colors = [RGBA::WHITE, RGBA::DARKSLATEGRAY, RGBA::LIGHTBLUE, RGBA::RED, RGBA::YELLOW]
colors.each_with_index do |color, i|
radius = (colors.size - i) * 40
canvas.circle(200, 200, radius, color, true)
end
(colors.size * 2).times do |i|
radius = ((colors.size * 2) - i) * 20
canvas.circle(200, 200, radius, RGBA::BLACK, false)
end
StumpyPNG.write(canvas, "circles.png")
```

### `canvas.fill_pilygon(vertices, color = RGBA::BLACK)`
Use the scanline algorithm to fill a polygon
defined by an arbitrary set of vertices.
_vertices_ should be an array of objects that respond to .x and .y methods.
StumpyCore::Point is provided as a default implementation.
#### Example
```crystal
require "stumpy_png"
require "stumpy_utils"
include StumpyPNG
canvas = Canvas.new(400, 400, RGBA::WHITE)
# triangle
canvas.fill_polygon [Point.new(150.0, 50.0), Point.new(250.0, 200.0), Point.new(50.0, 200.0)], RGBA::RED
# pentagon
num_points = 5
radius = 100
center = Point.new 200.0, 280.0
points = 0.upto(num_points).map do |n|
angle = 2.0*Math::PI*n/num_points - Math::PI/2.0
Point.new Math.cos(angle)*radius + center.x, Math.sin(angle)*radius + center.y
end.to_a
canvas.fill_polygon points, RGBA::BLUE
# star
center = Point.new 280.0, 150.0
points = 0.upto(num_points*2).map do |n|
angle = Math::PI*n/num_points - Math::PI/2.0
r = n % 2 == 0 ? radius : radius/2.0
Point.new Math.cos(angle)*r + center.x, Math.sin(angle)*r + center.y
end.to_a
canvas.fill_polygon points, RGBA::GREEN
StumpyPNG.write(canvas, "polygons.png")
```

### `canvas.text(x, baseline_y, text, font : PCFParser::FONT, color = RGBA:BACK)`
__NOTE:__ The fonts are not included in this repo.
On linux, you can find them using `fc-list | grep '.pcf'`.
(The files need to be unpacked using `gunzip` before using them)
``` crystal
require "stumpy_png"
require "stumpy_utils"
include StumpyPNG
canvas = Canvas.new(500, 80, RGBA::WHITE)
font1 = PCFParser::Font.from_file("./fonts/10x20.pcf")
font2 = PCFParser::Font.from_file("./fonts/helvR18.pcf")
text = "The quick brown fox jumps over the lazy dog"
canvas.text(10, 30, text, font1)
canvas.text(10, 60, text, font2, RGBA::BLUE)
StumpyPNG.write(canvas, "text.png")
```

## Contributors
Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
| [
Leon](http://leonrische.me)
[💻](https://github.com/stumpycr/stumpy_utils/commits?author=l3kn "Code") | [
Daniel Huffman](https://www.linkedin.com/pub/daniel-huffman/21/aa3/869)
[💻](https://github.com/stumpycr/stumpy_utils/commits?author=drhuffman12 "Code") | [
Andy Selvig](http://www.tinymission.com)
[💻](https://github.com/stumpycr/stumpy_utils/commits?author=ajselvig "Code") | [
Nishant Shah](https://nish.space)
[💻](https://github.com/stumpycr/stumpy_utils/commits?author=nini1294 "Code") |
| :---: | :---: | :---: | :---: |
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!