diff --git a/README.md b/README.md index b6749dba..e69de29b 100644 --- a/README.md +++ b/README.md @@ -1,2 +0,0 @@ -# lib-openscad -OpenSCAD libraries diff --git a/docs/images/line2d-1.JPG b/docs/images/line2d-1.JPG new file mode 100644 index 00000000..f13e612c Binary files /dev/null and b/docs/images/line2d-1.JPG differ diff --git a/docs/images/polyline2d-1.JPG b/docs/images/polyline2d-1.JPG new file mode 100644 index 00000000..43cea3b3 Binary files /dev/null and b/docs/images/polyline2d-1.JPG differ diff --git a/docs/images/polyline2d-2.JPG b/docs/images/polyline2d-2.JPG new file mode 100644 index 00000000..bf67f2ae Binary files /dev/null and b/docs/images/polyline2d-2.JPG differ diff --git a/docs/images/polyline2d-3.JPG b/docs/images/polyline2d-3.JPG new file mode 100644 index 00000000..abf10bae Binary files /dev/null and b/docs/images/polyline2d-3.JPG differ diff --git a/docs/line2d.md b/docs/line2d.md new file mode 100644 index 00000000..adc58d80 --- /dev/null +++ b/docs/line2d.md @@ -0,0 +1,26 @@ +# line2d + +Creates a line from two points. + +## Parameters + +- `p1` : 2 element vector `[x, y]`. +- `p2` : 2 element vector `[x, y]`. +- `width` : The line width. +- `p1Style` : The end-cap style of the point `p1`. The value must be `CAP_BUTT`, `CAP_SQUARE` or `CAP_ROUND`. The default value is `CAP_SQUARE`. +- `p2Style` : The end-cap style of the point `p2`. The value must be `CAP_BUTT`, `CAP_SQUARE` or `CAP_ROUND`. The default value is `CAP_SQUARE`. +- `round_fn` : When the end-cap style is `CAP_ROUND`, it controlls the `$fn` value used by the `circle` module. The default value is `24`. + +## Examples + + line2d(p1 = [0, 0], p2 = [5, 0], width = 1); + + translate([0, -2, 0]) + line2d(p1 = [0, 0], p2 = [5, 0], width = 1, + p1Style = CAP_ROUND, p2Style = CAP_ROUND); + + translate([0, -4, 0]) + line2d(p1 = [0, 0], p2 = [5, 0], width = 1, + p1Style = CAP_BUTT, p2Style = CAP_BUTT); + +![line2d](images/line2d-1.JPG) diff --git a/docs/polyline2d.md b/docs/polyline2d.md new file mode 100644 index 00000000..8a24e422 --- /dev/null +++ b/docs/polyline2d.md @@ -0,0 +1,27 @@ +# polyline2d + +Creates a polyline from a list of `x`, `y` coordinates. It depends on the `line2d` module so you have to `include` line2d.scad. + +## Parameters + +- `points` : The list of `x`, `y` points of the polyline. : A vector of 2 element vectors. The points are indexed from 0 to n-1. +- `width` : The line width. +- `startingStyle` : The end-cap style of the starting point. The value must be `CAP_BUTT`, `CAP_SQUARE` or `CAP_ROUND` (defined in line2d.scad). The default value is `CAP_SQUARE`. +- endingStyle : The end-cap style of the ending point. The value must be `CAP_BUTT`, `CAP_SQUARE` or `CAP_ROUND` (defined in line2d.scad). The default value is `CAP_SQUARE`. +- `round_fn` = When the end-cap style is `CAP_ROUND`, it controlls the `$fn` value used by the `circle` module. The default value is `24`. + +## Examples + + polyline2d(points = [[1, 2], [-5, -4], [-5, 3], [5, 5]], width = 1); + +![polyline2d](images/polyline2d-1.JPG) + + polyline2d(points = [[1, 2], [-5, -4], [-5, 3], [5, 5]], width = 1, + endingStyle = CAP_ROUND); + +![polyline2d](images/polyline2d-2.JPG) + + polyline2d(points = [[1, 2], [-5, -4], [-5, 3], [5, 5]], width = 1, + startingStyle = CAP_ROUND, endingStyle = CAP_ROUND); + +![polyline2d](images/polyline2d-3.JPG) \ No newline at end of file diff --git a/src/line2d.scad b/src/line2d.scad new file mode 100644 index 00000000..0055d2c0 --- /dev/null +++ b/src/line2d.scad @@ -0,0 +1,46 @@ +// The end-cap style +CAP_BUTT = 0; +CAP_SQUARE = 1; +CAP_ROUND = 2; + +module line2d(p1, p2, width, p1Style = CAP_SQUARE, p2Style = CAP_SQUARE, round_fn = 24) { + $fn = round_fn; + half_width = 0.5 * width; + + atan_angle = atan2(p2[1] - p1[1], p2[0] - p1[0]); + angle = 90 - atan_angle; + + offset_x = half_width * cos(angle); + offset_y = half_width * sin(angle); + + offset1 = [-offset_x, offset_y]; + offset2 = [offset_x, -offset_y]; + + polygon(points=[ + p1 + offset1, p2 + offset1, + p2 + offset2, p1 + offset2 + ]); + + module square_end(point) { + translate(point) + rotate(atan_angle) + square(width, center = true); + } + + module round_end(point) { + translate(point) + circle(half_width, center = true); + } + + if(p1Style == CAP_SQUARE) { + square_end(p1); + } else if(p1Style == CAP_ROUND) { + round_end(p1); + } + + if(p2Style == CAP_SQUARE) { + square_end(p2); + } else if(p2Style == CAP_ROUND) { + round_end(p2); + } +} \ No newline at end of file diff --git a/src/polyline2d.scad b/src/polyline2d.scad new file mode 100644 index 00000000..b3b23084 --- /dev/null +++ b/src/polyline2d.scad @@ -0,0 +1,22 @@ +module polyline2d(points, width, startingStyle = CAP_SQUARE, endingStyle = CAP_SQUARE, round_fn = 24) { + module line_segment(index) { + styles = index == 1 ? [startingStyle, CAP_ROUND] : ( + index == len(points) - 1 ? [CAP_ROUND, endingStyle] : [ + CAP_ROUND, CAP_ROUND + ] + ); + + line2d(points[index - 1], points[index], width, + p1Style = styles[0], p2Style = styles[1], + round_fn = round_fn); + } + + module polyline2d_inner(points, index) { + if(index < len(points)) { + line_segment(index); + polyline2d_inner(points, index + 1); + } + } + + polyline2d_inner(points, 1); +} \ No newline at end of file