{"id":21383718,"url":"https://github.com/stumpycr/stumpy_utils","last_synced_at":"2025-10-11T03:54:45.484Z","repository":{"id":67933314,"uuid":"73622855","full_name":"stumpycr/stumpy_utils","owner":"stumpycr","description":"Extensions (e.g. drawing functions) for stumpy_core","archived":false,"fork":false,"pushed_at":"2020-09-18T07:15:10.000Z","size":211,"stargazers_count":10,"open_issues_count":3,"forks_count":5,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-07-13T14:41:09.050Z","etag":null,"topics":["computer-graphics","crystal","draw"],"latest_commit_sha":null,"homepage":"","language":"Crystal","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stumpycr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-11-13T15:00:32.000Z","updated_at":"2024-12-01T12:31:19.000Z","dependencies_parsed_at":"2023-05-28T20:45:28.598Z","dependency_job_id":null,"html_url":"https://github.com/stumpycr/stumpy_utils","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/stumpycr/stumpy_utils","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stumpycr%2Fstumpy_utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stumpycr%2Fstumpy_utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stumpycr%2Fstumpy_utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stumpycr%2Fstumpy_utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stumpycr","download_url":"https://codeload.github.com/stumpycr/stumpy_utils/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stumpycr%2Fstumpy_utils/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279006109,"owners_count":26084026,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-10-11T02:00:06.511Z","response_time":55,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["computer-graphics","crystal","draw"],"created_at":"2024-11-22T11:32:06.188Z","updated_at":"2025-10-11T03:54:45.452Z","avatar_url":"https://github.com/stumpycr.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Stumpy Utils\n\nA collection of drawing functions for `stumpy_core`, `stumpy_png`, ...\n\n## Usage\n\n### Install the `stumpy_utils` shard\n\n1. `shards init`\n2. Add the dependency to the `shard.yml` file\n ``` yaml\n ...\n dependencies:\n   stumpy_utils:\n     github: stumpycr/stumpy_utils\n ...\n ```\n3. `shards install`\n\nYou also need to install and require (at least) one of the image extensions\n(\n[stumpy_png](https://github.com/stumpycr/stumpy_png),\n[stumpy_gif](https://github.com/stumpycr/stumpy_gif)\n)\nor\n[stumpy_core](https://github.com/stumpycr/stumpy_core).\n\n\n### Latest Release\n\n[![GitHub release](https://img.shields.io/github/release/stumpycr/stumpy_utils.svg)](https://github.com/stumpycr/stumpy_utils/releases)\n\n## Canvas\n\n### `canvas.line(x0, y0, x1, y1, color = RGBA::BLACK)`\n\nUse the Xiaolin Wo algorithm\nto draw an antialiased line from (x0, y0) to (x1, y1).\n`x0`, etc can be floats, too.\n\n#### Example\n\n``` crystal\nrequire \"stumpy_png\"\nrequire \"stumpy_utils\"\ninclude StumpyPNG\n\nrecord Branch, x0 : Int32, y0 : Int32, dir : Int32, length : Float64 do\n  RADIANTS = Math::PI / 180.0\n  FACTOR = (0.6..1.0)\n  ANGLE = (30..90)\n\n  def x1; x0 + (Math.sin(dir * RADIANTS) * length).to_i; end\n  def y1; y0 + (Math.cos(dir * RADIANTS) * length).to_i; end\n\n  def split\n    angle = rand(ANGLE) / 2\n    [Branch.new(x1, y1, dir + angle, length * rand(FACTOR)),\n     Branch.new(x1, y1, dir - angle, length * rand(FACTOR))]\n  end\nend\n\nbranches = [Branch.new(128, 256, 180, 50.0)]\ncanvas = Canvas.new(256, 256, RGBA::WHITE)\n\n10.times do |i|\n  branches = branches.flat_map do |b|\n    canvas.line(b.x0, b.y0, b.x1, b.y1, RGBA::DARKGREEN)\n    b.split\n  end\nend\n\nStumpyPNG.write(canvas, \"tree.png\")\n```\n\n![Tree generated using lines](spec/out/tree.png)\n\n### `canvas.circle(center_x, center_y, radius, color = RGBA::BLACK, filled = false)`\n\nUse the Xiaolin Wo algorithm\nto draw an antialiased (filled) circle\naround (center_x, center_y).\n\n#### Example\n\n``` crystal\nrequire \"stumpy_png\"\nrequire \"stumpy_utils\"\ninclude StumpyPNG\n\ncanvas = Canvas.new(400, 400, RGBA::WHITE)\ncolors = [RGBA::WHITE, RGBA::DARKSLATEGRAY, RGBA::LIGHTBLUE, RGBA::RED, RGBA::YELLOW]\n\ncolors.each_with_index do |color, i|\n  radius = (colors.size - i) * 40\n  canvas.circle(200, 200, radius, color, true)\nend\n\n(colors.size * 2).times do |i|\n  radius = ((colors.size * 2) - i) * 20\n  canvas.circle(200, 200, radius, RGBA::BLACK, false)\nend\n\nStumpyPNG.write(canvas, \"circles.png\")\n```\n![Concentric circles](spec/out/circles.png)\n\n### `canvas.fill_pilygon(vertices, color = RGBA::BLACK)`\n\nUse the scanline algorithm to fill a polygon\ndefined by an arbitrary set of vertices.\n\n_vertices_ should be an array of objects that respond to .x and .y methods.\nStumpyCore::Point is provided as a default implementation.\n\n#### Example\n\n```crystal\n    require \"stumpy_png\"\n    require \"stumpy_utils\"\n    include StumpyPNG\n\n    canvas = Canvas.new(400, 400, RGBA::WHITE)\n\n    # triangle\n    canvas.fill_polygon [Point.new(150.0, 50.0), Point.new(250.0, 200.0), Point.new(50.0, 200.0)], RGBA::RED\n\n    # pentagon\n    num_points = 5\n    radius = 100\n    center = Point.new 200.0, 280.0\n    points = 0.upto(num_points).map do |n|\n      angle = 2.0*Math::PI*n/num_points - Math::PI/2.0\n      Point.new Math.cos(angle)*radius + center.x, Math.sin(angle)*radius + center.y\n    end.to_a\n    canvas.fill_polygon points, RGBA::BLUE\n\n    # star\n    center = Point.new 280.0, 150.0\n    points = 0.upto(num_points*2).map do |n|\n      angle = Math::PI*n/num_points - Math::PI/2.0\n      r = n % 2 == 0 ? radius : radius/2.0\n      Point.new Math.cos(angle)*r + center.x, Math.sin(angle)*r + center.y\n    end.to_a\n    canvas.fill_polygon points, RGBA::GREEN\n\n    StumpyPNG.write(canvas, \"polygons.png\")\n```\n![Polygons](spec/out/polygons.png)\n\n\n### `canvas.text(x, baseline_y, text, font : PCFParser::FONT, color = RGBA:BACK)`\n\n__NOTE:__ The fonts are not included in this repo.\n\nOn linux, you can find them using `fc-list | grep '.pcf'`.\n(The files need to be unpacked using `gunzip` before using them)\n\n``` crystal\nrequire \"stumpy_png\"\nrequire \"stumpy_utils\"\ninclude StumpyPNG\n\ncanvas = Canvas.new(500, 80, RGBA::WHITE)\n\nfont1 = PCFParser::Font.from_file(\"./fonts/10x20.pcf\")\nfont2 = PCFParser::Font.from_file(\"./fonts/helvR18.pcf\")\n\ntext = \"The quick brown fox jumps over the lazy dog\"\n\ncanvas.text(10, 30, text, font1)\ncanvas.text(10, 60, text, font2, RGBA::BLUE)\n\nStumpyPNG.write(canvas, \"text.png\")\n```\n\n![Text samples in different fonts](spec/out/text.png)\n\n## Contributors\n\nThanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):\n\n\u003c!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section --\u003e\n\u003c!-- prettier-ignore --\u003e\n| [\u003cimg src=\"https://avatars1.githubusercontent.com/u/2060269?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eLeon\u003c/b\u003e\u003c/sub\u003e](http://leonrische.me)\u003cbr /\u003e[💻](https://github.com/stumpycr/stumpy_utils/commits?author=l3kn \"Code\") | [\u003cimg src=\"https://avatars3.githubusercontent.com/u/594270?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eDaniel Huffman\u003c/b\u003e\u003c/sub\u003e](https://www.linkedin.com/pub/daniel-huffman/21/aa3/869)\u003cbr /\u003e[💻](https://github.com/stumpycr/stumpy_utils/commits?author=drhuffman12 \"Code\") | [\u003cimg src=\"https://avatars1.githubusercontent.com/u/455594?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eAndy Selvig\u003c/b\u003e\u003c/sub\u003e](http://www.tinymission.com)\u003cbr /\u003e[💻](https://github.com/stumpycr/stumpy_utils/commits?author=ajselvig \"Code\") | [\u003cimg src=\"https://avatars3.githubusercontent.com/u/6395125?v=4\" width=\"100px;\"/\u003e\u003cbr /\u003e\u003csub\u003e\u003cb\u003eNishant Shah\u003c/b\u003e\u003c/sub\u003e](https://nish.space)\u003cbr /\u003e[💻](https://github.com/stumpycr/stumpy_utils/commits?author=nini1294 \"Code\") |\n| :---: | :---: | :---: | :---: |\n\u003c!-- ALL-CONTRIBUTORS-LIST:END --\u003e\n\nThis project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstumpycr%2Fstumpy_utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstumpycr%2Fstumpy_utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstumpycr%2Fstumpy_utils/lists"}