codedrawn
codedrawn
CodeDrawn
7 posts
A sideblog for me to show of drawings I've made in code
Last active 60 minutes ago
Don't wanna be here? Send us removal request.
codedrawn · 9 months ago
Text
Dragon Curve Again!
Tumblr media
Ok, so I know that I've been repeating things a lot, but I really wanted to kill a few hours so I used that as an opportunity to practice C and since I've been doing image stuff lately that was a obvious choice. I didn't make a drawing library for this one, I just set each endpoint to black and set the number of iterations up high enough for that to fill everything in.
I ran into an issue due to running the line splitting recursively over a linked list of points and after running the line splitting like 17 times there was so many lines I filled the stack, so I switch to doing it iteratively and all was well. More specifically, this version ran the line splitting 18 times, the minimum for it to actually fill in, and it has 262145 points so that's how many recursive function calls were attempted.
The reason I wanted to do a linked list for the points is because I need to insert points between other points constantly and when doing it in rust vector I constantly had to copy values from one vector to another and this solution avoids it. I'm not actually sure if that is enough to actually make linked lists the more performant solution but, between needing to insert values in the middle of the list thousands of times and always needing to iterate through the list instead of indexing, I doubt the stars will align better than this.
Aside from those caveats, the algorithm is the same, but I've decided to include a small snipped under the cut since its taking me so long to actually host my code.
Tumblr media
This image (made with carbon.now.sh) shows the line splitting part of the algorithm. In short, take the angle between the two points, rotate by 45 degrees with it being clockwise or counter-clockwise alternating, then find the distance the new length should be, which is equal to the length of the first line times the sine of 45 degrees, aka one over the square root of two, which is absorbed into the other square root for reasons that I can't remember because I found it using another method that I also can't remember and had written on a whiteboard that my boyfriend has drawn an among us/gravity falls comic on, but it's provably correct so I don't really care.
The rest of the code is just initializing everything, storing the results of this, then writing the pixels. I could optimize it more but its pretty fast as is so that isn't a priority.
0 notes
codedrawn · 9 months ago
Text
Dragon curve!
This one is fairly straightforward, I made the dragon curve. This has absolutely no bearing on the library I'm slowly building up, I just used the normal line drawing function and everything. I did have a plan that would've expanded the library a bit but I decided this was easier. Anyway, here it is!
Tumblr media
As for the method used, I started with one line, then on a loop, 18 times for this image, I replaced each line with two more lines going 45° from the previous. For a better explanation, refer to this animation on this wikipedia article for the dragon curve, I pulled every part of my implantation directly from interpreting that gif.
I had a couple interesting problems during this. For one thing, the inverse tangent (aka arctan or atan) not being able to differentiate between both side lengths being negative vs both being positive. This lead me to learning about libc's atan2 function, which I think is a hilarious way to title the function, like yeah, it really is the sequel to atan, just atan but better. Anyway, I used that, had to flip which point was the start and which was the end but it all worked out and was just a series of easy fixes.
Another issue I had was trying to get rust to give me an iterator of every pair of points in the list. I'm still not sure where I was going wrong there and I ended up just iterating over 1 less than the length of the list and indexing into it; bad practice, i know, but it worked and clippy isn't yelling at me so I say good enough.
0 notes
codedrawn · 9 months ago
Text
I made a bad circle algorithm, it uses an excessive number of points and still doesn't look very good for large circles and increasing it enough to remove that problem slows it down too much, but it'll be good enough to draw some patterns with.
Tumblr media
I decided to do a super simple circle based fractal where each circle has 3 circles inside it as big as possible. Its doing 7 iterations but it can go deeper without significant slowdown on my computer, I'm not even sure if all 7 are actually visible anyway.
One thing that I did here was to vary the angles the circles start at by 60° so it looks a bit more interesting, but doing that really made me want to draw a dragon curve, so that will probably be next. I don't want to just do it normally though so I think I will try to only have 1 line be specified and do the entire rest of the curve by transforming the canvas. Or maybe I'll change my mind and do something else, who knows.
0 notes
codedrawn · 9 months ago
Text
No new design but I have implemented a better line algorithm, specifically Xiaolin Wu's line algorithm. You can see the new one on the left and the old one on the right in the image below.
Tumblr media
I think the old one actually works better for this design but it helps with small details. Here's the first design redrawn with the new algorithm.
Tumblr media
While the implementation seems to be working, it doesn't look better in this design, but lower densities do seem better so I'm counting it as a success until I have a reason not to.
Next, I want to be able to make thicker lines and the current plan for that is to draw two lines with this algorithm and fill in between with solid color. I do want to add a circle drawing algorithm first, keeping with the theme that might be Xiaolin Wu's circle drawing algorithm mentioned in the wikipedia article linked above if I can find it but a rough version could be done just using sin and cos I believe. I still plan to host my code somewhere but I haven't done anything to that end yet.
0 notes
codedrawn · 9 months ago
Text
Tumblr media
This one isn't very visually interesting, just 40 spokes, but it will be very useful as a quick test for future line draw functions. This version uses the same line function as the last one. I have been trying to get one with built in antialiasing, but I've been having some issues with getting it to work. As a demo for why this image will be useful here is the current version of that new and broken function trying to draw the same pattern.
Tumblr media
As you can see, it only seems to be able to draw lines with a slope from 0 to 1 currently (the top left is 0,0 and each line is drawn from center out). Being able to quickly output this pattern will help debugging a lot. I also need to settle on how I want to do color blending because right now its just replacing the pixels entirely when it draws a new line.
0 notes
codedrawn · 9 months ago
Text
Tumblr media
A fairly simple one to start with and I imaging a many people would immediately recognize it, I know I tried to draw this many times in notebooks rather than paying attention during school. I wanted to start this project off with making a library to handle things like storing the actual image data and setting pixels, so this was a good test for that.
The actual design is a parabola made of straight lines, so x pixels down from the top left is connected to x pixels right from the bottom right as well as to x pixels left from the top right. The result of one of these is a nice looking parabola but doing four overlapping ones makes a really nice flower-like pattern.
The lines look a little rough because I didn't want to put a lot of effort into the line drawing function today, so all it does is for 1000 values of w between 0 and 1 it finds x=x₀*w + x₁*(1-w) and y=y₀*w + y₁*(1-w) and rounds those down and sets the pixel there to black. This is fast enough for the 800 lines in the drawing but should be improved eventually. I also would like some antialiasing, which wouldn't be too hard with this method, just setting the four nearest pixels to grey based on their distance to the actual point, but at that point I'd rather just have a better line algorithm. This does give me the idea to have multiple line algorithms and other utility functions and allow them to be quickly switched out using traits in Rust and globals or something in Python and seeing how it looks with different types of lines and stuff, but that can be a future addition.
0 notes
codedrawn · 9 months ago
Text
Right now, I'm planning to have somewhere to host the code I'm using, but I don't want to use github so I'm look for alternatives. I also plan to tag the images with the language I used. To actually draw the images I use netpbm and convert that into a png to actually share, its super easy to write a program to output into a netpbm format, especially if you use the plaintext variant. The languages I will almost always write in are Python and Rust, so unless otherwise noted assume it's one of those.
0 notes