Skip to content

Commit

Permalink
N-ary average()
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewcmyers committed Jan 21, 2023
1 parent c239f77 commit d0860e9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
13 changes: 10 additions & 3 deletions constrain.js
Original file line number Diff line number Diff line change
Expand Up @@ -1424,7 +1424,7 @@ class Figure {
return new CanvasRect(this.figure)
}
margin(n) {
return new CanvasRect(this.figure).inset(n === undefined ? this.lineWidth : n)
return new CanvasRect(this.figure).inset(n === undefined ? this.getLineWidth() : n)
}
rectangle(fillStyle, strokeStyle, lineWidth, x_hint, y_hint, w_hint, h_hint) {
return new Rectangle(this, fillStyle, strokeStyle, lineWidth, x_hint, y_hint, w_hint, h_hint)
Expand Down Expand Up @@ -1664,7 +1664,14 @@ class Figure {
sqrt(x) { return new Sqrt(legalExpr(x)) }
sqr(x) { return new Sq(legalExpr(x)) }
sq(x) { return new Sq(legalExpr(x)) }
average(x, y) { return new Average(legalExpr(x), legalExpr(y)) }
average(...args) {
if (args.length == 1) return legalExpr(args[0])
if (args.length == 2) {
return new Average(legalExpr(args[0]), legalExpr(args[1]))
}
const n = args.length
return plus(times(1/n, args[0]), times((n-1)/n, average(args.slice(1))))
}
distance(p1, p2, dims) { return new Distance(legalExpr(p1), legalExpr(p2), dims) }
nearZero(e, cost) { return new NearZero(this, legalExpr(e), cost) }
constraintGroup(...c) { return new ConstraintGroup(this, ...c) }
Expand Down Expand Up @@ -4620,7 +4627,7 @@ function flattenGraphicalObjects(objects) {
// intermediate objects along the way. Bezier splines are used to connect objects.
class Connector extends GraphicalObject {
constructor(figure, ...objects) {
super(figure, undefined, figure.strokeStyle, figure.lineWidth)
super(figure, undefined, figure.getStrokeStyle(), figure.getLineWidth())
this.fillStyle = this.strokeStyle
this.objects = flattenGraphicalObjects(objects)
this.labels = []
Expand Down
8 changes: 4 additions & 4 deletions doc/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -959,13 +959,13 @@ <h4>sqrt(a)</h4>
<h4>sq(a)</h4>
<p>The square of <code>a</code>.</p>

<h4>average(a, b)</h4>
<p>The average of the arguments, which may be arrays of the same length, so it is
possible to compute the average of two points.</p>

<h4>distance(p1, p2)</h4>
<p>The Euclidean distance between points <code>p1</code> and <code>p2</code>.</p>

<h4>average(a, b, ...)</h4>
<p>The average of the arguments, which may be arrays of the same length, so it is
possible to compute the average of two or more points.</p>

<h4>max(a, b, ...)</h4>
<p>The maximum of the arguments, which must all be scalars.</p>

Expand Down

0 comments on commit d0860e9

Please sign in to comment.