A Point3d represents a position in 3D space and is defined by its X, Y and Z
coordinates. This module contains a variety of point-related functionality, such
as
Points are distinct from vectors but interact with them in well-defined ways; you can translate a point by a vector to result in a new point, or you can compute the vector from one point to another, but you cannot 'add' two points like you can add two vectors.
The point (0, 0, 0).
Point3d.origin
--> Point3d.fromCoordinates ( 0, 0, 0 )Construct a point from its X, Y and Z coordinates.
point =
Point3d.fromCoordinates ( 2, 1, 3 )Construct a point halfway between two other points.
p1 =
Point3d.fromCoordinates ( 1, 1, 1 )
p2 =
Point3d.fromCoordinates ( 3, 7, 9 )
Point3d.midpoint p1 p2
--> Point3d.fromCoordinates ( 2, 4, 5 )Construct a point by interpolating from the first given point to the second, based on a parameter that ranges from zero to one.
startPoint =
Point3d.fromCoordinates ( 1, 2, 4 )
endPoint =
Point3d.fromCoordinates ( 1, 2, 8 )
Point3d.interpolateFrom startPoint endPoint 0.25
--> Point3d.fromCoordinates ( 1, 2, 5 )Partial application may be useful:
interpolatedPoint : Float -> Point3d
interpolatedPoint =
Point3d.interpolateFrom startPoint endPoint
List.map interpolatedPoint [ 0, 0.5, 1 ]
--> [ Point3d.fromCoordinates ( 1, 2, 4 )
--> , Point3d.fromCoordinates ( 1, 2, 6 )
--> , Point3d.fromCoordinates ( 1, 2, 8 )
--> ]You can pass values less than zero or greater than one to extrapolate:
interpolatedPoint -0.5
--> Point3d.fromCoordinates ( 1, 2, 2 )
interpolatedPoint 1.25
--> Point3d.fromCoordinates ( 1, 2, 9 )Construct a point along an axis at a particular distance from the axis' origin point.
Point3d.along Axis3d.z 2
--> Point3d.fromCoordinates ( 0, 0, 2 )Positive and negative distances are interpreted relative to the direction of the axis:
horizontalAxis =
Axis3d.with
{ originPoint =
Point3d.fromCoordinates ( 1, 1, 1 )
, direction = Direction3d.negativeX
}
Point3d.along horizontalAxis 3
--> Point3d.fromCoordinates ( -2, 1, 1 )
Point3d.along horizontalAxis -3
--> Point3d.fromCoordinates ( 4, 1, 1 )Construct a 3D point lying on a sketch plane by providing a 2D point specified in XY coordinates within the sketch plane.
Point3d.on SketchPlane3d.xy <|
Point2d.fromCoordinates ( 2, 1 )
--> Point3d.fromCoordinates ( 2, 1, 0 )
Point3d.on SketchPlane3d.xz <|
Point2d.fromCoordinates ( 2, 1 )
--> Point3d.fromCoordinates ( 2, 0, 1 )The sketch plane can have any position and orientation:
tiltedSketchPlane =
SketchPlane3d.xy
|> SketchPlane3d.rotateAround Axis3d.x
(degrees 45)
|> SketchPlane3d.moveTo
(Point3d.fromCoordinates ( 10, 10, 10 ))
Point3d.on tiltedSketchPlane <|
Point2d.fromCoordinates ( 2, 1 )
--> Point3d.fromCoordinates ( 12, 10.7071, 10.7071 )Construct a point given its local coordinates within a particular frame.
frame =
Frame3d.atPoint
(Point3d.fromCoordinates ( 1, 1, 1 ))
Point3d.in_ frame ( 1, 2, 3 )
--> Point3d.fromCoordinates ( 2, 3, 4 )This is shorthand for using Point3d.placeIn;
Point3d.in_ frame coordinatesis equivalent to
Point3d.placeIn frame
(Point3d.fromCoordinates coordinates)Attempt to find the circumcenter of three points; this is the center of the
circle that passes through all three points. If the three given points are
collinear, returns Nothing.
Point3d.circumcenter
( Point3d.fromCoordinates ( 1, 0, 0 )
, Point3d.fromCoordinates ( 0, 1, 0 )
, Point3d.fromCoordinates ( 0, 0, 1 )
)
--> Just (Point3d.fromCoordinates (0.33, 0.33, 0.33))
Point3d.circumcenter
( Point3d.origin
, Point3d.fromCoordinates ( 1, 0, 0 )
, Point3d.fromCoordinates ( 2, 0, 0 )
)
--> NothingGet the coordinates of a point as a tuple.
( x, y, z ) =
Point3d.coordinates pointGet the X coordinate of a point.
Point3d.fromCoordinates ( 2, 1, 3 )
|> Point3d.xCoordinate
--> 2Get the Y coordinate of a point.
Point3d.fromCoordinates ( 2, 1, 3 )
|> Point3d.yCoordinate
--> 1Get the Z coordinate of a point.
Point3d.fromCoordinates ( 2, 1, 3 )
|> Point3d.zCoordinate
--> 3Compare two points within a tolerance. Returns true if the distance between the two given points is less than the given tolerance.
firstPoint =
Point3d.fromCoordinates ( 2, 1, 3 )
secondPoint =
Point3d.fromCoordinates ( 2.0002, 0.9999, 3.0001 )
Point3d.equalWithin 1e-3 firstPoint secondPoint
--> True
Point3d.equalWithin 1e-6 firstPoint secondPoint
--> FalseFind the distance from the first point to the second.
p1 =
Point3d.fromCoordinates ( 1, 1, 2 )
p2 =
Point3d.fromCoordinates ( 2, 3, 4 )
Point3d.distanceFrom p1 p2
--> 3Partial application can be useful:
points =
[ Point3d.fromCoordinates ( 3, 4, 5 )
, Point3d.fromCoordinates ( 10, 10, 10 )
, Point3d.fromCoordinates ( -1, 2, -3 )
]
points
|> List.sortBy
(Point3d.distanceFrom Point3d.origin)
--> [ Point3d.fromCoordinates ( -1, 2, -3 )
--> , Point3d.fromCoordinates ( 3, 4, 5 )
--> , Point3d.fromCoordinates ( 10, 10, 10 )
--> ]Find the square of the distance from one point to another.
squaredDistanceFrom is slightly faster than distanceFrom, so for example
Point3d.squaredDistanceFrom p1 p2
> tolerance * toleranceis equivalent to but slightly more efficient than
Point3d.distanceFrom p1 p2 > tolerancesince the latter requires a square root under the hood. In many cases, however,
the speed difference will be negligible and using distanceFrom is much more
readable!
Determine how far along an axis a particular point lies. Conceptually, the point is projected perpendicularly onto the axis, and then the distance of this projected point from the axis' origin point is measured. The result will be positive if the projected point is ahead the axis' origin point and negative if it is behind, with 'ahead' and 'behind' defined by the direction of the axis.
axis =
Axis3d.with
{ originPoint =
Point3d.fromCoordinates ( 1, 0, 0 )
, direction = Direction3d.x
}
point =
Point3d.fromCoordinates ( 3, 3, 3 )
Point3d.signedDistanceAlong axis point
--> 2
Point3d.signedDistanceAlong axis Point3d.origin
--> -1DEPRECATED: Alias for signedDistanceAlong, kept for compatibility. Use
signedDistanceAlong instead.
Find the perpendicular (nearest) distance of a point from an axis.
point =
Point3d.fromCoordinates ( -3, 4, 0 )
Point3d.distanceFromAxis Axis3d.x point
--> 4
Point3d.distanceFromAxis Axis3d.y point
--> 3
Point3d.distanceFromAxis Axis3d.z point
--> 5Note that unlike in 2D, the result is always positive (unsigned) since there is no such thing as the left or right side of an axis in 3D.
Find the square of the perpendicular distance of a point from an axis. As
with distanceFrom/squaredDistanceFrom this is slightly more efficient than
distanceFromAxis since it avoids a square root.
Find the perpendicular distance of a point from a plane. The result will be positive if the point is 'above' the plane and negative if it is 'below', with 'up' defined by the normal direction of the plane.
plane =
Plane3d.with
{ originPoint =
Point3d.fromCoordinates ( 1, 2, 3 )
, normalDirection = Direction3d.y
}
point =
Point3d.fromCoordinates ( 3, 3, 3 )
Point3d.signedDistanceFrom plane point
--> 1
Point3d.signedDistanceFrom plane Point3d.origin
--> -2This means that flipping a plane (reversing its normal direction) will also flip the sign of the result of this function:
flippedPlane =
Plane3d.flip plane
Point3d.signedDistanceFrom flippedPlane point
--> -1
Point3d.signedDistanceFrom flippedPlane Point3d.origin
--> 2Perform a uniform scaling about the given center point. The center point is given first and the point to transform is given last. Points will contract or expand about the center point by the given scale. Scaling by a factor of 1 is a no-op, and scaling by a factor of 0 collapses all points to the center point.
centerPoint =
Point3d.fromCoordinates ( 1, 1, 1 )
point =
Point3d.fromCoordinates ( 1, 2, 3 )
Point3d.scaleAbout centerPoint 3 point
--> Point3d.fromCoordinates ( 1, 4, 7 )
Point3d.scaleAbout centerPoint 0.5 point
--> Point3d.fromCoordinates ( 1, 1.5, 2 )Avoid scaling by a negative scaling factor - while this may sometimes do what you want it is confusing and error prone. Try a combination of mirror and/or rotation operations instead.
Rotate a point around an axis by a given angle (in radians).
axis =
Axis3d.x
angle =
degrees 45
point =
Point3d.fromCoordinates ( 3, 1, 0 )
Point3d.rotateAround axis angle point
--> Point3d.fromCoordinates ( 3, 0.7071, 0.7071 )Rotation direction is given by the right-hand rule, counterclockwise around the direction of the axis.
Translate a point by a given displacement.
point =
Point3d.fromCoordinates ( 3, 4, 5 )
displacement =
Vector3d.fromComponents ( 1, 2, 3 )
Point3d.translateBy displacement point
--> Point3d.fromCoordinates ( 4, 6, 8 )Mirror a point across a plane. The result will be the same distance from the plane but on the opposite side.
point =
Point3d.fromCoordinates ( 1, 2, 3 )
-- Plane3d.xy is the plane Z=0
Point3d.mirrorAcross Plane3d.xy point
--> Point3d.fromCoordinates ( 1, 2, -3 )
-- Plane3d.yz is the plane X=0
Point3d.mirrorAcross Plane3d.yz point
--> Point3d.fromCoordinates ( -1, 2, 3 )The plane does not have to pass through the origin:
-- offsetPlane is the plane Z=1
offsetPlane =
Plane3d.offsetBy 1 Plane3d.xy
-- The origin point is 1 unit below the offset
-- plane, so its mirrored copy is one unit above
Point3d.mirrorAcross offsetPlane Point3d.origin
--> Point3d.fromCoordinates ( 0, 0, 2 )Find the orthographic projection of a point onto a plane:
point =
Point3d.fromCoordinates ( 1, 2, 3 )
Point3d.projectOnto Plane3d.xy point
--> Point3d.fromCoordinates ( 1, 2, 0 )
Point3d.projectOnto Plane3d.yz point
--> Point3d.fromCoordinates ( 0, 2, 3 )The plane does not have to pass through the origin:
offsetPlane =
Plane3d.offsetBy 1 Plane3d.xy
Point3d.projectOnto offsetPlane point
--> Point3d.fromCoordinates ( 1, 2, 1 )Project a point perpendicularly onto an axis.
point =
Point3d.fromCoordinates ( 1, 2, 3 )
Point3d.projectOntoAxis Axis3d.x
--> Point3d.fromCoordinates ( 1, 0, 0 )
verticalAxis =
Axis3d.with
{ originPoint =
Point3d.fromCoordinates ( 0, 1, 2 )
, direction = Direction3d.z
}
Point3d.projectOntoAxis verticalAxis
--> Point3d.fromCoordinates ( 0, 1, 3 )Functions for transforming points between local and global coordinates in different coordinate frames.
Take a point defined in global coordinates, and return it expressed in local coordinates relative to a given reference frame.
localFrame =
Frame3d.atPoint
(Point3d.fromCoordinates ( 1, 2, 3 ))
Point3d.relativeTo localFrame
(Point3d.fromCoordinates ( 4, 5, 6 ))
--> Point3d.fromCoordinates ( 3, 3, 3 )
Point3d.relativeTo localFrame
(Point3d.fromCoordinates ( 1, 1, 1 ))
--> Point3d.fromCoordinates ( 0, -1, -2 )Take a point defined in local coordinates relative to a given reference frame, and return that point expressed in global coordinates.
localFrame =
Frame3d.atPoint
(Point3d.fromCoordinates ( 1, 2, 3 ))
Point3d.placeIn localFrame
(Point3d.fromCoordinates ( 3, 3, 3 ))
--> Point3d.fromCoordinates ( 4, 5, 6 )
Point3d.placeIn localFrame
(Point3d.fromCoordinates ( 0, -1, -2 ))
--> Point3d.fromCoordinates ( 1, 1, 1 )Project a point into a given sketch plane. Conceptually, this finds the orthographic projection of the point onto the plane and then expresses the projected point in 2D sketch coordinates.
point =
Point3d.fromCoordinates ( 2, 1, 3 )
Point3d.projectInto SketchPlane3d.xy point
--> Point2d.fromCoordinates ( 2, 1 )
Point3d.projectInto SketchPlane3d.yz point
--> Point2d.fromCoordinates ( 1, 3 )
Point3d.projectInto SketchPlane3d.zx point
--> Point2d.fromCoordinates ( 3, 2 )Construct a bounding box containing both of the given points.
point1 =
Point3d.fromCoordinates ( 2, 1, 3 )
point2 =
Point3d.fromCoordinates ( -1, 5, -2 )
Point3d.hull point1 point2
--> BoundingBox3d.with
--> { minX = -1
--> , maxX = 2
--> , minY = 1
--> , maxY = 5
--> , minZ = -2
--> , maxZ = 3
--> }Construct a bounding box containing all points in the given list. If the
list is empty, returns Nothing.
points =
[ Point3d.fromCoordinates ( 2, 1, 3 )
, Point3d.fromCoordinates ( -1, 5, -2 )
, Point3d.fromCoordinates ( 6, 4, 2 )
]
Point3d.hullOf points
--> Just
--> (BoundingBox3d.with
--> { minX = -1
--> , maxX = 6
--> , minY = 1
--> , maxY = 5
--> , minZ = -2
--> , maxZ = 3
--> }
--> )
Point3d.hullOf []
--> Nothing