Skip to content

Commit 70fb8cc

Browse files
committed
For max and min constraints, pick randomly among the arguments when
all of them are equal.
1 parent 4c0d277 commit 70fb8cc

File tree

2 files changed

+40
-13
lines changed

2 files changed

+40
-13
lines changed

constrain.js

+30-12
Original file line numberDiff line numberDiff line change
@@ -3059,14 +3059,20 @@ class NaryExpression extends Expression {
30593059
class Min extends NaryExpression {
30603060
constructor(...args) { super(...args) }
30613061
operation(vals) {
3062-
let best = vals[0], n = vals.length, besti = 0
3062+
let best = vals[0], n = vals.length, besti = [0]
30633063
for (let i = 1; i < n; i++) {
30643064
if (best > vals[i]) {
30653065
best = vals[i]
3066-
besti = i
3066+
besti = [i]
3067+
} else if (best == vals[i]) {
3068+
besti.push(i)
30673069
}
30683070
}
3069-
this.which = besti
3071+
if (besti.length == 1) {
3072+
this.which = besti[0]
3073+
} else {
3074+
this.which = besti[Math.floor(Math.random() * besti.length)]
3075+
}
30703076
return best
30713077
}
30723078
gradop(vals) {
@@ -3089,14 +3095,20 @@ class Min extends NaryExpression {
30893095
class Max extends NaryExpression {
30903096
constructor(...args) { super(...args) }
30913097
operation(vals) {
3092-
let best = vals[0], n = vals.length, besti = 0
3098+
let best = vals[0], n = vals.length, besti = [0]
30933099
for (let i = 1; i < n; i++) {
30943100
if (best < vals[i]) {
30953101
best = vals[i]
3096-
besti = i
3102+
besti = [i]
3103+
} else if (best == vals[i]) {
3104+
besti.push(i)
30973105
}
30983106
}
3099-
this.which = besti
3107+
if (besti.length == 1) {
3108+
this.which = besti[0]
3109+
} else {
3110+
this.which = besti[Math.floor(Math.random() * besti.length)]
3111+
}
31003112
return best
31013113
}
31023114
gradop(vals) {
@@ -4752,10 +4764,16 @@ class Polygon extends Graphic {
47524764
super(figure, fillStyle, strokeStyle, lineWidth)
47534765
points = flattenGraphics(points)
47544766
this.points = points
4755-
figure.equal(this.x1(), figure.max(points.map(p => p.x())))
4756-
figure.equal(this.y1(), figure.max(points.map(p => p.y())))
4757-
figure.equal(this.x0(), figure.min(points.map(p => p.x())))
4758-
figure.equal(this.y0(), figure.min(points.map(p => p.y())))
4767+
const xpts = points.map(p => p.x()), ypts = points.map(p => p.y())
4768+
const maxx = figure.max(xpts), minx = figure.min(xpts), maxy = figure.max(ypts), miny = figure.min(ypts)
4769+
this.x_.remove()
4770+
this.x_ = figure.average(minx, maxx)
4771+
this.y_.remove()
4772+
this.y_ = figure.average(miny, maxy)
4773+
this.w_.remove()
4774+
this.w_ = figure.minus(maxx, minx)
4775+
this.h_.remove()
4776+
this.h_ = figure.minus(maxy, miny)
47594777
}
47604778
render() {
47614779
const figure = this.figure, ctx = figure.ctx
@@ -6787,10 +6805,10 @@ class CanvasRect extends LayoutObject {
67876805
x1() { return new Global(() => {
67886806
if (!this.figure.width) this.figure.setupCanvas()
67896807
return this.figure.width
6790-
}, "Width of figure " + this.figure.name)}
6808+
}, "Max x coordinate of figure " + this.figure.name)}
67916809
y0() { return 0 }
67926810
y1() { return new Global(() => this.figure.height,
6793-
"Height of figure " + this.figure.name) }
6811+
"Max y coordinate of figure " + this.figure.name) }
67946812
w() { return new Global(() => this.figure.width,
67956813
"Width of figure " + this.figure.name)}
67966814
h() { return new Global(() => this.figure.height,

doc/index.html

+10-1
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,19 @@
2727
div.inset { border: 1px solid #ccc; padding: 1ex 4ex }
2828
body {background-color: #fed }
2929
p, ul li { font-size: 12pt; font-family: Georgia, serif }
30-
pre.code, textarea { padding: 1ex; font-size: 10pt; width: 60ex; float:right; border: 1px solid black; background-color: #ddd; font-family: Menlo, fixed }
30+
pre.code, textarea {
31+
padding: 1ex;
32+
font-size: 10pt;
33+
width: 60ex;
34+
float:right;
35+
border: 1px solid black;
36+
background-color: #ddd;
37+
font-family: Menlo, fixed
38+
}
3139
textarea { border-width: 3px }
3240
code { font-family: Menlo, fixed; font-size: 90% }
3341
@media print {
42+
body { font-size: 11pt; margin: 1in }
3443
pre.code {
3544
page-break-inside: avoid;
3645
}

0 commit comments

Comments
 (0)