Skip to content

Commit 3682f7b

Browse files
author
Garth Minette
committed
Added dashed_stroke(). path_cut() -> path_cut_points().
1 parent 1ee6e3a commit 3682f7b

5 files changed

Lines changed: 405 additions & 167 deletions

File tree

coords.scad

Lines changed: 100 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,27 @@
99
// Section: Coordinate Manipulation
1010

1111
// Function: point2d()
12+
// Usage:
13+
// pt = point2d(p, <fill>);
14+
// Topics: Coordinates, Points
15+
// See Also: path2d(), point3d(), path3d()
1216
// Description:
13-
// Returns a 2D vector/point from a 2D or 3D vector.
14-
// If given a 3D point, removes the Z coordinate.
17+
// Returns a 2D vector/point from a 2D or 3D vector. If given a 3D point, removes the Z coordinate.
1518
// Arguments:
1619
// p = The coordinates to force into a 2D vector/point.
1720
// fill = Value to fill missing values in vector with.
1821
function point2d(p, fill=0) = [for (i=[0:1]) (p[i]==undef)? fill : p[i]];
1922

2023

2124
// Function: path2d()
25+
// Usage:
26+
// pts = path2d(points);
27+
// Topics: Coordinates, Points, Paths
28+
// See Also: point2d(), point3d(), path3d()
2229
// Description:
23-
// Returns a list of 2D vectors/points from a list of 2D, 3D or higher
24-
// dimensional vectors/points. Removes the extra coordinates from
25-
// higher dimensional points. The input must be a path, where
26-
// every vector has the same length.
30+
// Returns a list of 2D vectors/points from a list of 2D, 3D or higher dimensional vectors/points.
31+
// Removes the extra coordinates from higher dimensional points. The input must be a path, where
32+
// every vector has the same length.
2733
// Arguments:
2834
// points = A list of 2D or 3D points/vectors.
2935
function path2d(points) =
@@ -34,6 +40,10 @@ function path2d(points) =
3440

3541

3642
// Function: point3d()
43+
// Usage:
44+
// pt = point3d(p, <fill>);
45+
// Topics: Coordinates, Points
46+
// See Also: path2d(), point2d(), path3d()
3747
// Description:
3848
// Returns a 3D vector/point from a 2D or 3D vector.
3949
// Arguments:
@@ -43,6 +53,10 @@ function point3d(p, fill=0) = [for (i=[0:2]) (p[i]==undef)? fill : p[i]];
4353

4454

4555
// Function: path3d()
56+
// Usage:
57+
// pts = path3d(points, <fill>);
58+
// Topics: Coordinates, Points, Paths
59+
// See Also: point2d(), path2d(), point3d()
4660
// Description:
4761
// Returns a list of 3D vectors/points from a list of 2D or higher dimensional vectors/points
4862
// by removing extra coordinates or adding the z coordinate.
@@ -63,6 +77,10 @@ function path3d(points, fill=0) =
6377

6478

6579
// Function: point4d()
80+
// Usage:
81+
// pt = point4d(p, <fill>);
82+
// Topics: Coordinates, Points
83+
// See Also: point2d(), path2d(), point3d(), path3d(), path4d()
6684
// Description:
6785
// Returns a 4D vector/point from a 2D or 3D vector.
6886
// Arguments:
@@ -72,12 +90,15 @@ function point4d(p, fill=0) = [for (i=[0:3]) (p[i]==undef)? fill : p[i]];
7290

7391

7492
// Function: path4d()
93+
// Usage:
94+
// pt = path4d(points, <fill>);
95+
// Topics: Coordinates, Points, Paths
96+
// See Also: point2d(), path2d(), point3d(), path3d(), point4d()
7597
// Description:
7698
// Returns a list of 4D vectors/points from a list of 2D or 3D vectors/points.
7799
// Arguments:
78100
// points = A list of 2D or 3D points/vectors.
79101
// fill = Value to fill missing values in vectors with.
80-
81102
function path4d(points, fill=0) =
82103
assert(is_num(fill) || is_vector(fill))
83104
assert(is_path(points, dim=undef, fast=true), "Input to path4d is not a path")
@@ -102,8 +123,10 @@ function path4d(points, fill=0) =
102123

103124
// Function: polar_to_xy()
104125
// Usage:
105-
// polar_to_xy(r, theta);
106-
// polar_to_xy([r, theta]);
126+
// pt = polar_to_xy(r, theta);
127+
// pt = polar_to_xy([r, theta]);
128+
// Topics: Coordinates, Points, Paths
129+
// See Also: xy_to_polar(), xyz_to_cylindrical(), cylindrical_to_xyz(), xyz_to_spherical(), spherical_to_xyz()
107130
// Description:
108131
// Convert polar coordinates to 2D cartesian coordinates.
109132
// Returns [X,Y] cartesian coordinates.
@@ -114,6 +137,13 @@ function path4d(points, fill=0) =
114137
// xy = polar_to_xy(20,45); // Returns: ~[14.1421365, 14.1421365]
115138
// xy = polar_to_xy(40,30); // Returns: ~[34.6410162, 15]
116139
// xy = polar_to_xy([40,30]); // Returns: ~[34.6410162, 15]
140+
// Example(2D):
141+
// r=40; ang=30; $fn=36;
142+
// pt = polar_to_xy(r,ang);
143+
// stroke(circle(r=r), closed=true, width=0.5);
144+
// color("black") stroke([[r,0], [0,0], pt], width=0.5);
145+
// color("black") stroke(arc(r=15, angle=ang), width=0.5);
146+
// color("red") move(pt) circle(d=3);
117147
function polar_to_xy(r,theta=undef) = let(
118148
rad = theta==undef? r[0] : r,
119149
t = theta==undef? r[1] : theta
@@ -122,8 +152,10 @@ function polar_to_xy(r,theta=undef) = let(
122152

123153
// Function: xy_to_polar()
124154
// Usage:
125-
// xy_to_polar(x,y);
126-
// xy_to_polar([X,Y]);
155+
// r_theta = xy_to_polar(x,y);
156+
// r_theta = xy_to_polar([X,Y]);
157+
// Topics: Coordinates, Points, Paths
158+
// See Also: polar_to_xy(), xyz_to_cylindrical(), cylindrical_to_xyz(), xyz_to_spherical(), spherical_to_xyz()
127159
// Description:
128160
// Convert 2D cartesian coordinates to polar coordinates.
129161
// Returns [radius, theta] where theta is the angle counter-clockwise of X+.
@@ -133,6 +165,13 @@ function polar_to_xy(r,theta=undef) = let(
133165
// Examples:
134166
// plr = xy_to_polar(20,30);
135167
// plr = xy_to_polar([40,60]);
168+
// Example(2D):
169+
// pt = [-20,30]; $fn = 36;
170+
// rt = xy_to_polar(pt);
171+
// r = rt[0]; ang = rt[1];
172+
// stroke(circle(r=r), closed=true, width=0.5);
173+
// zrot(ang) stroke([[0,0],[r,0]],width=0.5);
174+
// color("red") move(pt) circle(d=3);
136175
function xy_to_polar(x,y=undef) = let(
137176
xx = y==undef? x[0] : x,
138177
yy = y==undef? x[1] : y
@@ -141,11 +180,13 @@ function xy_to_polar(x,y=undef) = let(
141180

142181
// Function: project_plane()
143182
// Usage: With the plane defined by 3 Points
144-
// xyz = project_plane(point, a, b, c);
183+
// pt = project_plane(point, a, b, c);
145184
// Usage: With the plane defined by Pointlist
146-
// xyz = project_plane(point, POINTLIST);
185+
// pt = project_plane(point, POINTLIST);
147186
// Usage: With the plane defined by Plane Definition [A,B,C,D] Where Ax+By+Cz=D
148-
// xyz = project_plane(point, PLANE);
187+
// pt = project_plane(point, PLANE);
188+
// Topics: Coordinates, Points, Paths
189+
// See Also: lift_plane()
149190
// Description:
150191
// Converts the given 3D points from global coordinates to the 2D planar coordinates of the closest
151192
// points on the plane. This coordinate system can be useful in taking a set of nearly coplanar
@@ -165,6 +206,20 @@ function xy_to_polar(x,y=undef) = let(
165206
// a=[0,0,0]; b=[10,-10,0]; c=[10,0,10];
166207
// xy = project_plane(pt, a, b, c);
167208
// xy2 = project_plane(pt, [a,b,c]);
209+
// Example(3D):
210+
// points = move([10,20,30], p=yrot(25, p=path3d(circle(d=100, $fn=36))));
211+
// plane = plane_from_normal([1,0,1]);
212+
// proj = project_plane(points,plane);
213+
// n = plane_normal(plane);
214+
// cp = centroid(proj);
215+
// color("red") move_copies(points) sphere(d=2,$fn=12);
216+
// color("blue") rot(from=UP,to=n,cp=cp) move_copies(proj) sphere(d=2,$fn=12);
217+
// move(cp) {
218+
// rot(from=UP,to=n) {
219+
// anchor_arrow(30);
220+
// %cube([120,150,0.1],center=true);
221+
// }
222+
// }
168223
function project_plane(point, a, b, c) =
169224
is_undef(b) && is_undef(c) && is_list(a)? let(
170225
mat = is_vector(a,4)? plane_transform(a) :
@@ -195,6 +250,8 @@ function project_plane(point, a, b, c) =
195250
// xyz = lift_plane(point, POINTLIST);
196251
// Usage: With Plane Definition [A,B,C,D] Where Ax+By+Cz=D
197252
// xyz = lift_plane(point, PLANE);
253+
// Topics: Coordinates, Points, Paths
254+
// See Also: project_plane()
198255
// Description:
199256
// Converts the given 2D point from planar coordinates to the global 3D coordinates of the point on the plane.
200257
// Can be called one of three ways:
@@ -232,8 +289,10 @@ function lift_plane(point, a, b, c) =
232289

233290
// Function: cylindrical_to_xyz()
234291
// Usage:
235-
// cylindrical_to_xyz(r, theta, z)
236-
// cylindrical_to_xyz([r, theta, z])
292+
// pt = cylindrical_to_xyz(r, theta, z);
293+
// pt = cylindrical_to_xyz([r, theta, z]);
294+
// Topics: Coordinates, Points, Paths
295+
// See Also: xyz_to_cylindrical(), xyz_to_spherical(), spherical_to_xyz()
237296
// Description:
238297
// Convert cylindrical coordinates to 3D cartesian coordinates. Returns [X,Y,Z] cartesian coordinates.
239298
// Arguments:
@@ -252,12 +311,13 @@ function cylindrical_to_xyz(r,theta=undef,z=undef) = let(
252311

253312
// Function: xyz_to_cylindrical()
254313
// Usage:
255-
// xyz_to_cylindrical(x,y,z)
256-
// xyz_to_cylindrical([X,Y,Z])
314+
// rtz = xyz_to_cylindrical(x,y,z);
315+
// rtz = xyz_to_cylindrical([X,Y,Z]);
316+
// Topics: Coordinates, Points, Paths
317+
// See Also: cylindrical_to_xyz(), xyz_to_spherical(), spherical_to_xyz()
257318
// Description:
258-
// Convert 3D cartesian coordinates to cylindrical coordinates.
259-
// Returns [radius,theta,Z]. Theta is the angle counter-clockwise
260-
// of X+ on the XY plane. Z is height above the XY plane.
319+
// Convert 3D cartesian coordinates to cylindrical coordinates. Returns [radius,theta,Z].
320+
// Theta is the angle counter-clockwise of X+ on the XY plane. Z is height above the XY plane.
261321
// Arguments:
262322
// x = X coordinate.
263323
// y = Y coordinate.
@@ -272,11 +332,12 @@ function xyz_to_cylindrical(x,y=undef,z=undef) = let(
272332

273333
// Function: spherical_to_xyz()
274334
// Usage:
275-
// spherical_to_xyz(r, theta, phi);
276-
// spherical_to_xyz([r, theta, phi]);
335+
// pt = spherical_to_xyz(r, theta, phi);
336+
// pt = spherical_to_xyz([r, theta, phi]);
277337
// Description:
278-
// Convert spherical coordinates to 3D cartesian coordinates.
279-
// Returns [X,Y,Z] cartesian coordinates.
338+
// Convert spherical coordinates to 3D cartesian coordinates. Returns [X,Y,Z] cartesian coordinates.
339+
// Topics: Coordinates, Points, Paths
340+
// See Also: cylindrical_to_xyz(), xyz_to_spherical(), xyz_to_cylindrical()
280341
// Arguments:
281342
// r = distance from origin.
282343
// theta = angle in degrees, counter-clockwise of X+ on the XY plane.
@@ -293,12 +354,13 @@ function spherical_to_xyz(r,theta=undef,phi=undef) = let(
293354

294355
// Function: xyz_to_spherical()
295356
// Usage:
296-
// xyz_to_spherical(x,y,z)
297-
// xyz_to_spherical([X,Y,Z])
357+
// r_theta_phi = xyz_to_spherical(x,y,z)
358+
// r_theta_phi = xyz_to_spherical([X,Y,Z])
359+
// Topics: Coordinates, Points, Paths
360+
// See Also: cylindrical_to_xyz(), spherical_to_xyz(), xyz_to_cylindrical()
298361
// Description:
299-
// Convert 3D cartesian coordinates to spherical coordinates.
300-
// Returns [r,theta,phi], where phi is the angle from the Z+ pole,
301-
// and theta is degrees counter-clockwise of X+ on the XY plane.
362+
// Convert 3D cartesian coordinates to spherical coordinates. Returns [r,theta,phi], where phi is
363+
// the angle from the Z+ pole, and theta is degrees counter-clockwise of X+ on the XY plane.
302364
// Arguments:
303365
// x = X coordinate.
304366
// y = Y coordinate.
@@ -313,8 +375,10 @@ function xyz_to_spherical(x,y=undef,z=undef) = let(
313375

314376
// Function: altaz_to_xyz()
315377
// Usage:
316-
// altaz_to_xyz(alt, az, r);
317-
// altaz_to_xyz([alt, az, r]);
378+
// pt = altaz_to_xyz(alt, az, r);
379+
// pt = altaz_to_xyz([alt, az, r]);
380+
// Topics: Coordinates, Points, Paths
381+
// See Also: cylindrical_to_xyz(), xyz_to_spherical(), spherical_to_xyz(), xyz_to_cylindrical(), xyz_to_altaz()
318382
// Description:
319383
// Convert altitude/azimuth/range coordinates to 3D cartesian coordinates.
320384
// Returns [X,Y,Z] cartesian coordinates.
@@ -334,8 +398,10 @@ function altaz_to_xyz(alt,az=undef,r=undef) = let(
334398

335399
// Function: xyz_to_altaz()
336400
// Usage:
337-
// xyz_to_altaz(x,y,z);
338-
// xyz_to_altaz([X,Y,Z]);
401+
// alt_az_r = xyz_to_altaz(x,y,z);
402+
// alt_az_r = xyz_to_altaz([X,Y,Z]);
403+
// Topics: Coordinates, Points, Paths
404+
// See Also: cylindrical_to_xyz(), xyz_to_spherical(), spherical_to_xyz(), xyz_to_cylindrical(), altaz_to_xyz()
339405
// Description:
340406
// Convert 3D cartesian coordinates to altitude/azimuth/range coordinates.
341407
// Returns [altitude,azimuth,range], where altitude is angle above the

geometry.scad

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -999,10 +999,11 @@ function plane_transform(plane) =
999999

10001000
// Function: projection_on_plane()
10011001
// Usage:
1002-
// projection_on_plane(points);
1002+
// pts = projection_on_plane(plane, points);
10031003
// Description:
1004-
// Given a plane definition `[A,B,C,D]`, where `Ax+By+Cz=D`, and a list of 2d or 3d points, return the 3D orthogonal
1005-
// projection of the points on the plane.
1004+
// Given a plane definition `[A,B,C,D]`, where `Ax+By+Cz=D`, and a list of 2d or
1005+
// 3d points, return the 3D orthogonal projection of the points on the plane.
1006+
// In other words, for every point given, returns the closest point to it on the plane.
10061007
// Arguments:
10071008
// plane = The `[A,B,C,D]` plane definition where `Ax+By+Cz=D` is the formula of the plane.
10081009
// points = List of points to project
@@ -1061,24 +1062,6 @@ function distance_from_plane(plane, point) =
10611062
point3d(plane)* point - plane[3];
10621063

10631064

1064-
// Function: closest_point_on_plane()
1065-
// Usage:
1066-
// pt = closest_point_on_plane(plane, point);
1067-
// Description:
1068-
// Takes a point, and a plane [A,B,C,D] where the equation of that plane is `Ax+By+Cz=D`.
1069-
// Returns the coordinates of the closest point on that plane to the given `point`.
1070-
// Arguments:
1071-
// plane = The [A,B,C,D] coefficients for the equation of the plane.
1072-
// point = The 3D point to find the closest point to.
1073-
function closest_point_on_plane(plane, point) =
1074-
assert( _valid_plane(plane), "Invalid input plane." )
1075-
assert( is_vector(point,3), "Invalid point." )
1076-
let( plane = normalize_plane(plane),
1077-
n = point3d(plane),
1078-
d = n*point - plane[3] // distance from plane
1079-
)
1080-
point - n*d;
1081-
10821065

10831066
// Returns [POINT, U] if line intersects plane at one point.
10841067
// Returns [LINE, undef] if the line is on the plane.

0 commit comments

Comments
 (0)