meta data for this page
  •  

This is an old revision of the document!


Path

A path is a series of points, connected by very thin (invisible) line segments. Paths can be created as a line from an array of points, or by calling various functions to create or extend another path.

let mypathA = line([[10, 10], [-10, 10]])
let mypathB = line([[-10, -10], [10, -10]])
mypath = path2.concat(mypathA, mypathB)
mypath = path2.close(mypath)

In addition, a path can be open or closed. An open path is typically used to create another by appending additional points. A closed path is final, and has a line segment between first and last points.

The 'path2' geometry is accessed through the modeling API using the following:

const { path2 } = require('@jscad/modeling').geometries

Curved Paths

Curved paths can also be simulated. Paths can be created as an 'arc', or a 'bezier' curve.

The 'arc' primitive creates a curved path about a center. The arc is created from line segments placed the same distance from the center, at the given radius. Start and end angles for the arc can also be supplied. The segments specify the number of segments to create per full rotation.

Defaults:

  • center : [0, 0]
  • radius : 1
  • startAngle : 0
  • endAngle : PI * 2
  • segments : 32
let mypath = arc({radius: 5, startAngle: Math.PI / 2})
let mypath = arc({center: [5, 5], radius: 5, startAngle: Math.PI, endAngle: Math.PI / 2, segments: 4})

Appending Points

Append the given list of points to the end of the given geometry.

let mypath = line([[27, -22], [27, 22]])
mypath = path2.appendPoints([[-27, 22], [-27, -27]], mypath)
mypath = path2.close()

Append a series of points to the given geometry that simulate an arc.

Note: This implementation follows the SVG specifications.

Append a series of points to the given path that simulates a Bezier curve. The Bézier curve starts at the last point in the given path, and ends at the last control point. The other control points are intermediate control points to transition the curve from start to end points. The first control point may be 'null' to ensure a smooth transition occurs between curves.

let p5 = path2.create({}, [[10,-20]])
p5 = path2.appendBezier({controlPoints: [[10,-10],[25,-10],[25,-20]]}, p5);
p5 = path2.appendBezier({controlPoints: [null, [25,-30],[40,-30],[40,-20]]}, p5)

Conversion to 2D Shape

There are many ways to convert paths into to two dimensional shapes. The following provide some examples.

First, a two dimensional shape can be created from the points inside a path, but the path must be closed.

// create a closed path in shape of triangle
let mypath = line([[10, 10], [-10, 10], [-10, -10], [10, 10]])
let myshape = geom2.fromPoints(path2.toPoints(mypath))

Second, a path can be expanded into a two dimensional shape. The result is a shape that fits around the path. The path can be either open or closed.

// create an open path in shape of L
let mypath = line([[10, 10], [-10, 10], [-10, -10]])
let myshape = expand({delta: 2, corners: 'chamfer'}, mypath)

Conversion to 3D Shape

Hint: Any two dimensional shape can be extruded into a three dimensional shape.