Categories
Uncategorized

Circular Positioning in OpenSCAD

This post is about a problem… it’s about looking at a problem from the wrong angle, and eventually realizing it.

I wrote some code in OpenSCAD, and then I wanted to change something and I could not figure out how to get the positioning right. I should confess I am terrible at maths. In school I took geometry, but never did calculus or trigonometry, and know almost nothing about those areas of mathematics.

Since I tend to work in Cartesian coordinate systems I think about moving things in X and Y. Left, right, up, down… Sure, there are angles, but they can be difficult. But maybe they shouldn’t be…

Here’s what I wanted to do: Create a circle with three smaller circles evenly placed into a triangle. Now typically in OpenSCAD I move things around in the X and Y and for the mostly rectilinear things I design (mostly enclosures) that’s simple, but circles can get weird. So what I tried to do was move each of the holes along the X and Y axes, but I could not get them evenly spaced…

See, this is easy for me. Four things, evenly spaced. Everything moves from the center an equal amount in X and Y. My simple maths brain has no issue with this. I’m so used to doing it this way I can bang it out without any thought. But this way (while simple) is sometimes not ideal.

As is often the case, I took to Mastodon with my problem, because there are people there much smarter than I am… Luckily @GeekAndDad was on the case! While he was busy doing the hard math I was distracted with some other projects… but when I returned I realized I knew how to do it… because I did it ten years ago! Check out this post about Derby Wheels.

Okay, so I did it before, but didn’t have easy access to the code I had written, but remembered how it worked, so I banged this out:


// Position things in a circular pattern in OpenSCAD

$fn=100;

howMany    = 3;
distance   = 10;
holeSize   = 10;
startAngle = 30;

difference() {

    // create the object
    translate([0,0,0])
        cylinder(d=40, h=3, center=true);

    // loop around in a circle and difference some holes
    for(variable = [startAngle : 360/howMany : 360+startAngle]) {
        rotate([0,0,variable])
            translate([distance,0,0])
                cylinder(d=holeSize, h=3.1, center=true);   
    }
}

(You can also find this code in my gists repository.)

Here we go! Three holes, evenly spaced… We do this by rotating around the center and placing the hole X number of units from the center, every X number of degrees. You can set the number of holes howMany, the distance from the center, and of course the holeSize and also the startAngle which helps rotate the whole set into something that matches up properly.

Hey, you can even do four holes! They are all still evenly spaced from the center…

You can do seven holes… The bit with 360/howMany does the maths to determine how many degrees there are before adding another hole.

Heck, you can even do just two. Want them top and bottom instead of left and right? Just change the startAngle appropriately.

You can put the holes on a square instead… or a rectangle, or anything, really. And they don’t even need to be holes… I just needed holes for this specific example.

Another square with some holes…

And yet another square with some holes… This time with a different startAngle set.

Those holes look a little large… can we make them smaller?

We sure can! Look at that… Obviously you are not limited to just making circles, and this technique of using a for loop to put things around the center of an object can be expanded in many ways. I just wanted to share this recent remembrance so I can find it again when I need it!

By the way, these OpenSCAD renders were all done using my Cyber Night theme for OpenSCAD.

Categories
Uncategorized

The Power Of Math

In The Power of Sync, Seth says:

100 people doing something at the same time has far more power than 300 people doing it over time.

I can see how this could make sense… but I can also see how it makes no sense. I’m no math expert but…

If 100 people buy your product at $100 at the same time, you’ve got $10,000. (You know, if your servers/systems/staff, can handle that load.)

If 300 people buy your product at $100 over the course of 3 days, you’ve got $30,000. (You’ve also got 3 times the number of customers now, which could come in handy in the future.)

Like I said, I’m no math expert… so maybe someone can explain to me why this is wrong.