{"id":244,"date":"2025-09-25T19:32:33","date_gmt":"2025-09-25T18:32:33","guid":{"rendered":"https:\/\/zebra-north.com\/code\/?p=244"},"modified":"2026-08-10T18:35:38","modified_gmt":"2026-08-10T17:35:38","slug":"how-to-draw-concentric-triangles-in-a-shader","status":"publish","type":"post","link":"https:\/\/zebra-north.com\/code\/2025\/09\/25\/how-to-draw-concentric-triangles-in-a-shader\/","title":{"rendered":"How to Draw Concentric Triangles in a Shader"},"content":{"rendered":"\n<p>A simple way to draw a triangle, or concentric triangles, in a shader.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Given any point <code>(u, v)<\/code> in space, we wish to find the size of the triangle upon which that point lies. We can then use that size to draw the triangle.<\/p>\n\n\n\n<p>We will define the size of the triangle as the distance between centre and the midpoint of each edge. The triangle is symmetrical about the y axis, with the horizontal edge on the bottom. The centre is at the origin, (0, 0).<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"668\" height=\"720\" src=\"https:\/\/zebra-north.com\/code\/wp-content\/uploads\/2025\/09\/triangle.png\" alt=\"\" class=\"wp-image-245\" srcset=\"https:\/\/zebra-north.com\/code\/wp-content\/uploads\/2025\/09\/triangle.png 668w, https:\/\/zebra-north.com\/code\/wp-content\/uploads\/2025\/09\/triangle-278x300.png 278w\" sizes=\"auto, (max-width: 668px) 100vw, 668px\" \/><\/figure>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Calculate <code>a<\/code> as <br><code>a = -v<\/code>.<\/li>\n\n\n\n<li>Calculate <code>b<\/code> by rotating the edge to be vertical, then taking the horizontal part of the coordinate:<br><code>b = rotate2d((abs(u), v), 90 - \u03b1).x<\/code><br>where <code>\u03b1<\/code> is the angle between the base of the triangle and one side of the triangle.<\/li>\n\n\n\n<li>The size of the triangle is then the greater of <code>a<\/code> and <code>b<\/code>:<br><code>size = max(a, b)<\/code><\/li>\n<\/ol>\n\n\n\n<p>The size can then be used to daw the triangle:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To draw a filled triangle, colour any points where the size is less than a threshold.<\/li>\n\n\n\n<li>To draw a triangular cutout, colour any points where the size is greater than a threshold.<\/li>\n\n\n\n<li>To draw concentric triangles, colour any points where sin(size * frequency) &gt; 0.<\/li>\n<\/ul>\n\n\n\n<p>To make a non-symmetrical triangle perform step 2 twice, once with the angle of the left side and once with the angle of the right side.<\/p>\n\n\n\n<p>To rotate, scale or translate the triangle: rotate, scale or translate (u, v) by the inverse of the effect required before performing this procedure.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A simple way to draw a triangle, or concentric triangles, in a shader.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-244","post","type-post","status-publish","format-standard","hentry","category-shader"],"_links":{"self":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/comments?post=244"}],"version-history":[{"count":3,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/244\/revisions"}],"predecessor-version":[{"id":252,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/posts\/244\/revisions\/252"}],"wp:attachment":[{"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/media?parent=244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/categories?post=244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zebra-north.com\/code\/wp-json\/wp\/v2\/tags?post=244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}