Add libcircle

This commit is contained in:
maride 2018-02-05 22:11:28 +01:00
parent 16cf0a7b53
commit c5a549f07f

86
circle.scad Normal file
View File

@ -0,0 +1,86 @@
/*
* circle
*
*
* Functions:
* libcircle_quarter and its alias libcircle_14
* libcircle_half and its alias libcircle_24 and libcircle_12
* libcircle_threequarter and its alias libcircle_34
*
* All functions take either r (radius) or d (diameter) as argument.
* If both are given (for whatever reason you'd do this), radius is chosen over diameter.
* Same applies for libcircle_degrees, however you also need to pass this module
* the "degrees" argument which is pretty self-explanatory.
*/
$fn = 36;
module libcircle_14(r=0, d=0) {
libcircle_quarter(r, d);
}
module libcircle_quarter(r=0, d=0) {
diameter = __diameter_radius_conversion(r, d);
intersection() {
circle(d=diameter);
square([radius, radius]);
}
}
module libcircle_24(r=0, d=0) {
libcircle_half(r, d);
}
module libcircle_12(r=0, d=0) {
libcircle_half(r, d);
}
module libcircle_half(r=0, d=0) {
diameter = __diameter_radius_conversion(r, d);
intersection() {
circle(d=diameter);
translate([0, -diameter, 0])
square([diameter, diameter*2]);
}
}
module libcircle_34(r=0, d=0) {
libcircle_threequarter(r, d);
}
module libcircle_threequarter(r=0, d=0) {
diameter = __diameter_radius_conversion(r, d);
difference() {
circle(d=diameter);
translate([-diameter, 0, 0])
square([diameter, diameter]);
}
}
module libcircle_degrees(r=0, d=0, degrees) {
diameter = __diameter_radius_conversion(r, d);
degrees = degrees % 360;
if(degrees > 180) {
__half_circle_degrees(diameter, 180);
rotate([0, 0, 180])
__half_circle_degrees(diameter, degrees-180);
} else {
__half_circle_degrees(diameter, degrees);
}
}
module __half_circle_degrees(diameter, degrees) {
// This is the most dirty hack I've pulled in a while.
// But... Who cares? #sorrynotsorry ;)
difference() {
libcircle_half(d=diameter);
rotate([0, 0, degrees])
translate([0, -diameter/2])
square([diameter/2, diameter]);
}
}
// https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/General#Scope_of_variables
// let's work our way around this... ;(
function __diameter_radius_conversion(r=0, d=0) = (r > 0) ? r*2 : d;